It is a homework question but!

I am only asking if I have fulfilled the requirements of the homework. If not, I understand. I am simply asking a yes or no response.

The requirement is this:
Write a program that asks the user to input a series of five numbers. Then, average these numbers and output the results. You must have a function called average that is used in main but has a separate average.h file and an average.cpp file associated with it and included into the main program.

What I have is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
#include "average.h"

using namespace std;

int main()
{
int avg = 0;    //this shows a coded average within the main

average(avg);   //this is where the work is pulled from average.h
	
	system("pause");

	return 0;
}


To give the return, I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int average(int avg)
{
	int a, b, c, d, e;
	int sum;
		

	cout << "Please input five numbers for me to average.\n";
	cin >> a;
	cin >> b;
	cin >> c;
	cin >> d;
	cin >> e;
	sum=a+b+c+d+e;
	avg=sum/5;
	cout << "Your average is: "<< avg <<".\n";

	return(avg);
}


Am I missing anything? If yes, just tell me that I am, and I will accept it as such and continue to try. If not, please, tell me that I am fretting over nothing and am capable of submitting as is.

All I ask for is a generic 'yes' or 'no'. Thanks.
Two things:

1.) The instructions ask for "a separate average.h and average.cpp file associated with it". This would mean you need a main file for your driver main() function. A function prototype for average() would go in the average.h, and the function definition would go in the average.cpp.

2.) You may want to double check with your instructor what the average() function should do. The instructions aren't clear on this point - should the function ask for input, and then average it (as you have it), or should it only average some parameters after the user provides input in main?
Last edited on
Fair enough. Both points are valid, xismn. Thank you very much.

Edit: I copied this verbatim off of the instructions page. This isn't clear on how it should be coded. So, I am debating on weather or not to code the inputs from the main(), transferring the inputs over to int average(int avg), and then returning the avg, and submitting that as well. (If that is possible, which I am sure it is.)
Last edited on
also currently you are taking an input of avg =0; into the function, which will then make a copy to work with. so i dont really see a point in taking an input in you current code, because the --avg-- is not changing, waste of a variable, imo.
secondly, generally i would assume, if the input is required for the assignment,
you should probably take the input from the user in main and carry over the values to be averaged. just a thought.
Last edited on
Topic archived. No new replies allowed.