| jimfrick (2) | |
|
I am a brand new student to programming, and am trying to figure out how to do the following: Read X integers from the keybboard (set Maximum Number of inputs at 500) Provide a method to end the input before reaching the maximum of 500. Ask the user if the program should: a - calculate the average of the numbers input OR b - calculate the sum of the numbers input Call a function for whichever calculation is selected I have some code that sort of works for the sum (haven't even gotten to the average yet) as I can't get the sum part to work correctly: Where am I going wrong??? #include "stdafx.h" #include <iostream> using namespace std; int SumFx (int Input[ ], int ArraySize); int main () { int X, Y = 0; int ArraySize = 5; int Input[5]; cout << "Enter up to 5 numbers." << endl; cout << "Use ZERO to break and continue: " << endl << endl; while (X >= 1) { cout << "Enter your numbers: "; cin >> X; Input[Y] = X; SumFx(Input, ArraySize); Y++; } cout << "The SUM is: " << SumFx(Input, ArraySize) << endl; for(X = 0; X < 5; X++) { cout << Input[5] << " "; } cout << endl; system("PAUSE"); return 0; } int SumFx (int Input[ ], int ArraySize) { int Sum, Y; for(Y = 0; Y < ArraySize; Y++) { Sum = Sum + Input[Y]; } return Sum; } | |
|
|
|
| Moschops (5961) | |
| Don't forget to set Sum to zero before you start the calculation. | |
|
|
|
| jimfrick (2) | |
|
That doesn't help. It won't even pass through the compiler.... #include "stdafx.h" #include <iostream> using namespace std; int SumFx (int Input[ ], int SizeOfArray); double AvgFx (int Input[ ], int SizeOfArray); int main () { int X, Y = 0; int ArraySize = 5; int Input[5]; int Select = 0; cout << "Enter up to 5 numbers." << endl; cout << "Use ZERO to break and continue: " << endl << endl; while (X >= 1) { cout << "Enter your numbers: "; cin >> X; Input[Y] = X; SumFx(Input, ArraySize); Y++; } cout << endl << endl; cout << "If you want to calculate THE SUM of your numbers," << endl; cout << "Input 2 and press ENTER." << endl << endl; cout << "Otherwise, to calculate THE AVERAGE of your numbers," << endl; cout << "Input 3 and press ENTER." << endl << endl; cout << "Input your selection here: "; cin >> Select; cout << endl; while (Select < 2 || Select > 3) { cout << "Inpute must be 2 or 3." << endl; cout << "Please re-enter: "; cin >> Select; } if (Select == 2) cout << "The SUM is: " << SumFx(Input, ArraySize) << endl; for(X = 0; X < 5; X++) { cout << Input[X] << " "; } cout << endl; system("PAUSE"); return 0; } int SumFx (int Input[ ], int SizeOfArray) { int Sum = 0, Y; for(Y = 0; Y < SizeOfArray; Y++) { Sum = Sum + Input[Y]; } return Sum; } double AvgFx (int Input[ ], int SizeOfArray) { int Sum, Y, Avg = 0, Counter = 1; for(Y = 0; Y < SizeOfArray; Y++) { Sum = Sum + Input[Y]; Counter++; Avg = Sum / Counter; } return Avg: } | |
|
|
|
| Moschops (5961) | |||
The reason your code above doesn't get through the compiler is this function you added on the end:
If your compiler did not give you an error message that pointed you at that final line, return Avg:, which should be return Avg;, throw it away and get a helpful compiler.Also, in this function, you are calculating Sum but you did not set it to zero at the start, so it will be some random value. I see that you did fix this in the function SumFx; you will have to set the variable Sum to zero at the start every time you create it inside a function. Doing it in just one function isn't enough.
| |||
|
Last edited on
|
|||