How can we find the largest value?

Sep 29, 2011 at 6:05pm
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
Sep 29, 2011 at 7:05pm
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 Sep 29, 2011 at 7:34pm
Sep 29, 2011 at 7:48pm
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;
}
Sep 29, 2011 at 7:58pm
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);}
Sep 29, 2011 at 8:50pm
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 Sep 29, 2011 at 8:50pm
Sep 29, 2011 at 10:28pm
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;
}
Sep 29, 2011 at 11:35pm
closed account (S6k9GNh0)
Gee, I never knew.
Sep 30, 2011 at 5:52am
Gee, I never knew.
Sep 30, 2011 at 7:38am
Chriscpp, you probably want to move that output statement outside of the for-loop. But other then that, yea...
Sep 30, 2011 at 11:25am
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.
Sep 30, 2011 at 11:32am
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.
Sep 30, 2011 at 12:13pm
Gaminic You are so right. Next time I'll take care. Let them first post their code they have so far.
Sep 30, 2011 at 2:40pm

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 Oct 11, 2011 at 2:24pm
Sep 30, 2011 at 3:14pm
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.
Sep 30, 2011 at 3:26pm
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 Oct 11, 2011 at 2:24pm
Sep 30, 2011 at 3:35pm
Thanz post your code. We all started with "Hello World" ;)
Sep 30, 2011 at 3:40pm
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.
Sep 30, 2011 at 3:59pm
I'm with you Gaminic :)
Sep 30, 2011 at 4:13pm
Thank so much for both of you..I'll follow this rule in future...:)
Topic archived. No new replies allowed.