How to pass multi-dimensional array to function?


I am trying to pass a 2D array of variable size to a function to print it.but the code doesn't show the exact result of sum.

this is the code:

#include <stdio.h> #define ROW 5 #define COLL 5 void print_arr(int a[][COLL],int m,int n){ int i,j,sum; for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("a[%d][%d]=%d\n",i,j,a[i][j]); } } } int sum_arr(int a[][COLL],int m,int n){ int i,j,sum; for(i=0;i<m;i++){ for(j=0;j<n;j++){ sum+=a[i][j]; } } return sum; } int main (void){ int a[ROW][COLL]; int i,j,m,n; int sum; printf("enter rows:");scanf("%d",&m); printf("enter coll:");scanf("%d",&n); for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("a[%d][%d]=",i,j);scanf("%d",&a[i][j]); } } print_arr(a,m,n); printf("\n"); sum=sum_arr(a,m,n); printf("sum=%d\n",sum); return 0; }

this is the result of the code

enter rows:2 enter coll:3 a[0][0]=5 a[0][1]=8 a[0][2]=4 a[1][0]=7 a[1][1]=9 a[1][2]=6 a[0][0]=5 a[0][1]=8 a[0][2]=4 a[1][0]=7 a[1][1]=9 a[1][2]=6 sum=-1217388517

please tell me what's wrong with the code....


You should pass the exact size of the second dimension of the array to the function, not COLL. change it to m (or n, whatever) It passes the number 5 to the function while the number should be 3 :) How ever, this is not the main reason that you're code is not working, just a suggestion. Initialize the variable sum. It will make your code work. e.g. sum = 0; If you don't initialize it, you won't get any compile errors, but it points to a location of memory and reads thing been there before (not a valid amount) and uses it as the initial amount of that for sum. So your array is being added to a non-valid amount, that's why your code doesn't work.


There is no technical problem with passing, but in sum_arr,<br /> your variable sum does not start at 0 (but some strange value).


You have to initialize sum to zero in sum_arr function.


  • Mongo collection query and Operators
  • Why is some code from K&R not working in Code:Blocks?
  • Approach for better solution - Sum of medians
  • Increase the speed of redrawing contour plot in matplotlib
  • My C program reads a text file. When I put it into Xcode it fails. What's happening?
  • Doubt regarding a tail optimized code under 'gdb'
  • ObjectID generated by server on pymongo
  • Calculating Digital Root, is there a better way?
  • Plot a decision tree with R
  • Making a switch statement in C with an array?
  • How to delete a newline using \\b
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • allocating memory to an array of string
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • Sencha Touch 2.0 Controller refs attribute not working?
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Retrieving value from sql ExecuteScalar()
  • What is the “return” in scheme?
  • vba code to select only visible cells in specific column except heading
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Do I've to free mysql result after storing it?
  • Unanticipated behavior
  • Transpose CSV data with awk (pivot transformation)
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • EntityFramework adding new object to nested object collection
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?