Glorpy
Glorpy

Reputation: 83

Finding the highest number in a set of numbers the user enters

I bought the textbook C++ How to program 9th edition and I have come across a question that I am just stumped on, even though it is probably pretty simple. The question all summed up is this: "Use a while statement to determine and print the largest number of 10 numbers entered by the user". But here is the part that stumps me. The question wants me to use only 3 variables. counter, number, and largest. I know to make counter variable go up by 1 for each number entered until it gets to 10, and I know the number variable is used for input. I just can't seem to find out how to use the largest variable, or how to check to see what value is the largest without using other variables. This is all I have so far. Right now I put a break in the code so it wouldn't be an infinite loop.

UPDATED CODE

#include <iostream>

using namespace std;

void main()
{
int counter = 0;
int number = 0;
int largest = 0;

cout << "Please enter up to 10 numbers and I will print the largest one on the screen.\n\n";

while (counter <= 10)
{
    cout << "Number: ";
    cin >> number;
    counter++;

    if (number > largest)
    {
        largest = number;
    }

    else if (counter == 10)
    {
        cout << "The largest number was: " << number;
        break;
    }
}
}

Upvotes: 1

Views: 59820

Answers (4)

Irrational Person
Irrational Person

Reputation: 550

Solution(you don't even need a while loop)

#define MAX_NUM 8
//user input goes in myints
int myints[MAX_NUM] = {3,7,2,5,6,4,9};

// using default comparison:
std::cout << "The largest element is " << *std::max_element(myints,myints+MAX_NUM) << '\n';

Other Solution using int arrays even though you can replace int array with one variable

int main() 
{
    int largest = 0;
    int myints[10] = {3,7,2,5,6,4,9,7,2,6};
    for(int i =0 ; i< 10;i++)
    {
        if (myints[i] > largest)
        {
            largest = myints[i];
        }
    }
    cout << largest << endl;
    return 0;
}

Compiled Code Second Compiled Code

Upvotes: 2

randPow
randPow

Reputation: 86

You just need to add in while loop checking is the number you entered bigger than largest. If it is you just store it in largest. And actually you are entering 11 numbers because you count from 0 to 10. Just set counter to 1 or while(counter < 10)

int counter = 1;
    int number = 0;
    int largest = 0;

    cout << "Please enter up to 10 numbers and I will print the largest one on the screen.\n\n";

    while (counter <= 10)
    {
        cout << "Number: ";
        cin >> number;
        counter++;
        if (largest < number)
        {
            largest = number;
        }

    }

    cout << largest << endl;

Upvotes: 1

Chris Maes
Chris Maes

Reputation: 37732

cout << "The largest number was: " << number;

should be

cout << "The largest number was: " << largest;

Upvotes: 4

Peter Carnesciali
Peter Carnesciali

Reputation: 431

Inside the while loop, if number is greater than largest, then set largest = number.

Then at the end you can output largest.

Upvotes: 3

Related Questions