Thursday, July 30, 2009

Passing an Array to a Function C++?

what am I doing wrong? Im trying to simply pass the array testvals to calcaverage. I just want the program to calculate the average and then pass the average calculated back to main.





I get an error of "variable-sized object `testvals' may not be initialized "








#include %26lt;iostream%26gt;


using namespace std;





int calcaverage(int);





int main()


{


int NUMEL, average;


int testvals[NUMEL] = {89,95,72,83,99,54,86,75,92,73,79,75,82,...





cout %26lt;%26lt; "WELCOME! ";





average = calcaverage(testvals[NUMEL]);


cout %26lt;%26lt; "the average of these numbers is: " %26lt;%26lt; average;











cout %26lt;%26lt; endl %26lt;%26lt; endl;


system("PAUSE");


return 0;





}





int calcaverage(int testvals)


{





int i, average=0, total=0, NUMELS=14;





for (int i = 0; i %26lt; NUMELS; i++)





total += testvals;


average = total/14;





return average;


}

Passing an Array to a Function C++?
You never initialize NUMEL to a value, so what size is testvals[] suppose to be?





Pass the array AND the size of the array to calcaverage(). The function prototype can then be calcaverage(int a[], int size). It currently is just taking a single int.





In calcaverage(), there is no need to define variable i twice - or NUMELS at all. Now that you are passing a valid array to the function you have to address it with a subscript.





Below are the corrections.





#include %26lt;iostream%26gt;


using namespace std;





int calcaverage(int testvals[], int size);





int main()


{


const int NUMEL = 14;


int average;


int testvals[NUMEL] = {89,95,72,83,99,54,86,75,92,73,79,75,82, 88};





cout %26lt;%26lt; "WELCOME! ";





average = calcaverage(testvals, NUMEL);


cout %26lt;%26lt; "the average of these numbers is: " %26lt;%26lt; average;





cout %26lt;%26lt; endl %26lt;%26lt; endl;


system("PAUSE");


return 0;


}





int calcaverage(int testvals[], int size)


{





int average=0, total=0;





for (int i = 0; i %26lt; size; i++)


{


total += testvals[i];


}





average = total / size;





return average;


}
Reply:DO NOT use C-style arrays in C++. That is just plain bad style. Try using std::vector%26lt;int%26gt; instead. See http://www.cplusplus.com/reference/stl/v...





But if you insists using the C style arrays:








The function prototype for calcaverage should be:





int calcaverage(int testvals[NUMEL] )





OR





int calcaverage(int * testvals , int numtestVals)





Which I prefer more.





Then call





calcaverage(testvals)





OR





calcaverage(%26amp;testvals[0], NUMELS)
Reply:I don't think thats the correct way of passing array to the function:


the signature of the function should be like this:


int calcaverage(int testvals[]);





to call this function you simply have to do this:


average = calcaverage(testvals);





and also the NUMEL should be some initialized constant.


const int NUMEL = 5;


No comments:

Post a Comment