Eleanor
Eleanor

Reputation: 13

I keep getting incorrect output for my second largest number program (C Program)

I'm having trouble getting the correct output in my program for finding the second largest number out of three numbers. (I know it's really basic, but I've just begun learning C, so any help and tips would be appreciated!) In my program, I get the max and the min of the three numbers (no problem there) but I keep getting the answer 204 for the second largest number. (which is wrong!) Here's my code, sorry if there's any awful mistakes, like I said, I'm new to this! Thank you so much! :)

//include the libraries

#include <stdio.h>

#include <conio.h>

//include the main function

int main()
{

    //declare the variables
    long a,b,c,min,max,secmax;

    //read the variables
    printf("a=");scanf("%ld",&a);
    printf("b=");scanf("%ld",&b);
    printf("c=");scanf("%ld",&c);

    // Find the min of the 3 numbers.
    if( b>a && c>a)
    {
        min=a;
    }
    else
    {
        min==b || min==c;
    }

    if( a>b && c>b )
    {
        min=b;
    }
    else
    {
        min==a || min==c;
    }

    if( a>c && b>c)
    {
        min=c;
    }
    else
    {
        min==a || min==b;
    }

    // Find the max of the 3 numbers.

    if( b>a && b>c)
    {
        max=b;
    }
    else
    {
        max==a || max==c;
    }

    if( a>b && a>c )
    {
        max=a;
    }
    else
    {
        max==b || max==c;
    }

    if( c>a && c>a)
    {
        max=c;
    }
    else
    {
        max==a || max==b;
    }

    //Find the second largest number
    if(a!=max && a!=min)
    {
       a=secmax;
    }
    else
    {
        b==secmax || c==secmax;
    }

    if(b!=max && b!=min)
    {
       b=secmax;
    }
    else
    {
        a==secmax || c==secmax;
    }

    if(c!=max && c!=min)
    {
       c=secmax;
    }
    else
    {
        b==secmax || a==secmax;
    }

    //print the output
    printf("\nThe maximum is %d\n",max);
    printf("\nThe second largest number is %d\n",secmax); 
    printf("\nThe minimum is %d\n",min);

getch();

return 1;
}

Upvotes: 1

Views: 191

Answers (3)

nomann
nomann

Reputation: 2267

First thing you need to do is to get rid of all the else checks you currently have. They are just evaluating bool results and doing nothing with them.

Second, when you are finding the second largest number you are assigning secmax into a, b &c but in actuality it should be like this:

if(a!=max && a!=min)
{
   secmax = a;
}

if(b!=max && b!=min)
{
   secmax = b;
}

if(c!=max && c!=min)
{
   secmax = c;
}

Upvotes: 0

DaveWeber
DaveWeber

Reputation: 141

// Find the min of the 3 numbers.
if (b>a && c>a)  
{  
  min = a;  
}  
else if(a>b && c>b)  
{  
  min = b;  
}  
else  
{  
  min = c;  
}

This will properly set the min. You can use similar logic/control flow to set max, and based on the min/max, figure out the middle value.

Upvotes: 0

anatolyg
anatolyg

Reputation: 28241

Assignment works this way:

...
secmax = a;
...

This line of code assigns a to secmax. If secmax didn't contain anything (contained "garbage" in programmer jargon), after executing this line it will be equal to a. This is what you want.


a = secmax;

This line of code is no good - it does the opposite of what you want. It's probably a typo.


There are other problems with your code, but this one seems the most important to fix; there is a chance your code will work after fixing it.

Upvotes: 1

Related Questions