Help Finding The Max value

Hi everyone, I want to know how we can make the program print the the max value from several data that user have inputed, for example I have the user input 5 integer :
1. 15
2. 35
3. 66
4. 13
5. 41
And the program should print 66 because it's the max value(or the biggest value).
Actually I have solve this problem with my code, here's the code:

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
30
#include <iostream>

int main ()
{


    std::cout<<"Enter 5 number: " << std::endl;

    int number[5];

        // Store data
        for (int i=0; i<5; i++)
        {
           std::cout<< i+1 << ". " << ": ";
           std::cin >> number[i];
        }
        int Max=0;

        // Search the max number
        for (int j=0; j<5; j++)
        {
        if (number[Max] < number[j])
            Max=j;
        }

            std::cout << "The max number is: " << number[Max]
                      << std::endl;

return 0;
}


But my friend teasing me and told me like this, you can do that because you know(or you tell the user) to input the data 5 times, what if you didn't know how many data that must be stored by the user ?

Then she told me about using a function and something aboutva_list, va_arg and also about using stdarg.h header
But seriously I don't know anything about this, and I hope someone willing to help me, I don't need the source code(but if you kind enough thanks ^_^), I just need a little explanation about this, probably you can direct me to a link with a discussion about this function.
Regardless anything, thank you very much, and I'm sorry if I make a bad question or something like that, because I'm a beginner.. Peace all.

P.S: I'm sorry but if you're going to tell me about asking my friend that told me about these functions, that's I can't do because she won't tell me, that's why I'm here now -_-
Your method stores the index of the element of the array that contains the largest value.

Her method stores the largest value and replaces it if a new element is larger..
Thanks for the explanation, although I still did not use my friend's advice, I have managed to resolve this problem. The program will print the max value from several integer data that I myself(the programmer) don't ever know how many integer involved in this 'process of finding the largest value (at least I know that it's between 1 -15 data because I set it like that, see line 9th),
Basically I make the computer to decide how many integer data will be involved in this 'process of finding the largest value ', and then for the value of each data I also make the computer to generate it randomly too.
Here's my code:

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
30
31
32
33
34
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstdlib>

int main()
{ srand((unsigned)time(NULL));
    int input;
    input = rand() % 50 + 1; // Generate random number from 1 - 50, store it in variable input
    int number[input]; // declare number, an array type variable with random size
                       // (the size obtained from variable input)


    for( int i=0; i<input; i++) {
    number[i] = rand() % 100+1; // store random value into array number

    // This 'if' used to make the output look neater,
    if (i!=10)
    std::cout << std::setw(2)     // Add a space at the left boundaries from number 1 - 10
              << i+1 << ". " << number[i] << std::endl;
    else if ( i >= 10 )           // For number > 10 the space is eliminated
    std::cout << i+1 << ". " << number[i] << std::endl;
    }

    // Find the largest value or max value
    int Max=0;
    for (int j = 0; j<input; j++) {
        if (number[Max] < number[j])
            Max = j;
    }

    std::cout << "Max value is: " << number[Max] << " at number: " << Max + 1;
return 0;
}


I'm sorry if my code is to long or not elegant, but at least this is that I can do..
thank you
Last edited on
Topic archived. No new replies allowed.