# C++ - Determining the largest number out of 15 numbers entered

• 03-07-2010
nicalic
C++ - Determining the largest number out of 15 numbers entered
Good Afternoon,
This is my first attempt at writing a C++ program. I recently learned programming in C and it did not seem to be a problem for me. I am feeling pretty stupid with this program. Here is the question:

Create a program to determine the largest number out of 15 numbers entered (numbers entered one at a time). This should be done in a function using this prototype:

double larger (double x, double y);

Make sure you use a for loop expression inside your function.

Enter 15 numbers
11 67 99 2 2 6 8 9 25 67 7 55 27 1 1
The largest number is 99

Hints:

* Read the first number of the data set. Because this is the only number read to this point, you may assume that it is the largest number so far and call it max.
* Read the second number and call it num. Now compare max and num, and store the larger number into max. Now max contains the larger of the first two numbers.
* Read the third number. Compare it with max and store the larger number into max.
* At this point, max contains the largest of the first three numbers. Read the next number, compare it with max, and store the larger into max.

Repeat this process for each remaining number in the data set using a for expression.

Here is my code:

Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int max; int num; for (int counter = 0; counter<15; counter++) { cout <<"Enter a number: "; cin >> max; cout << "Enter another number: "; cin >> num; if (num > max) num = max; cout << "The largest number is " <<max; } return 0; }```
As you can see, I have the largest number line printing out after every user input number. I know it's probably something wrong with my for loop or my braces, but I can not figure it out for the life of me. Any input will be appreciated. Thank you.
• 03-07-2010
EVOEx
Code:

```if (num > max) num = max;```
The bug is in there, see if you can find it.
• 03-07-2010
anon
I think there are other problems besides that, such as having user enter the max value 15 times. You'd rather need to keep a running max.
• 03-07-2010
QuestionKing
Quote:

Create a program ... This should be done in a function using this prototype: ...
You do not yet have a function.
Quote:

Make sure you use a for loop expression inside your function.
Looks like you did well on the loop so far...
Quote:

* Read the first number ... this is the only number read to this point, ... it is the largest number so far and call it max.
This hint is more of a reminder as to what is happening, rather than an instruction to follow. If you think about it, what you do inside your loop for every step, is capable of handling your first cycle through the loop. (you don't need to assign to max inside your loop on the first cycle, and then cin num, that actually causes your loop to cin 30 numbers in 15 cycles) Your cout is also going to repeat, as you noted, because it is within the body of the loop. You want to find the largest number before outputting, so the loop must complete all cycles before the cout can be reached.
Quote:

* Read the second number and call it num. Now compare max and num, and store the larger number into max. Now max contains the larger of the first two numbers.
Looks like what you did for this part is the right idea, but has a problem. Consider the rules of the assignment operator.

using std::endl; .... you never used endl at all
```#include <iostream> using namespace std; int main()         {         int max = 0, tempmax = 0, i =0;         int        num, num1;                 while (i<7)         {                 cout << "enter two numbers" << endl;                 cin >> num >> num1;                                 if (num > num1)                         tempmax = num;                 else                         tempmax = num1;                         if (tempmax > max)                         max = tempmax;                 i++;         }                 cout << "The largest number is " << max << endl;         return 0; }```