• Log in or sign up for an account here: https://www.sythe.org/login

helps

PsychicFreak

Member
Joined
Jun 6, 2008
Sythe Gold
0
Messages
53
helps
Create a program that will ask a user to input 3 numbers. Use the if-else statement to get the MINIMUM and MAXIMUM of the given numbers.
Sample Output:

Enter first number: 65
Enter second number: 45
Enter third number: 20

The minimum is 20 and the maximum is 65.
 
helps
Does this have to be in c or c++?
 
helps
i have the codes here blind

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

int main()
{
int a, b, c;

printf("Enter first number: ");
scanf("%d",&a);

printf("Enter second number: ");
scanf("%d",&b);

printf("Enter third number: ");
scanf("%d",&c);

if (a>b&&c)
{
printf("\n The maximum number is:%d",a);
}
if (b>a&&c)
{ printf("\n The maximum number is:%d",b);
}
if (c>a&&b)
{
printf("\n The maximum number is:%d",c);
}


else if (a<b&&c)
{
printf("\n The minimum number is:%d",a);
}
else if (b<a&&c)
{ printf("\n The minimum number is:%d",b);
}
else if (c<a&&b)
{
printf("\n The minimum number is:%d",c);
}






getch ();
return 0;
}





the result is maximum maximum how can i change the other one into minimum?
 
helps
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
int a, b, c;

printf("Enter first number: ");
scanf("%d",&a);

printf("Enter second number: ");
scanf("%d",&b);

printf("Enter third number: ");
scanf("%d",&c);

if (a>b && a > c)
{
printf("\n The maximum number is:%d",a);
}
else if (b > a && b > c)
{ printf("\n The maximum number is:%d",b);
}
else if (c > a && c > b)
{ 
printf("\n The maximum number is:%d",c);
}

if (a < b && a < c)
{
printf("\n The minimum number is:%d",a);
}
else if (b < a && b < c)
{ printf("\n The minimum number is:%d",b);
}
else if ( c < a && c < b)
{ 
printf("\n The minimum number is:%d",c);
}

Your conditions were wrong. You had, if (c<a&&b). Basically, if c is less than a and b is true (in this case, if b is anything but 0). What you needed was, if (c < a && c < b).

Another thing was your if/else if structure. You had:

if()..
if()..
if()..
else if()..
else if()..
else if()..


What I highlighted in red, is what I guess I could say is, grouped. The first three if statements would be checked, regardless of whether the previous was true or not. However, with the third if statement, and the following else if statements, if any of those evaluated to be true, it would go to the end of the group of if statements.

Sorry if the explanation isn't too great. Let me know how it works out for you.
 
helps
ohh i see tnx blind :)) i got it hehehe ty bro :D
 
helps
The best way to do this is to actually SORT the numbers, not just PRINT them in sorted order.

Basically, for a program like this, you want to use the bubble sort technique (http://en.wikipedia.org/wiki/Bubble_sort), except that you only have 3 variables to sort.

Bubble sort basically works by comparing adjacent values and swapping them if they are out of order. You start from the first two and when you get to the last two you start over again with the first two.

Example...

you start with:
4 7 3

First pass:
compare first two, 4 and 7
they are in order so you don't swap
you end with
4 7 3

compare second and third numbers, 7 and 3
they are out of order so you swap
you end with
4 3 7

Second pass:
compare first two, 4 and 3
they are out of order so you swap
you end with
3 4 7

After one "pass" you swap enough times such that the last number in the series is in the correct spot. The first pass you make two comparisons so 7 is in the correct spot at the end. The second pass you make one comparison (since 7 is already correct) so that 4 is in the correct spot. if 4 is in the correct spot then so is 3. Every pass you make one less comparison than the pass before because one more number ends up in the correct spot.

you make sum(n-1) comparisons to sort numbers using bubble sort

3 numbers - 3 comparisons
4 numbers - 6 comparisons
5 numbers - 10 comparisons
6 numbers - 15 comparisons

etc.

if you still don't understand see http://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example

i hope you understand the basic concept of bubble sort such that later you can use loops to sort an undetermined amount of numbers

ill go ahead and give you some sample code so you can see its implementation better

Code:
int firstNum, secondNum, thirdNum;
cout << "Enter first number: ";
cin >> firstNum;

cout << "Enter second number: ";
cin >> secondNum;

cout << "Enter third number: ";
cin >> thirdNum;

int temp;

if (firstNum > secondNum)
{
     temp = firstNum;
     firstNum = secondNum; // these three lines swap firstNum and secondNum
     secondNum = temp;
}

if (secondNum > thirdNum)
{
     temp = secondNum;
     secondNum = thirdNum;
     thirdNum = temp;
}

if (firstNum > secondNum)
{
     temp = firstNum;
     firstNum = secondNum;
     secondNum = temp;
}

cout << firstNum << " " << secondNum << " " << thirdNum; // to print all numbers sorted

cout << firstNum << " " << thirdNum; // to print min then max
 
helps
If you're going to bubble sort than make a function and pass in an array of values, which are looped through. Sorting like that basically defeats the purpose of a bubble sort.
 
helps
Well I didn't say I was using bubble sort exactly, rather I'm borrowing the style from bubble sort. There's no reason to use arrays and loops since there are only 3 variables and I'm assuming he doesn't know arrays or loops yet.

I just think its more useful to use a bubble-sort-like technique because you actually sort the numbers, rather than print them sorted. Also, it uses less if's than just printing them sorted. IMO it's a more elegant way of solving the problem but that's just me...
 
helps
Well I didn't say I was using bubble sort exactly, rather I'm borrowing the style from bubble sort. There's no reason to use arrays and loops since there are only 3 variables and I'm assuming he doesn't know arrays or loops yet.

I just think its more useful to use a bubble-sort-like technique because you actually sort the numbers, rather than print them sorted. Also, it uses less if's than just printing them sorted. IMO it's a more elegant way of solving the problem but that's just me...

Bubble sorting is kind of pointless for non arrays. No need to get into it when only sorting 3 numbers. It's just over complicating a simple task.
 
helps
I don't get why you're arguing with me. Nowhere did I tell him to learn bubble sort. I'm just telling him how to sort 3 numbers using a technique similar to bubble sort. I'm just trying to help the guy, so why are you trying to stop that? Anyways, he can do what he wants...
 
Runescape Gamblingrunescape gambling
Back
Top