Thread: New to C programming and need help.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    New to C programming and need help.

    Ok so I am trying to write a basic program that will take a set of numbers, and determine the 2 largest numbers in the set. The stipulation is that I can use a while loop and some if statements. Oh and all the numbers have to be different (cannot have 2 equal numbers).

    I came up with the following code:
    Code:
    int counter, num=0, largest=0, ndlargest=0, pnum=0;
    
    while (counter != 10)
    counter++;
    printf("Please enter a number (cannot be equal to any other number):");
    scanf("%d", &num);
    
    if (num>largest){    \*this takes care of the largest number*\
        largest = num;
    else
      if (num>ndlargest)  \*this takes care of the 2nd largest number but only if descending*\
        ndlargest = num;}
    if (largest>pnum)   \*should test the values for all cases other than the previous if*\
        ndlargest=pnum;
    pnum = largest;
    Am I doing something wrong? Because when I consider the logic I think I have all the possibilities covered. However, when I compile and use 1,2,3 (counter != 3, for faster testing) I get 3 for largest value and 1 for 2nd largest. When I run the program with 3,2,1 as the input, I get the correct results. When I use 3,1,2, I get 3 as largest and 1 as 2nd largest.

    Any help would be appreciated

    Oh and I forgot to mention I use DMC as my compiler in DOS on a WindowsXP platform.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're missing braces after the while. Thusfar the while will run 10 times but the code after counter++; runs once.

    Also do you include negative numbers in "what's the largest"? If so then your logic is broken.
    Last edited by zacs7; 02-04-2009 at 04:42 AM.

  3. #3
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    1. you used var "counter" without being initialized. you sould initialized it before usuing it in the loop.
    2.
    Code:
    while (counter != 10)
    counter++;
    This does nothing just increase the counter, witch however is not initialized. I recomend you to use "for" instead of "while" and don't forget the "{}".

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    here's some more info about why stuff is recommended.

    Quote Originally Posted by ralu. View Post
    1. you used var "counter" without being initialized. you sould initialized it before usuing it in the loop.
    if you don't set a value the variable has whatever value is at that address. it's probably not going to be 10, so not initializing will probably work, but it's safer to set the value before using it.
    Quote Originally Posted by ralu. View Post
    2.
    Code:
    while (counter != 10)
    counter++;
    This does nothing just increase the counter, witch however is not initialized. I recomend you to use "for" instead of "while" and don't forget the [curly braces].
    a for loop is better because it's a counted loop and that's what the code needs. a while loop works too but most programmers don't read a while as a counted loop. you'd confuse your peers.

    curly braces are only needed for a block with more than one statement but if you don't know whether to use them or not, it's safer to use them.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    I apologize, I actually had the counter set to 0 in my code. I had to retype it from memory because the MWR (morale, welfare, recreation) at this base does not allow us to bring our own computers. However, as I was coming up with the code from memory I made the solution...When I got back I realized I had
    Code:
    pnum=num;
    as the last statement so therefore the value was being replaced with a smaller digit and therefore giving me incorrect readings.

    You're missing braces after the while. Thusfar the while will run 10 times but the code after counter++; runs once.

    Also do you include negative numbers in "what's the largest"? If so then your logic is broken.
    I did have brackets in the while loop I just forgot to add them here.

    P.S. the code works fine now. But Zacs why is the logic wrong when I use negatives?


    This is the revised code:
    Code:
    int counter=0, num=0, largest=0, ndlargest=0, pnum=0;
    
    while (counter != 10){
    counter++;
    printf("Please enter a number (cannot be equal to any other number):");
    scanf("%d", &num);
    
    if (num>largest){    \*this takes care of the largest number*\
        largest = num;
    else
      if (num>ndlargest)  \*this takes care of the 2nd largest number but only if descending*\
        ndlargest = num;}
    if (largest>pnum)   \*should test the values for all cases other than the previous if*\
        ndlargest=pnum;
    pnum = largest;}

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this code will not compile - your usage of braces is horrible

    Code:
    while (counter != 10)
    {
    	counter++;
    	printf("Please enter a number (cannot be equal to any other number):");
    	scanf("%d", &num);
    
    	if (num>largest)
    	{    \*this takes care of the largest number*\
    		largest = num;
    		else
    			if (num>ndlargest)  \*this takes care of the 2nd largest number but only if descending*\
    				ndlargest = num;
    	}
    	if (largest>pnum)   \*should test the values for all cases other than the previous if*\
    		ndlargest=pnum;
    	pnum = largest;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > But Zacs why is the logic wrong when I use negatives?
    Largest and second largest start off at 0. Given that and you enter -1, -5, -6. What do you think your program will say the second and largest are? Remember that 0 > -1.

Popular pages Recent additions subscribe to a feed