# simple program in c++

So this should be a pretty simple program but I'm not exactly sure how to do two of the five parts. The user enters 4 numbers and then the program tells you:

1) The sum of the numbers
2) The product of the numbers.
3) The maximum number.
4) The minimum number.
5) The average of the numbers.

I can get my program to do all but numbers 3 and 4. Does anyone have any suggestions just to help get me going? Any help would be greatly appreciated.

This is what I have so far:
[code]
#include
using namespace std;

int main()
{
double num_one, num_two, num_three, num_four, sum, product, average;
char ans;

do
{
cout << "Please enter four numbers: " << endl;
cin >> num_one >> num_two >> num_three >> num_four;

cout << "The sum of the numbers is: " << endl;
sum = num_one + num_two + num_three + num_four;
cout << sum << endl;

cout << "The product of the numbers is: " << endl;
product = num_one * num_two * num_three * num_four;
cout << product << endl;

cout << "The average of the numbers is: " << endl;
average = (num_one + num_two + num_three + num_four)/4;
cout << average << endl;

cout << endl;
cout << "Enter Y or y to continue.
";
cin >> ans;
cout << endl;
}
while(ans == 'y' || ans == 'Y');

return 0;
}
[/code]

• There are several ways. You could add a couple more variables and then compare the variables to each input.

I would have approached this project with an array for the four numbers:
[code]
double number[4];
[/code]
Then you could use loops to do all the math:
[code]
for (i=0;i<4;i++)
product *= number[i];
[/code]
Using a temporary variable, let's call it maximum, you can set it to number[0] and then swap it if the there's a larger value:
[code]
maximum = number[0];
for (i=1;i<4;i++)
if (maximum<number[i])
maximum=number[i];
[/code]
To do it your original way just test each case as a separate event and alter the variable accordingly. You should be able to use the same routine for minumum as I did for maximum by simply changing the < for >

Take Care,
Ed
• : So this should be a pretty simple program but I'm not exactly sure
: how to do two of the five parts. The user enters 4 numbers and then
: the program tells you:
:
: 1) The sum of the numbers
: 2) The product of the numbers.
: 3) The maximum number.
: 4) The minimum number.
: 5) The average of the numbers.
:
: I can get my program to do all but numbers 3 and 4. Does anyone
: have any suggestions just to help get me going? Any help would be
: greatly appreciated.
:
Basically you need to get into the habit of writing functions to do 3 and 4 cleanly.

Declare the function

double mymin(double *array, int N)
{
/* code goes here */
}

now your first guess is that array[0] is the minimum. Iterate through the array from i = 1 to i = N-1, and check if you find a number less than your guess. If you do, replace the guess with the new lowest.
On exit from the loop your guess variable will conatin the same value as the lowest, so return it.

• I'm still super confused? How do I use arrays? I have looked it up but I am very sick so it's making every thing a little bit harder.
Is there a formula or do I have to use an array? I just don't know how to take the four numbers that are inputed by the user and return the largest of them?
• Arrays lend a method of having several elements with all the same name, but identified numerically, so that you can use a math formula to address each:

int array[4];

defines an array of integers with a maximum of 4 elements; array[0], array[1], array[2] and array[3]. The 0-3 allow for an easy way to step through the four integers.

However, since you are unfamiliar with arrays at this point, the solution for you would be to do a series of tests on your four numbers against each other to check for the larger (or smaller) of the four. In this case, you would first set the maximum (or minimum) variable to the first of your numbers - num_one. Then you would test this variabe against each of the other three values and if necessary move one of the new values into the maximum (or minimum) variable. After the tests, the variable will hold the value of the maximum (or minimum) of the four numbers.

Take Care,
Ed