Solved

# passing an array to a function

Posted on 2000-03-06
249 Views
Last Modified: 2010-04-02
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
0
Question by:zhu_zheng
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 2
• 2
• +6
15 Comments

LVL 33

Expert Comment

ID: 2590330
Hope the below code is correct. One problem with your code is that the lower index of the 2D array must have a value.

#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
0

LVL 3

Expert Comment

ID: 2590333
What is the problem you are getting ?
0

LVL 84

Expert Comment

ID: 2590357
the function prototype is missing a ;
n is undefined in main
an uninitialised pointer is passed to row
and // is a c++ comment, not a c comment
0

Author Comment

ID: 2590445
Thanks to hongjun's answer.The reason to reject the answer is because I need to use point as a argument instead of a known array.

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)+y);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

0

LVL 1

Expert Comment

ID: 2590565
But, how U allocate memory for matrix?
0

LVL 2

Expert Comment

ID: 2590606
Are you initializing 'matrix' like this?

for (int x = 0; x < n; ++x) {
*(matrix+x) = malloc(n*sizeof(int));
for (int y = 0; y < n; ++y) {
*(*(matrix+x)+y) = somevalue();
}
}
0

LVL 12

Expert Comment

ID: 2590633
You need to multiply "y" by the row length to read the correct values for the array. Thus, your sum line should look like:

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

LVL 3

Expert Comment

ID: 2590652
How are you using the output of
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)+y);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
sum+=sum_row;
}
}

0

LVL 1

Expert Comment

ID: 2590703
but U can't use 1D array like this?

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*n);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

0

LVL 33

Expert Comment

ID: 2590872
Are you using pointer? If yes then you may have to use malloc or alloc to allocate memory.

hongjun
0

LVL 5

Expert Comment

ID: 2591267
Since n is variable, we cannot do the proper thing and work with predefined structure types.  We have to create our own structure on-the-fly by multiplying the row number by n when we access the matrix, as was said by pjknibbs.
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);
}
}

0

LVL 2

Accepted Solution

fremsley earned 50 total points
ID: 2591706
I think, this is what most people here consider the main point:

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)

0

LVL 2

Expert Comment

ID: 2591740
I seem to have mixed 'array' and 'matrix' above. Sorry
0

Expert Comment

ID: 2594982
Hi,
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,4000};
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)+x);
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
0

Author Comment

ID: 2596906
My question is very well answered by many people.
Thank you all for your intelligent inputs and your kindly heart.
Thanks,
-Zheng
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll

#### 732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.