Multiple files - 2nd parameter advice

One little thing: it seems "size" is not the proper parameter to use as the 2nd parameter in Main.cpp as it's a formal parameter in funcAverage.cpp. I didn't think it would work in Main.cpp since it's not an actual parameter during the calcAverage call.

However, it does compiles completely without errors and let's me enter the 5 numbers, but when I hit "enter", the console closes without reaching (or performing) the funcAverage call. I'm just learning about arrays and multiple file construction so my logic is still a bit fuzzy.

So, my question is what constitutes the parameters in the function call in Main? I thought it would be the parameters in the funcAvg.cpp but it doesn't seem to be the case. What am I doing wrong?

average.h
1
2
3
4
5
6
7
8
9
10
/*=========================*/
//  Function Declaration   //
/*=========================*/


#include<iostream>

using namespace std;

float calcAverage (int numberInputs[], const int size);


funcAvg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*==========================*/
//   Function Definition    //
/*==========================*/


#include "average.h"


float calcAverage(int numberInputs[], const int size) 
{
	int sum = 0;
	int i = 0;
	float average = sum / size;  //to find average

	for (int i = 0; i < size; ++i)
	{
		sum += numberInputs[i]; //calculation to get the total sum of numbers from user
	}
	return average;
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "average.h"
                       
using namespace std;      
                    
int main()
{
int numberInputs [5];
int userInput;
int size = 5;     // not sure if this is necessary. Takes place of const int size in average.h??

cout <<"This program will average a group of 5 numbers of your choice." <<endl;
cout <<"\n\nPlease enter 5 numbers: " <<" ";
cin >> userInput;
cout <<"\nThe average of the numbers you entered is: ";
cout << calcAverage(numberInputs, size) <<endl;  /*------not sure what to put as 2nd parameter--------*/
				
cin.get();
cin.get();
return 0;
}



Any advice or direction would be greatly appreciated as this is due today.




1
2
3
4
5
6
7
8
9
10
11
12
float calcAverage(int numberInputs[], const int size) 
{
	int sum = 0;
	int i = 0;
	float average = sum / size;  //to find average

	for (int i = 0; i < size; ++i)
	{
		sum += numberInputs[i]; //calculation to get the total sum of numbers from user
	}
	return average;
}

Here you calculate average before you're calculating sum of numbers. So you get 0.
You shall calculate average after calculating sum of numbers.
Thanks Dufresne,

That was a dumb mistake on my part and I should've known better, lol. I DID make it calculate the sum before calculating the average now. However, the initial problem still remains. I just don't think my parameters are working in the Main.cpp for the calcAverage function call because the console still closes AFTER I enter the 5 numbers and hit ENTER.
Well, I guess I got the 2nd parameter error to stop complaining but now it says, "the variable average is being used without being initialized." On a better note, I got it to read the next cout statement that says, cout << "The average of the numbers you entered is: "; but that's when I get the "average not being intialized" error. I declared it in funcAvg.cpp but how do I initialize it? I cant intiailze it to 0 or any number. It wouldn't make sense to me.

Can somebody explain why I'm getting this error?

Here is my latest funcAvg.cpp (both average.h & Main.cpp remain unchanged).


funcAvg.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*==========================*/
//   Function Definition    //
/*==========================*/


#include "average.h"


float calcAverage(int numberInputs[], const int size) 
{
	int sum = 0;
	int i = 0;
	float average;
	
	for (int i = 0; i < size; ++i)
	{
		sum += numberInputs[i]; //calculation to get the total sum of numbers from user
		float average = sum / size;  //to find average
	}
	
	return average;	
}


Where is my logic amiss?




You shall calculate average outside of loop. And you shoudn't make another decleration in the loop , because when you do , compiler acts like they're different variables.It's a scope problem.
So your code shall be
1
2
3
4
5
6
7
8
9
float calcAverage(int numberInputs[], const int size) 
{
	int sum = 0;	
	for (int i = 0; i < size; ++i)
		sum += numberInputs[i]; //calculation to get the total sum of numbers from user
     
        float  average = (float)sum/size;
	return  average ;	
}

Or you can also do
1
2
3
4
5
6
7
8
float calcAverage(int numberInputs[], const int size) 
{
	int sum = 0;	
	for (int i = 0; i < size; ++i)
		sum += numberInputs[i]; //calculation to get the total sum of numbers from user
     
	return  (float)sum/size;	
}

Here, you shall be careful about casting. (float)sum/size If you don't cast result of division to float , you may get wrong result.Because result of int/int is integer.
Last edited on
Topic archived. No new replies allowed.