How can we find the largest value?

How can we find the largest value in C++?

like if a user enters 100 numbers how I'll find which one is the largest???

plz rep soon ...as its urgent
closed account (S6k9GNh0)
This sounds like a homework assignment of the easiest kind, I'll answer in abstract details.

Create an array of integral types(or std::vector if unsure of the size at compile-time) the size of the number of numbers to hold.
Create a integral type variable that's initialized to the lowest possible value. We'll call this value "temp".
Fill the array with the data that you need.
Iterate through the array. For each element, compare it to "temp".
If "temp" is lower than the element of the array, assign temp that specific element of the array.

There you go.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    int array[5] = {5,123,5,9,1};
    int temp = 0;

    for(int i=0;i<5;i++)
    {
        if(array[i]>temp)
        temp=array[i];
    }
    cout << "The biggest number is: " << temp << endl;
    return 0;
}
Chrisscpp! You're breaking the code! Here have a better version!
1
2
#include <iostream>
int m(int a,int b){return((a>b)?(a):(b));}int main(){int max;int arr[]={1,2,3};max=m(m(arr[1],arr[2]),m(arr[2],arr[3]));std::cout<<max<<std::endl;return(0);}
computerquip, if all he's doing is reading input and calculating the highest number entered, you don't need to store every entree.

Just check if each entered amount is higher then the currant highest, if it is replace the current highest; if not, throw it away (i.e. do nothing.)
Last edited on
Mathhead200 you mean like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int x=0;
    int y=0;

    for(int i=0;i<100;i++)
    {
        cin >> x;
        if(x>y)
        y=x;
        cout << "The biggest number is: " << y << endl;
    }
    return 0;
}
closed account (S6k9GNh0)
Gee, I never knew.
Gee, I never knew.
Chriscpp, you probably want to move that output statement outside of the for-loop. But other then that, yea...
Mathhead200 well, that's another version of this game but we don't know what Thanz wants to do. He wrote it's urgent but never replied.
Little point in printing 100 times what the current highest number is.

Also, it's best not to provide example code in topics like this. Clearly, he's too lazy to make his own homework. Programming assignments don't get any easier than this, thus if he can't manage, it should be clear as soon as possible.
Gaminic You are so right. Next time I'll take care. Let them first post their code they have so far.

Thanks Chrisscpp, Mathhead200 n to all of you to reply...

Gaminic First of all I'm new n had no idea about the quick replies here. I posted this question last night now I've got a chance to check is someone replied or not?? n happy to see such active ppl here.

I'm quite new in this world n started learning C++ from few days back..how can i write n share my codes here???


Last edited on
In all fairness, "I'm quite new n [sic] started learning C++" doesn't come over well when you apparently did no effort to solve the problem yourself. Next time, post your code so-far (in code tags, check the "format" table when posting) and specify what's wrong. We'll gladly give you some pointers to help you.

Also, I hope for your sake chipp doesn't read this topic.
m really surprised to see ua attitude with new comers.

N.e ways I tried to solve it by myself as well but I was hesitating to share my codes because here ppl're much expert ..I was thinking mine were wrong so called u ppl for help..
Last edited on
Thanz post your code. We all started with "Hello World" ;)
Don't worry, we don't judge. Well, I do, but not based on code.

We (the people who log on to this forum in our spare time and answer questions to the best of our ability) are here to help you learn, not to provide the answer to your assignment. We want to see your mistakes, so we can tell you what you did wrong and explain why it doesn't work, so you don't make the same mistake again.

We get enough bastards in here who simply want us to do their homework (and then often edit away the question...), so forgive us (well, just me, apparently) about getting cranky when seeing topics like this.
I'm with you Gaminic :)
Thank so much for both of you..I'll follow this rule in future...:)
Topic archived. No new replies allowed.