Solved

passing an array to a function

Posted on 2000-03-06
15
209 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
Comment
Question by:zhu_zheng
  • 3
  • 2
  • 2
  • +6
15 Comments
 
LVL 33

Expert Comment

by:hongjun
Comment Utility
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

by:ufolk123
Comment Utility
What is the problem you are getting ?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

by:zhu_zheng
Comment Utility
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

by:mahno
Comment Utility
But, how U allocate memory for matrix?
0
 
LVL 2

Expert Comment

by:fremsley
Comment Utility
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

by:pjknibbs
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:ufolk123
Comment Utility
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

by:mahno
Comment Utility
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

by:hongjun
Comment Utility
Are you using pointer? If yes then you may have to use malloc or alloc to allocate memory.

hongjun
0
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
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

by:
fremsley earned 50 total points
Comment Utility
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

by:fremsley
Comment Utility
I seem to have mixed 'array' and 'matrix' above. Sorry
0
 

Expert Comment

by:jayak
Comment Utility
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

by:zhu_zheng
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now