Why debugger shows problem before sorting? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have problem with my program written in C. I want to draw int numbers to 2-size array, show it and then sort numbers in lines, and then make transposition with array, but it doesn't work. debugger show error in line with printf in function main() but i totally don't know what's wrong.
Thank for all support.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>//dla swapa
void draw(int **tab, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
tab[m][n] = rand() % (100 + 1); //0-100
}
}
}
void sort(int **tab, int m, int n)
{
m = 0;
while (m)
{
for (int j = n - 1; j > 0; j--)
{
for (int i = 0; i < n; i++)
{
if (tab[i] > tab[i + 1])
{
std:: swap(tab[i], tab[i + 1]);
}
}
}
m++;
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("|%d||[%d|\n", tab[i][j]);
}
}
}
int main()
{
int w, k;
int **tab;
printf("Lines: ");
scanf(" %d", &w);
printf("columns: ");
scanf(" %d", &k);
tab = (int**)malloc(w*sizeof(int*));
for (int i = 0; i < k; i++) {
tab[i] = (int*)malloc(k * sizeof(int));
}
printf("\nBefore sorting: \n");
for (int i = 0; i < w; i++)
{
for (int j = 0; j < k; i++)
{
printf("|%d||%d|", tab[i][j]);
}
}
// lub tab=(int*)malloc(w*k*sizeof(int));
srand(time(NULL));
draw(tab, w, k);
printf("\nSorted array:\n");
sort(tab,w,k);
}

The line for (int i = 0; i < k; i++) { should be for (int i = 0; i < w; ++i)
The line printf("|%d||%d|", tab[i][j]); should be printf("tab[%d][%d] = %d\n", i, j, tab[i][j]);
Your function sort() is wrong.
You should use like row, col not w, k
The following code could work:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
void draw(int **tab, int row, int col) {
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
tab[i][j] = rand() % (100 + 1);
}
void sort(int **tab, int row, int col) {
int x = 0;
int y = 0;
while (x != row) {
int temp = tab[x][y];
for (int j = y + 1; j != col; ++j)
if (tab[x][j] < tab[x][y])
std::swap(tab[x][j], tab[x][y]);
for (int i = x + 1; i != row; ++i)
for (int j = 0; j != col; ++j)
if (tab[i][j] < tab[x][y])
std::swap(tab[i][j], tab[x][y]);
++y;
if (y == col) {
y = 0;
++x;
}
}
}
void output(int** tab, int row, int col) {
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
printf("tab[%d][%d] = %d\n", i, j, tab[i][j]);
}
int main() {
int row, col;
int **tab;
printf("Lines: ");
scanf(" %d", &row);
printf("columns: ");
scanf(" %d", &col);
tab = (int **)malloc(row * sizeof(int *));
for (int i = 0; i < row; ++i)
tab[i] = (int *)malloc(col * sizeof(int));
printf("\nBefore sorting: \n");
srand(time(NULL));
draw(tab, row, col);
output(tab, row, col);
printf("\nSorted array:\n");
sort(tab, row, col);
output(tab, row, col);
return 0;
}

The following lines (it appears twice):
printf("|%d||[%d|\n", tab[i][j]);
Wants to print two numbers but you are only giving it a single value tab[i][j] to print.
You also need to look at this code:
tab = (int**)malloc(w*sizeof(int*));
for (int i = 0; i < k; i++) {
tab[i] = (int*)malloc(k * sizeof(int));
}
Here you allocate w array elements but then use kwhen in the for loop.

Related

C++ on vscode: Debug get error but run code without debug is still run

This is my code to sorting array 2D use extra array or don't. I use funtion pointer like this
int main() {
input();
printf("Array 2D: \n");
output();
printf("After sorting: \n");
solve(sortWithoutUseExtraArr);
output();
}
and my funtion:
void sortWithoutUseExtraArr { ..... }
void solve(void (*sort)()) {
(*sort)();
}
When i run without debug. It run perfectlly. But when i start debug. It get error:too many arguments to function 'void solve()'
Can everyone explain this for me. Thank you so much.
And sorry because my english is too bad!
EDIT: the entire code:
#include<stdio.h>
#define MAX 100
int a[MAX][MAX];
int row, column;
void input();
void output();
void sortArray1D();
void sortUseExtraArr();
void sortWithoutUseExtraArr();
void solve();
int main() {
input();
printf("Array 2D: \n");
output();
printf("After sorting: \n");
solve(sortWithoutUseExtraArr);
output();
}
void input() {
printf("Nhap so hang: ");
scanf("%d", &row);
printf("Nhap so cot: ");
scanf("%d", &column);
for (int i = 0; i < row;++i)
{
for (int j = 0; j < column;++j)
{
printf("a[%d][%d] = ", i + 1, j + 1);
scanf("%d",&a[i][j]);
}
}
}
void output() {
for (int i = 0; i < row;++i) {
for (int j = 0; j < column;++j) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
void sortArray1D(int arr[],int n) {
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void sortUseExtraArr() {
int b[MAX * MAX];
int n = 0;
for (int i = 0; i < row;++i)
{
for (int j = 0; j < column;++j)
{
b[n] = a[i][j];
++n;
}
}
sortArray1D(b, n);
int d = 0;
for (int i = 0; i < row;++i)
{
for (int j = 0; j < column;++j)
{
a[i][j] = b[d];
++d;
}
}
}
void sortWithoutUseExtraArr() {
int n = row * column;
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
if(a[i / column][i % column] > a[j / column][j % column])
{
int temp = a[i / column][i % column];
a[i / column][i % column] = a[j / column][j % column];
a[j / column][j % column] = temp;
}
}
}
}
void solve(void (*sort)()) {
(*sort)();
}

Find the sum of an entire matrix using functions (C++)

I am writing some code that writes a matrix with a 10x10 size and prints it using functions. What I need is a function that takes the sum of the entire matrix array. What I have done is that I tried to sum up each row and column separately and add the total together. The problem with mine is that I believe that it only prints out the sum of the row or the column.
This is the code I have:
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int ROW_SIZE = 10;
const int COLUMN_SIZE = 10;
void initialize(int [][10], int, int);
void display(int matrix[][10], int, int);
void sum(int matrix[][COLUMN_SIZE], int, int);
int main() {
int matrix [ROW_SIZE][COLUMN_SIZE];
initialize(matrix, ROW_SIZE, COLUMN_SIZE);
display(matrix, ROW_SIZE,COLUMN_SIZE);
sum(matrix, ROW_SIZE,COLUMN_SIZE);
return 0;
}
void initialize(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for (int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
matrix[i][j] = 1 + rand() % 99;
}
}
}
void display(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
cout<< setw(4)<<matrix[i][j]<< " ";
}
cout<< endl;
}
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row;
int sum_col;
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j];
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
What you have done is sum of last row + last column. This should be enough:
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
few early problems in your code
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row; // uninitialized make it equal to 0
int sum_col; // uninitialized make it equal to 0
for(int i = 0; i < ROW_SIZE; i++){
int sum_row = 0; // This is local to block and masks the sum_row above
for(int j = 0; j < COLUMN_SIZE; j++){
sum_row = sum_row + matrix[i][j];
}
}
for(int i = 0; i < ROW_SIZE; i++){
int sum_col = 0;
for(int j = 0; j < COLUMN_SIZE; j++){
sum_col = sum_col + matrix[i][j]; //Iterating over rows again matrix[j][i] may be
}
}
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum_row = 0;
int sum_col = 0;
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_row += matrix[i][j];
for(int i = 0; i < ROW_SIZE; i++)
for(int j = 0; j < COLUMN_SIZE; j++)
sum_col += matrix[j][i];
int sum = sum_row + sum_col;
cout<<"The sum of the matrix is "<<sum<< endl;
}
Now of course the question and logic doesn't seem to co-exist together and if sum of entire matrix means sum of all elements which is what it sounds like it can be achieved with the one here
void sum(int matrix[][COLUMN_SIZE], int ROW_SIZE, int COLUMN_SIZE){
int sum = 0;
for(int i = 0; i < ROW_SIZE; i++){
for(int j = 0; j < COLUMN_SIZE; j++){
sum += matrix[i][j];
}
}
cout<<"The sum of the matrix is "<<sum<< endl;
}
You're doing something wrong in terms of naming. You have constants called ROW_SIZE and COLUMN_SIZE and then you have parameters with the same names to functions like sum(). This is confusing for something reading your code. Call your parameters something else, like rows and columns.
Here is an alternative to the proposed solutions using std::accumulate().
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 3;
void sum(int matrix[][COLUMN_SIZE], int const rows, int const cols)
{
int sum = std::accumulate(&matrix[0][0], &matrix[rows-1][cols], 0);
std::cout<<"The sum of the matrix is "<<sum<< std::endl;
}
int main()
{
int m[ROW_SIZE][COLUMN_SIZE] {{1,2,3}, {4,5,6}, {7,8,9}};
sum(m, ROW_SIZE, COLUMN_SIZE);
}
std::accumulate() takes the iterators that delimit a range. Those are the iterators to the first element and the one past the last element of the range. In this case, these are &matrix[0][0] and &matrix[row-1][col] (since &matrix[row-1][col-1] is the last element of the matrix). There are other ways to put that, such as the following:
auto begin = std::begin(matrix[0]);
int sum = std::accumulate(begin, begin + rows * cols, 0);

multiply two negative numbers in c++

when I tried to multiple two negative numbers the value it is zero in c++,
for example -5 * -3
the result is zero,
why?
this is my code
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void Multiply(const int v_arr[], const int m_arr[][3], int signed
o_arr[], int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
o_arr[i] = 0;
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
}
//End your code here
}
int main()
{
int n;
cin >> n;
int v_array[n];
int m_array[n][3];
int signed o_array[3];
for (int i = 0; i < n; i++) {
cin >> v_array[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> m_array[i][j];
}
}
//fuction
Multiply(v_array, m_array, o_array, n);
for (int j = 0; j < 3; j++) {
cout << o_array[j] << " ";
}
return 0;
}
how to fix it to get the correct result?
the input is
2
2 -3
2 -3
2 -4
Your issue is here:
for (int k = 0; k < 3; k++)
o_arr[i] += v_arr[k] * m_arr[k][i];
}
You access elements at indices 0, 1 and 2 in v_arr, but it only has 2 elements. That's Undefined Behaviour.
Assuming this is matrix*vector multiplication code, it should look like this (untested):
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
Also, your loop based on j is useless. You can remove it:
void Multiply(const int v_arr[], const int m_arr[][3], int signed o_arr[], int size)
{
for(int k = 0; k < 3; k++) { //initialize output array
o_arr[k] = 0;
}
for (int i = 0; i < size; i++) {
for (int k = 0; k < 3; k++)
o_arr[k] += v_arr[i] * m_arr[i][k];
}
}

Counting all the number in a 2d array

I want to count all the numbers in a 2d array and store the count into another array so can I can use the values in a histogram equalization.I am counting values that range from 0 to 255, so everytime a number for example 18 comes up in the 2d array I want to count how many 18's there are in the 2d array and then store the count into num[17]. Problem is I don't get the right amount. I know is due to the temp not being in the right place but I cannot figure out where to put it. Any help would be appreciated.
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int temp = 0;
for (int k = 1; k <= 255; k++)
{
if (pix[i][j] == k)
{
temp = temp + 1;
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}
}
}
It's not very clear form your question, but my best guess is that you need this:
void histeq(int **pix, int height, int width) {
int num[256] = {0};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]] += 1;
}
}
for (int i = 0; i < 256; ++i)
{
cout << num[i] << endl;
}
}
#include <map>
void histeq(int **pix, int height, int width) {
std::map <int, int> num;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num[pix[i][j]]++;
}
}
for (int i = 0; i < num.size(); i++)
cout << num[i] <<endl;
}
I got your expectation. You mean you want to count how many time number of arange[0:255] in 2d Array:
#include <iostream>
void histeq(int **pix, int height, int width) {
int num[255];
for (int k =1; k <= 255; k++)
{
int temp = 0;
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
if (pix[i][j] == k)
{
temp +=1;
}
}
}
num[k - 1] = temp;
cout << num[k - 1] << endl;
}
}

How to use pointer to pointer for passing matrix?

Passing matrix as a pointer to pointer to function not working.
#include <stdio.h>
void printMatrix(int **matrix, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", matrix[i][j]);
printf("\r\n");
}
}
void printM (size_t row, size_t col, int matrix[3][4])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", matrix[i][j]);
printf("\r\n");
}
}
int main()
{
int M[3][4];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
M[i][j] = 4*i+j;
printM(3, 4, M);
int *row = *M;
printMatrix(&row, 3, 4); //not working
}
Function printM works, but I would like to know how to use pointer to pointer correctly, thanks for help.
First of all, thank-you for this question. It is a good review of how C does multi-dimensional arrays. Also, it is OK to do double pointers. Remember an array reference is equivalent to a pointer, such as: a[0] and *a both refer to the first element of int a[12]; where *a is the de-referencing of pointer a. And so, &M is a the address of the pointer M when M is declared as int M[3][4];
I modified your code by adding a few comments for clarity and so that it would run in Eclipse using a C compiler from Microsoft, specifically, int declarations where moved out of the for statements. Other than that it is the same as what you originally wrote with a change to the printMatrix declaration and how it is invoked.
Hope this helps, please ask if more questions...
#include <stdio.h>
void printMatrix(int (*matrix)[3][4], int row, int col)
{
int i, j;
// point t so that when de-referenced it is at
// the matrices first element
int *t = (*matrix)[0];
printf("\n");
for (i = 0; i < row; i++)
{
// in C matrices are stored in Row Major form, so
// per K&R just sequentially loop thru (*t)[12]
for (j = 0; j < col; j++) {printf("%d ", *t++);}
printf("\r\n");
}
} // end printMatrix
void printM (size_t row, size_t col, int matrix[3][4])
{
int i, j;
printf("\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++) {printf("%d ", matrix[i][j]);}
// new line for next row
printf("\r\n");
}
}
int main()
{
int i,j;
// define a matrix with 3 rows and 4 columns
int M[3][4];
// fill-in the matrix with values
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
M[i][j] = 4*i + j;
// print the three rows and four columns of M
printM(3, 4, M);
printMatrix(&M, 3, 4); // Also Works
} // end main
void printMatrix(int *matrix, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
printf("%d ", *(matrix+(i*col)+j);
printf("\r\n");
}
}
Don't do a double pointer.