Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.

I want to calculate the sum of each row on a 2 dimension array. Also, I need to make it as a function using pointer as argument.But, the function couldn't work. Here is my code:

int **matrix; //matrix is a 2 dimension array.

void row(int **ptr,int n)

main()

{

/*another part of the code go in here to calculate the value of matrix*/

row(matrix,n);

}

void row(int **ptr,int n)

{

int sum_row,x=0,y=0;

for (y=0; y<=n-1;y++)

{

sum_row=0;

for (x=0;x<=n-1;x++)

{

sum_row=sum_row+*(*(ptr+x)+y);

}

printf("The sum of row %d is %d\n", y+1, sum_row);

}

}

Is there anything wrong when I pass the point to the function?

Thank you very much. -Zheng

int **matrix; //matrix is a 2 dimension array.

void row(int **ptr,int n)

main()

{

/*another part of the code go in here to calculate the value of matrix*/

row(matrix,n);

}

void row(int **ptr,int n)

{

int sum_row,x=0,y=0;

for (y=0; y<=n-1;y++)

{

sum_row=0;

for (x=0;x<=n-1;x++)

{

sum_row=sum_row+*(*(ptr+x)

}

printf("The sum of row %d is %d\n", y+1, sum_row);

}

}

Is there anything wrong when I pass the point to the function?

Thank you very much. -Zheng

#include <stdio.h>

void row(int ptr[10][10],int n);

void main()

{

int n;

int a[10][10];

printf("Enter the number of integers you wish to enter > ");

scanf("%d",&n);

row(a,n);

}

void row(int ptr[10][10], int n)

{

int i,j;

int count=0;

int total=0;

printf("Enter the %d number of integers >\n",n);

for ( i=0; i<=9; i++ )

{

total=0;

for ( j=0; count<n && j<=9; j++ )

{

scanf("%d",&ptr[i][j]);

total += ptr[i][j];

count++;

}

printf("The total is for row %d is %d\n",i+1,total);

}

}

hongjun

n is undefined in main

an uninitialised pointer is passed to row

and // is a c++ comment, not a c comment

I'd like to make my question clear. I have calculated the value of each element in a n*n array. In main(), I need to calculate the sum on each row by calling a function. The pointer should be used as an argument, since n is unknown. I run the following code, it end up "0" no matter what the matrix value is. Something must be wrong in passing the pointer to the function.Where is wwrong?

int n, **matrix;

void row(int **ptr,int n);

main()

{ printf("Enter the n value here:\n");

scanf("%d",n);

/* value of *(*(matrix+x)+y)will be calculate here */

/*call function row*/

row(matrix,n);

}

void row(int **ptr,int n)

{

int sum_row,x=0,y=0;

for (y=0; y<=n-1;y++)

{

sum_row=0;

for (x=0;x<=n-1;x++)

{

sum_row=sum_row+*(*(ptr+x)

}

printf("The sum of row %d is %d\n", y+1, sum_row);

}

}

for (int x = 0; x < n; ++x) {

*(matrix+x) = malloc(n*sizeof(int));

for (int y = 0; y < n; ++y) {

*(*(matrix+x)+y) = somevalue();

}

}

sum_row=sum_row+*(*(ptr+x)

void row(int **ptr,int n) function in main ?

Actually you could return the sum_row as a return value from row function if you are using it to calculate the sum for a row.

I feel better is you do the following.

int n,sum, **matrix;

void row(int **ptr,int n);

main()

{ printf("Enter the n value here:\n");

scanf("%d",n);

/* value of *(*(matrix+x)+y)will be calculate here */

/*call function row*/

row(matrix,n);

/* Use sum here which contains value of matrix sum or you can

have sum as a pointer argument to row() which will populate it with

value of matrix sum*/

printf("The sum of matrix is %d\n", sum);

}

void row(int **ptr,int n)

{

int sum_row,x=0,y=0;

for (y=0; y<=n-1;y++)

{

sum_row=0;

for (x=0;x<=n-1;x++)

{

sum_row=sum_row+*(*(ptr+x)

}

printf("The sum of row %d is %d\n", y+1, sum_row);

sum+=sum_row;

}

}

int n, *matrix;

void row(int *ptr,int n);

main()

{

int x, y;

printf("Enter the n value here:\n");

scanf("%d",&n);

matrix = (int *)malloc(n*n*sizeof(int));

for (x = 0; x < n; ++x) {

for (y = 0; y < n; ++y) {

*(matrix+x+y*n) = somevalue();

}

}

row(matrix,n);

}

void row(int *ptr,int n)

{

int sum_row,x=0,y=0;

for (y=0; y<=n-1;y++)

{

sum_row=0;

for (x=0;x<=n-1;x++)

{

sum_row=sum_row+*(ptr+x+y*

}

printf("The sum of row %d is %d\n", y+1, sum_row);

}

}

Here is a complete program that produces the output below when n is set to 5.

The sum of row 0 is 10

The sum of row 1 is 35

The sum of row 2 is 60

The sum of row 3 is 85

The sum of row 4 is 110

#include <stdio.h>

#include "malloc.h"

void addRow(int *ptr,int n);

void main()

{

int *matrix;

int column, row, n, k;

k = 0;

printf("Enter the n value here:\n");

scanf("%d",&n);

matrix= malloc(n*n*sizeof(int));

/*fill the array with values.*/

for( row=0; row<n; row++ ){

for(column=0; column<n; column++) {

*(matrix + row*n + column) = k++;

}

}

/*call function addRow to print the sum of each row*/

addRow(matrix,n);

}

}

void addRow(int *ptr,int n)

{

int sum_row;

int row=0;

int column=0;

for (row=0; row<n; row++)

{

sum_row=0;

for (column=0; column<n; column++)

{

sum_row=sum_row+ *(ptr + row*n + column);

}

printf("The sum of row %d is %d\n", row, sum_row);

}

}

He wrongly placed the fields x and y.

Find below is the program and output. I hope it will do for him.

Assumed : To calculate the sum of row values.

# include <stdio.h>

int **matrix;

void row(int **ptr,int n);

main()

{

int n = 4;

int val1[4]={1,2,3,4},

val2[4]={10,20,30,40}, val3[4]={100,200,300,400},

val4[4]={1000,2000,3000,40

int *ip[4];

ip[0] = val1;

ip[1] = val2;

ip[2] = val3;

ip[3] = val4;

matrix = ip;

/*another part of the code go in here to calculate the value of matrix*/

row(matrix,n);

}

void row(int **ptr,int n)

{

int sum_row,x=0,y=0,old_value;

for (y=0; y<=n-1;y++)

{

sum_row=0;

old_value = 0;

for (x=0;x<=n-1;x++)

{

old_value = sum_row;

sum_row=sum_row+*(*(ptr+y)

old_value = sum_row - old_value;

printf("The value is %d \n",old_value);

}

printf("The sum of row %d is %d\n", y+1, sum_row);

}

}

Output :

----------

The value is 1

The value is 2

The value is 3

The value is 4

The sum of row 1 is 10

The value is 10

The value is 20

The value is 30

The value is 40

The sum of row 2 is 100

The value is 100

The value is 200

The value is 300

The value is 400

The sum of row 3 is 1000

The value is 1000

The value is 2000

The value is 3000

The value is 4000

The sum of row 4 is 10000

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

You have two basic choices on how to arrange your 'matrix' (assuming both dimensions to be 'N').

1. You allocate one array of N*N values.

array = malloc(N*N*sizeof(int));

Access to a cell is then:

matrix[x*N + y] or *(matrix + x*N + y)

2. In your example you created an array of pointers pointing to row arrays. In this case both, the pointer array and each row must be allocated:

array = malloc(N*sizeof(int *));

for (i=0..N-1) array[i] = malloc(N*sizeof(int));

Access to a cell is like this:

array[x][y] or *(*(array+x)+y)