1

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; } } } 
2
  • I suggest you check up on if statements. Commented Jan 14, 2015 at 6:43
  • 1
    Insert this line after cin >> statement. largest = number > largest ? number : largest; Commented Jan 14, 2015 at 6:58

4 Answers 4

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

should be

cout << "The largest number was: " << largest; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Chris. It was my own stupidity that got the best of me. I should not program when my brain is half asleep haha. Thank you for your help
you're most welcome. Better double test before posting on stackoverflow though. I always use lots of printf or cout for debugging :)
3

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

Then at the end you can output largest.

6 Comments

Hmm. That actually makes a lot of sense. Though for some reason when I go through it all, it outputs a number that is not the largest one. For example when I was testing it, it said 93 was larger than 103
@Glorpy If that's your problem then put that code in the question; we can't debug code we can't see.
@cdhowie Right, sorry my bad.
Oh God I am an idiot. I am sorry for wasting your time, one sec let me change number to largest. Why do I program at 2 AM haha
Yep, that did it. Thank you very much for your help @Peter Carnesciali
|
2

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

Comments

1

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.