Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,390 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,129 people online right now. Registration is fast and FREE... Join Now!




Sorting Numbers

 
Reply to this topicStart new topic

Sorting Numbers

browngod2002
20 Sep, 2006 - 12:39 AM
Post #1

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
need to compare ten numbers input from a user and find the two highest. i haver to do this without putting the numbers in an array and then using qsort to sort them.
CODE

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num;
    int higher;
    int highest;
    int counter = 0;

while(counter <10)
{
    printf("Enter a number: ");
    scanf{"%d", &num);

    if(num > highest)
       num =higher;
    else
       num = highest;
}

  printf("The two highest numbers are %d and %d\n.", higher,highest);

system("PAUSE");
return 0;
}

User is offlineProfile CardPM
+Quote Post

Delta
RE: Sorting Numbers
20 Sep, 2006 - 10:46 AM
Post #2

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 2


My Contributions
CODE
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    int higher=0;
    int highest=0;
    int counter = 0;

while(counter <10)
{
    printf("Enter a number: ");
    scanf("%d", &num);

    if(num > highest)
       highest = num;
    else if (num >= higher && num < highest)
       higher = num;
    
    counter += 1;
}

  printf("The two highest numbers are %d and %d\n.", higher,highest);

system("PAUSE");
return 0;
}


You were assigning the variables the wrong way. On one of your "scanf" statements you were using a curly brace instead of a parenthasie. I had the If Else statement check to see if the last inputted number was actually higher than the last number assigned to int higher. Without it, if you entered a number that was bigger or smaller than the one already assigned to "higher" it would reassign it to that number instead of see if the previous was bigger or not.

Also, inside your while loop you didnt incriment the counter, so basically int counter would always stay the same number and never test out of the loop, also known as an infinite loop.
User is offlineProfile CardPM
+Quote Post

browngod2002
RE: Sorting Numbers
20 Sep, 2006 - 10:52 AM
Post #3

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
Thank u so much. I know it was a simple code but I'm new at this and i am trying to understand how C works. I appreciate your help.

This post has been edited by Dark_Nexus: 23 Sep, 2006 - 06:17 PM
User is offlineProfile CardPM
+Quote Post

wvtrammell
RE: Sorting Numbers
22 Sep, 2006 - 12:30 PM
Post #4

New D.I.C Head
*

Joined: 24 Jun, 2006
Posts: 4


My Contributions
#
CODE
include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    int higher=0;
    int highest=0;
    int counter = 0;

while(counter <10)
{
    printf("Enter a number: ");
    scanf("%d", &num);

    if(num > higher)
        {
        if(num > highest)
            {
            higher = highest;
            highest = num;
            }
        else
            higher = num;
       }
    
    counter += 1;


  printf("The two highest numbers are %d and %d\n.", higher,highest);
}  
return 0;
}

Try this. It may work better.

QUOTE(browngod2002 @ 20 Sep, 2006 - 01:39 AM) *

need to compare ten numbers input from a user and find the two highest. i haver to do this without putting the numbers in an array and then using qsort to sort them.
CODE

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int num;
    int higher;
    int highest;
    int counter = 0;

while(counter <10)
{
    printf("Enter a number: ");
    scanf{"%d", &num);

    if(num > highest)
       num =higher;
    else
       num = highest;
}

  printf("The two highest numbers are %d and %d\n.", higher,highest);

system("PAUSE");
return 0;
}



This only works if the numbers are in a special order. Try rearranging the order of entry, Largest before next largest. Next largest before largest.
Warren

This post has been edited by Dark_Nexus: 22 Sep, 2006 - 01:36 PM
User is offlineProfile CardPM
+Quote Post

browngod2002
RE: Sorting Numbers
23 Sep, 2006 - 04:53 AM
Post #5

New D.I.C Head
Group Icon

Joined: 16 Sep, 2006
Posts: 27



Thanked: 1 times
Dream Kudos: 275
My Contributions
Thank you that cleared it up for me.

This post has been edited by Dark_Nexus: 23 Sep, 2006 - 06:17 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month