(I am a beginner, please don't be mean if I make any serious mistakes.) :sad:
The purpose of this program is to cout the largest and the second largest number among 10 numbers. Although I have thought for serveral times, I still don't get what is the problem.

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    int counter=1;
    int number;
    int max;
    int secondmax;
    int few;//number of times the max. number appears
    int secondfew;//number of times the second max. number appears
    
    cout<<"Enter a number= ";
    cin>>number;
    max=number;
    secondmax=number;    
    while(counter<10)
    {
       cout<<"Enter a number= ";
       cin>>number;
       if(number>max)
       {
          secondmax=max;           
          max=number;
          few=1;
       }
       else if(number==max)
       {
          few++;          
       }

       if(number>secondmax&&secondmax<max)
       {
          secondmax=number;
          secondfew=1;
       }
       else if(number==secondmax&&secondmax<max)
       {
          secondfew++;
       }
       counter++;
    }
    cout<<"There are "<<few<<" person(s) with the maximum number of "<<max<<"."<<endl;
    cout<<"There are "<<secondfew<<" person(s) with the second maximum number of "<<secondmax<<"."<<endl;
    system("pause");
    return 0;
}

If any of you know what is the problem, please give me a hand. Thank you very much.

Here's an idea,

Try creating an array of ints. Then sort them into ascending order.

Then use a pointer to print out the last two elements. but u need 2 check 4 repeats

Job done???

God bless

Thanks, but I forgotton to mention that only the basis are allowed.(Not including the array and pointer). Sorry about my mistake,but thanks for your method.

int largest=0;
int second_largest=0;
for(int i=0; i<10; i++)
{
int num;
cin>>num;
if(num>largest)
{
largest=num;
}
else if(sum condition here)
{
second_largest=num;
}
}

[edit]Actually I can't see a way to do this without reading the whole thing into a buffer[/edit]
The second largest is the problem...

R u sure u can't use arry?

oh dear, brain lock :sad:

Edited 3 Years Ago by happygeek: fixed formatting

Well cant you do it on the fly? Since you know the number of inputs, do something like this.

int FIRST= 0, SECOND= 0; // Variables to store the Largest and Second Largest Numbers
int input ;
for ( int i = 0 ; i < NO_OF_INPUTS ; i++ )
{
         cin >> input;
         if ( input > FIRST)
         {
                    SECOND= FIRST;
                    FIRST= input;
                    continue;
         }
         if ( input > SECOND)
         {
                    SECOND= input;
         }
}

Actually I can't see a way to do this without reading the whole thing into a buffer.The second largest is the problem...

I wonder if anyone can confirm or disprove my suspicion?

thats clever wolf

I am afraid that "fly" technique can not be used either.However, I will give it a try.

am afraid that "fly" technique can not be used either.

huh? How come? No arrays in it. No pointers. Nothing. Only a for loop.

huh? How come? No arrays in it. No pointers. Nothing. Only a for loop.

I thought u had it wolf but now I'm truly stumped. :cry:

Anyway,wolf, I want to ask what does the "continue" do? And what if all the inputs are negative numbers.

what does the "continue" do?

The continue statement skips the rest of an iteration in the for loop and starts the other iteration.

Search for continue and break in your text book. It should have a more detailed explaination.

And what if all the inputs are negative numbers.

My code will not work if all the numbers are negative unless you initialize the values FIRST and SECOND to the largest negative integer allowed by the compiler. And neither wont the code you pasted at the start of the thread. Who wrote that code by the way?

Something like

FIRST = -4294967296; // for 32 bit integers ; i.e -2^32
SECOND = -4294967296;

You cant assign numbers smaller than this so it should work okay after that.

Okay, I wrote the code.
Actually, when you said that "fly" technique. I just thought its just another complicate thing. After I replied, I realized that my technique is almost the same as yours. However, when I want to edit my post, you two had already replied first. That's why my answers seemed to be a bit werid.

Anyway, I am sorry about that. And about your program, I still have a question. I think intializing the "FIRST" and "SECOND" to the smallest number may not be a good idea.
It is because they may vary among different compilers(I think...). Is there an alternate method?

I meant do the calculations on the fly. Not a special technique. Just to mean that nothing is stored other than the results and maybe some temp variables.

It is because they may vary among different compilers(I think...). Is there an alternate method?

Good point. Yeah it will vary. There is an alternate method.
You can use

numeric_limits<int>::min()

which is found in the

<limits>

header.
Or you can use the

INT_MIN

constant found in

<climits>

.

hmm.......I think it is still a bit artificial. But thanks for your method and effort.

Hell... if using that is artificial using std::cout is also artificial. Use the facilities provided by the compiler, and make your life easier.

This article has been dead for over six months. Start a new discussion instead.