### Void function

HEy everyone thanks for your time. the code i have here is used to find the sum,average and product , i got them to work in a normal function which returns a value but now i want to try it in a void function. i have the other parts of the program except for the average part disabled when i call the function in int maini always end up with 1. The numbers from the user are being stored in a array.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` `````` #include using namespace std; //prototyping variables void averageCal(double a[]); //double productCal (double p[]); //double sumCal (double s[]); int main () { double userinputs [5];//storing the user inputs in a array for(int i=0; i<5; i++)//suing a for loop to cycly and store the user inputs { cout<<"Please enter a value: "; cin>>userinputs[i]; }//end of for loop for user inputs //displaying the average,product and sum on screen //cout << "The average of the 5 numbers are: " << averageCal(userinputs) << endl; //cout << "The product of the 5 numbers are: " << productCal(userinputs) << endl; //cout << "The sum of the 5 numbers are: " << sumCal(userinputs) << endl; //this is where i want the program to display the average cout<<"The average of the five numbers is:"<
Look at the following snippet:
 ``12`` ``````//this is where i want the program to display the average cout<<"The average of the five numbers is:"<

First averageCal is a function, you need the (). Second since averageCal doesn't return a value you can't try to print this non-value. Since your function is printing to the screen just call the function.

But I really recommend you return the average from the function instead of the void function, and don't do the printout in the function. Try to have your functions do as few things as possible. In this case just preform the calculation.

I also suggest you add another parameter to your call, the size of the array. And then use the parameter in your loop to replace the "magic number" 5. This way you can use this function with any size array, not just an array with 5 elements.

look line 32! averageCal is a function. are you trying to make a function call in your cout? The call to averageCal has no argument.
Thanks for your reply and yes i did the entire program and used the return but i wanted to see how can i make it work with with void, how do i call it in my int main cause every time i try to call it is displays a 1 instead of the right answer.
change line 32 as follows.

`cout << "The average of the five numbers is:" ;`

then make call to averageCal with userinputs as arguments.

then change line 50 to...
`cout << average_ans << endl;`
 ``12`` ``````//this is where i want the program to display the average cout<<"The average of the five numbers is:"<

i changed it to this but i still dont get anything on the screen

 ``12`` ``````//this is where i want the program to display the average averageCal;``````

This is what i did after your reply but i got more errors :(
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include using namespace std; //prototyping variables void averageCal(double a[]); //double productCal (double p[]); //double sumCal (double s[]); int main () { double userinputs [5];//storing the user inputs in a array for(int i=0; i<5; i++)//suing a for loop to cycly and store the user inputs { cout<<"Please enter a value: "; cin>>userinputs[i]; }//end of for loop for user inputs //displaying the average,product and sum on screen //cout << "The average of the 5 numbers are: " << averageCal(userinputs) << endl; //cout << "The product of the 5 numbers are: " << productCal(userinputs) << endl; //cout << "The sum of the 5 numbers are: " << sumCal(userinputs) << endl; cout << "The average of the five numbers is:" <
Last edited on
supply the array in arguments.

I think like this...
`averageCal(unserinputs[]);`
32|error: expected primary-expression before ']' token|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

when i added the brackets i got that error. ln 32
Last edited on
okay I play with your code and here it is finished...

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; //prototyping variables void averageCal(double a[], int size); //double productCal (double p[]); //double sumCal (double s[]); int main () { double userinputs [5];//storing the user inputs in a array for(int i=0; i<5; i++)//suing a for loop to cycly and store the user inputs { cout<<"Please enter a value: "; cin>>userinputs[i]; }//end of for loop for user inputs //displaying the average,product and sum on screen //cout << "The average of the 5 numbers are: " << averageCal(userinputs) << endl; //cout << "The product of the 5 numbers are: " << productCal(userinputs) << endl; //cout << "The sum of the 5 numbers are: " << sumCal(userinputs) << endl; cout << "The average of the five numbers is:"; averageCal(userinputs, 5); cout << endl; cout << "Press enter to continue."; cin.get(); cin.get(); return 0; } //function for finding the average void averageCal (double a[], int size) { //initialize variables double addition=0; double average_ans = 0; for (int c = 0; c < size; c++)//for loop to add the numbers {addition = addition + a[c];}//end of for loop average_ans = addition / 5;//dividint the numbers by 5 cout << average_ans << endl; }//end of function ``````
closed account (NyhkoG1T)
I wrote this before scrolling down the post to see you already found an answer but here it is for reference if it is needed.

I used a dynamic array so the user can enter as many numbers as they wish and then get the average
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include void getAverage(double a[], int size, int *pVar) { int tempHold = 0; for (int n=0; n> arraySize; userinput=new (nothrow) double [arraySize]; if(userinput==0) { cout << "Couldn't allocate memory."; return 1; } for(int n=0; n> userinput[n]; } getAverage(userinput, arraySize, &average); cout << "The result is: " << average << endl; delete [] userinput; return 0; }``````

Also, a neat trick (that doesn't work with dynamic arrays though) is that you can calculate the size of an array by doing this
sizeof(array) / sizeof(array type)

since we are working with doubles

sizeof(userinput) / sizeof(double)

this makes perfect sense if you know the size of a double is 8 bytes, the size of an array with 5 doubles is 40

40 / 8 = 5

and because, in your original code, you work with a static array, your averageCal function could be as this:

 ``12345678910111213141516`` ``````void averageCal (double a[]) { //initialize variables double addition=0; double average_ans = 0; int size = sizeof(a) / sizeof(double); for (int c = 0; c < size; c++)//for loop to add the numbers {addition = addition + a[c];}//end of for loop average_ans = addition / 5;//dividint the numbers by 5 cout << average_ans << endl; }//end of function ``````

which would require one less parameter for your function
Last edited on
your averageCal function could be as this: [snip] which would require one less parameter for your function

This will not work because in the averageCal function, 'a' is a pointer, and therefore sizeof(a) will always be sizeof(double*).

This kind of confusion is exactly why you should avoid using the sizeof() approach to determine the size of an array.

A better approach is the template approach:

 ``12345`` ``````template int arraysize( const T (&v)[S] ) { return S; }``````

This will work the same for static arrays, but will produce a compiler error if you use it incorrectly (such as with a dynamic array, or a pointer) rather than compiling okay and screwing up at runtime.

 ``123456789101112`` ``````int main() { int foo[6]; cout << arraysize( foo ); // prints '6' as expected cout << sizeof(foo)/sizeof(foo[0]); // also prints 6 int* bar = new int[6]; cout << arraysize( bar ); // Compiler error cout << sizeof(bar)/sizeof(bar[0]); // erroneously prints 1 }``````
Last edited on
Topic archived. No new replies allowed.