Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've been struggling with this for a while now because I'm a newbie with C project.


I written this code to find Max and Min values and can't figure out why it displays Max=1 whichever values I put in.

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int m,n,a,b,Min,Max;
printf("m=");
scanf("%d",&m);
printf("n=");
scanf("%d",&n);
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
if(m>n&&n>8)
{
	{
		if(a>b)
		Max=a;
		else
		Max=b;	
	}
	
}
else
{
	
	{
		if(a<b)
		Min=a;
		else
		Min=b;
		
	}
}

printf("Max=%d\n",Max);
printf("Min=%d",Min);
	return 0;
}


What I have tried:

I have tried fiddling with these brackets {} but it seems I'm dumb.
Posted
Updated 6-Dec-21 20:17pm

You have some extra braces there. It should look more like this :
C++
int main(int argc, char *argv[])
{
    int m,n,a,b,
    int Min = 0;   // added initialization
    int Max = 0;   // added initialization
    printf("m=");
    scanf("%d",&m);
    printf("n=");
    scanf("%d",&n);
    printf("a=");
    scanf("%d",&a);
    printf("b=");
    scanf("%d",&b);

    if( ( m > n ) && ( n > 8 ) )    // added parens to be explicit
    {
       if(a>b)
          Max=a;
       else
          Max=b;	
    }
    else
    {
       if(a<b)
          Min=a;
       else
          Min=b;
    }
    
    printf("Max=%d\n",Max);
    printf("Min=%d\n",Min);
    return 0;
}
Note that I added a few things. Remember that unless your first if condition is true Max will not be assigned a value. Similarly, if the condition is false then Min will not be assigned a value so I added initializers for those.
 
Share this answer
 
In your outer if/else blocks, you only assign a value to either Max, when the condition m>n && n>8 is true or to Min otherwise. In order to assign a value to both Max and Min you will have to assign both in the same block e.g.
C
if ( m > n && n > 8) /* 8? really? why 8? */
{
    if( a > b)
    {
        Min = b;
        Max = a;
    }
    else
    {
       Min = a;
       Max = b;
    }
}
else
{
   /* omitted, details to be inserted by original author */
}
You'll probably want to revisit your code and decide how m and n are supposed to affect the outcome of Min and Max. Its not obvious to me what the intent is of those variable in this code sample.
 
Share this answer
 
Pay much attention to all brackets, because false use means BUGS on strange places, so you wont see sometimes the problem directly.

Rule of thumb is: Write ALL brackets you can write, especially when in doubt. so I recommend Rick Yorks solution. Always remember the compiler hasnt any problem with too much brackets, so correct code is best at all times. Optimizing is only in need when speed is slow.

Dont be confused, but learn to write code slow and precise. If you are a newbie than visit some Learn C tutorial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900