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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 #include<iostream>
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:"<<averageCal<<endl;

    return 0;
}


//function for finding the average
void averageCal (double a[])
{
    //initialize variables
double addition=0;
double average_ans = 0;

for (int c = 0; c < 5; 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<<"The average is :"<<average_ans<<endl;

}//end of function

/*
//function for finding the product
double productCal (double p[])
{//initializing variables
    double multiplication= 1;
    double product_ans= 0;

    for (int m = 0; m < 5; m++)//for loop to cycle through the user numbers and multiply them
    {multiplication = multiplication * p[m];}//end of for


    return (multiplication);//return value
}


//function fo rfinding the sum
double sumCal (double s[])
{//variables
    double addition= 0;


    for (int a = 0; a < 5; a++)//for loop
    {addition = addition + s[a];}//end of for

    return (addition);
}  */
Look at the following snippet:
1
2
//this is where i want the program to display the average
        cout<<"The average of the five numbers is:"<<averageCal<<endl;

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;
1
2
//this is where i want the program to display the average
        cout<<"The average of the five numbers is:"<<averageCal<<endl;


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

1
2
//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 :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
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:" <<averageCal(userinputs);

    return 0;
}


//function for finding the average
void averageCal (double a[])
{
    //initialize variables
double addition=0;
double average_ans = 0;

for (int c = 0; c < 5; 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 
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
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  
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <new>

void getAverage(double a[], int size, int *pVar)
{

	int tempHold = 0;

	for (int n=0; n<size; n++) {
		tempHold+=a[n];
	}

	*pVar = tempHold;
}
int main() {

	double * userinput;
	int arraySize=0;
	int average=0;

	cout << "How many numbers would you like to enter? ";
	cin >> arraySize;

	userinput=new (nothrow) double [arraySize];

	if(userinput==0) { cout << "Couldn't allocate memory."; return 1; }

	for(int n=0; n<arraySize; n++) {
		cout << "Enter a number: ";
		cin >> 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
Lucian Adamson wrote:
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:

1
2
3
4
5
template <typename T, int S>
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.


1
2
3
4
5
6
7
8
9
10
11
12
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.