find the largest number of user input

First we ask the user how many numbers he would like to input then he enters the numbers, then we find the largest number and output it. My main problem is writing a code that finds the largest number given we don't know how many numbers he will input. My knowledge only goes as far as understanding loops and functions, beyond that I am incapable, so keep that in mind. Thanks for any help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int main ()
{
   int num;
cout << "How many numbers would you like to enter?" << endl;
   cin >> num;


//Now that we know how many numbers he would like to enter we ask him to enter his numbers

cout << "You would like to enter" << num << "numbers" <<
   cout << "Please enter your numbers:" <<

//Now he could enter a large amount of numbers here. How can we write code to find the largest number knowing this number could be very large and without knowing the number ahead of time. An if statement doesn't really work since it could take hours to write depending on how many numbers he inputs. Also, we could only do an if statement after we know the numbers the user input, and we dont have that knowledge ahead of time


you could just create an array larger than you think you need. For example, it is unlikely that a person will input 100 numbers manually. Then you can use the size the user inputs in a for loop, to cycle through the array.
yea, but I have not learned that yet, so keep that in mind
So ask the user to enter how many numbers he would like to enter. Then use a for loop like this
for(int i = 0; i < size; i++) with an if statement inside it, like this
1
2
if(num > largets)
largest = num;

>you could just create an array larger than you think you need.

Or use dynamically allocated arrays or vectors

Banshee, the easiest way to do this will be to use a container such as a vector to hold the values because a vector does not need to know the number of input it gets.

Even easier will be to not use a container and evaluate the numbers at run time then print the largest number found
Last edited on
Smac89 wrote:
Or use dynamically allocated arrays or vectors

Yes but the op states that he only knows functions and loops so its unlikely he/she could do that.

Smac89 wrote:
Banshee, the easiest way to do this will be to use a container such as a vector to hold the values because a vector does not need to know the number of input it gets

Again yes, but if the op dosn't know how to use arrays. Like he/she states above than he/she is not going to know how to use vectors
1. input into int array use a loop to check which is the largest number.
2. input into vector use a loop to check which is the largest number.
3. input into a list use a loop to check which is the largest number.

there was just a forum topic on this yesterday I forgot where exactly but it was april 20 around 8pm I believe.
closed account (D80DSL3A)
If all that you want is to find the greatest value entered then no array or other container is needed. You need a container if all input must be stored for sorting (or other use), but not to simply identify the maximum value.
This task can be done as the data is entered.

I think this is Smac and Yansons point also. The solution proposed by giblet is unnecessarily complex for the given task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main ()
{
   int num;
    cout << "How many numbers would you like to enter?" << endl;
   cin >> num;


//Now that we know how many numbers he would like to enter we ask him to enter his numbers

    cout << "You would like to enter" << num << "numbers\n";
   cout << "Please enter your numbers:\n";

    int highNum;
    cin >> highNum;// save 1st value as highest
    
    int inNum;// for reading the rest of the values
    for(int i = 2; i <= num; ++i )
    {
        cin >> inNum;// read input
        if( inNum > highNum )// compare to highest yet
            highNum = inNum;// replace highest
    }

    cout << "The highest number entered was: " << highNum;
    return 0;
}
Last edited on
My main problem is writing a code that finds the largest number given we don't know how many numbers he will input.


This is not complete but may help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main(){

int
        value=0,
        largestNumber=0;

cout<<"Enter number to find the largest(or 0 to exit): ";
cin>>value;

while(value!=0){
if(value>largestNumber)
        largestNumber=value;
cout<<"Enter number to find the largest(or 0 to quit): ";
cin>>value;
}//end while

cout<<"Largest number is: "<<largestNumber<<'\n'<<endl;

//type your favorite pause statement HERE i.e "cin.ignore();" or whatever you want

return 0; //indicate successful termination
}//end main



Enter number to find the largest(or 0 to exit): -1
Enter number to find the largest(or 0 to quit): 5
Enter number to find the largest(or 0 to quit): 3
Enter number to find the largest(or 0 to quit): 2
Enter number to find the largest(or 0 to quit): 7
Enter number to find the largest(or 0 to quit): 8
Enter number to find the largest(or 0 to quit): -12
Enter number to find the largest(or 0 to quit): 77
Enter number to find the largest(or 0 to quit): 0
Largest number is: 77

Enter number to find the largest(or 0 to exit): -1
Enter number to find the largest(or 0 to quit): -2
Enter number to find the largest(or 0 to quit): -3
Enter number to find the largest(or 0 to quit): -4
Enter number to find the largest(or 0 to quit): 0
Largest number is: 0
Last edited on
Topic archived. No new replies allowed.