C++ Read Document and Print Largest and Smallest Number
-
10-03-2006 #one
wise_ron
largest and smallest number
This program is running good, the problem is that is suppose to impress the smallest and largest, simply sometimes it just prints the largest and sometimes it prints the smallest. I desire both of them to exist printed at the end. I want to do this plan with if and if else... tin one help me.
Fell free to give me your opinions and what my mistakes are. Thank you Very Much
Code:
/* Write a plan that reads in 4 integers and determines and prints the largest and the smallest integeers in the group. */ #include <stdio.h> int main() { int a,b,c,d,e; printf( "Enter number for '1':\n" ); scanf( "%d", &a ); printf( "Enter number for 'TWO':\north" ); scanf( "%d", &b ); printf( "Enter number for 'Three':\n" ); scanf( "%d", &c ); printf( "Enter number for 'Four':\due north" ); scanf( "%d", &d ); printf( "Enter number for 'V':\due north" ); scanf( "%d", &e ); if (a > b && a > c && a > d && a > eastward) printf( "%d is the largest number\due north", a); else if(a < b && a < c && a < d && a < e) printf( "%d is the smallest number\due north", a); else if(b > a && b > c && b > d && b > eastward) printf( "%d is the largest number\northward", b); else if(b < a && b < c && b < d && b < e) printf( "%d is the smallest number\n", b); else if(c > a && c > b && c > d && c > e) printf( "%d is the largest number\north", c); else if(c < a && c < b && c < d && c < eastward) printf( "%d is the smallest number\n", c); else if(d > a && d > b && d > c && d > e) printf( "%d is the largest number\due north", d); else if(d < a && d < b && d < c && d < due east) printf( "%d is the smallest number\n", d); else if(eastward > a && e > b && east > c && e > d) printf( "%d is the largest number\due north", e); else if(e < a && e < b && e < c && e < d) printf( "%d is the smallest number\northward", e); render 0; }
Wise_ron
One man's constant is another homo'due south variable
-
10-03-2006 #two
The superhaterodyne
In your programme, Modify the else if'southward to if's -
if ( a largest conditions )
else if ( a smallest weather )if ( b largest conditions )
else if ( b smallest conditions )etc
EDIT - Also - "Write a program that reads in four integers [...]". You lot're reading in five integers
Last edited by twomers; 10-03-2006 at 01:19 PM.
-
10-03-2006 #3
{Jaxom,Imriel,Liam}'south Dad
Utilise two additional vars for min and max. Set min and max to the beginning number you read in. And so, compare the min and max to the rest of the values.
EDIT: And, past the way, search the forums. . . this has been asked quite a few times.
-
x-03-2006 #4
wise_ron
ok, i got it!
Thanks twomers and Kennedy. As Twomers said what i had to practise is alter the code to this and it works:
Code:
#include <stdio.h> int main() { int a,b,c,d,e; printf( "Enter number for 'ONE':\n" ); scanf( "%d", &a ); printf( "Enter number for 'Ii':\n" ); scanf( "%d", &b ); printf( "Enter number for 'Three':\due north" ); scanf( "%d", &c ); printf( "Enter number for 'Four':\due north" ); scanf( "%d", &d ); printf( "Enter number for 'Five':\n" ); scanf( "%d", &e ); if (a > b && a > c && a > d && a > e) printf( "%d is the largest number\n", a); else if(a < b && a < c && a < d && a < e) printf( "%d is the smallest number\n", a); if(b > a && b > c && b > d && b > e) printf( "%d is the largest number\n", b); else if(b < a && b < c && b < d && b < e) printf( "%d is the smallest number\n", b); if(c > a && c > b && c > d && c > e) printf( "%d is the largest number\northward", c); else if(c < a && c < b && c < d && c < e) printf( "%d is the smallest number\north", c); if(d > a && d > b && d > c && d > eastward) printf( "%d is the largest number\n", d); else if(d < a && d < b && d < c && d < e) printf( "%d is the smallest number\n", d); if(e > a && e > b && e > c && eastward > d) printf( "%d is the largest number\n", e); else if(e < a && e < b && east < c && eastward < d) printf( "%d is the smallest number\n", e); return 0; }
Wise_ron
One man's abiding is another man's variable
-
10-03-2006 #v
Lurking
Just sort an array.
Code:
#include <stdio.h> void bubblesort ( int array[], int due north ) { int yard, j, temp, flag; for (k = due north - one; k > 0; --one thousand) { flag = 1; for (j = 0; thou > j; ++j) { if (array[j] > assortment[j + ane]) { temp = assortment[j]; array[j] = array[j+1]; array[j + 1] = temp; flag = 0; } } if (flag) intermission; } } int primary ( void ) { int numbers[] = { 3, 45, 31, xiii, }; bubblesort(numbers, sizeof numbers / sizeof numbers[0]); printf("largest number = %d, smallest number = %d\north", numbers[iii], numbers[0]); return 0; }
largest number = 45, smallest number = iii
-
10-04-2006 #6
and Nothing Else Matters
Here is a version which but uses if-else statements and iii variables:
Lawmaking:
#include <stdio.h> int master(void) { int num, min, max; puts("Enter number:"); scanf("%d",&num); min=max=num; puts("Enter number:"); scanf("%d",&num); if(num<=min) min=num; else max=num; puts("Enter number:"); scanf("%d",&num); if(num<=min) min=num; else max=num; puts("Enter number:"); scanf("%d",&num); if(num<=min) min=num; else max=num; printf("The Largest is: %d. Smallest is: %d",max,min); render 0; }
Information technology is not who I am inside but what I do that defines me.
-
10-04-2006 #7
Registered User
Code:
/* Write a plan that reads in 4 integers and determines and prints the largest and the smallest integeers in the group. */ #include <stdio.h> #include <limits.h> #define MAX_INTS 4 int main() { int min = INT_MAX; int max = INT_MIN; int a[ MAX_INTS ]; int i; for (i = 0; i < MAX_INTS; i++) { printf( "Enter number %d of %d : ", (i + 1), MAX_INTS); scanf( "%d", &a[i] ); if ( a[i] < min) min = a[i]; if ( a[i] > max) max = a[i]; } printf("%d is the smallest number\north", min); printf("%d is the largest number\n", max); return 0; }
-
10-04-2006 #eight
Simply Lurking
This tin can also be done without an assortment.
Code:
/* ... */ int value; /* instead of an array */ /* ... */ for ( i = 0; i < MAX_INTS; i++ ) { /* prompt and user input into value */ if ( value < min ) { min = value; } if ( value > max ) { max = value; } } printf("min = %d, max = %d\n", min, max); /* ... */
vii. Information technology is easier to write an wrong programme than understand a correct one.
twoscore. There are ii ways to write error-free programs; only the tertiary one works.*
-
10-05-2006 #9
wise_ron
Improving the min & max
Thank you guys for your support, I take found that the program i have is not efficient and information technology has some bugs, because when i enter numbers equal numbers example: num1= 44, num2 =44, num3= 44, num4= 44. It wont print nothing.
The all-time way to do information technology with out doing an array is how Dave_Sinkula posted. I have modified the lawmaking, but i take a problem information technology dosent print the minimun number. It just prints a crazy number but the maximum number works fine.can some one signal out where is the error?
Lawmaking:
#include <stdio.h> int main() { int i,MAX_INTS=4,value,min,max; /* instead of an array */ for ( i = 0; i < MAX_INTS; i++ ) { printf("\nEnter a number: "); /* prompt and user input into value */ scanf("%d",&value); if ( value < min ) { min = value; } if ( value > max ) { max = value; } } printf("min = %d, max = %d\n", min, max); return 0; }
Wise_ron
One man's abiding is another human'south variable
-
10-05-2006 #x
Registered User
You're using min and max uninitialized. You should actually larn to pass at least -Wall to the compiler to take hold of these things. The header file "limits.h" defines two constants, INT_MIN and INT_MAX, that are useful in situations such as this.
Lawmaking:
1 #include <stdio.h> 2 #include <limits.h> three 4 int main() five { half-dozen int i, seven value, 8 min = INT_MAX, ix max = INT_MIN; 10 11 const int MAX_INTS = four; 12 13 for ( i = 0; i < MAX_INTS; i++ ) 14 { 15 printf(" \northward Enter a number: "); /* prompt and user input into value */ 16 scanf(" %d ",&value); 17 18 if ( value < min ) xix { 20 min = value; 21 } 22 if ( value > max ) 23 { 24 max = value; 25 } 26 } 27 printf("min = %d , max = %d \n ", min, max); 28 return 0; 29 } xxx
-
10-05-2006 #11
wise_ron
It works, Thanks
Thanks zx-1 i got information technology! I learn something new nearly the #include <limits.h>
I got it working and the code is actually brusque compare to my first code. Thanks
Wise_ron
I human's constant is another homo'southward variable
-
ten-05-2006 #12
ATH0
All zx-i did was mimic Skeane and put in pointless line numbers. If you actually read the replies people type to you lot it'll aid you in the long run. Oh, and it saves us all fourth dimension having to tell you again what you've already been told.
Quzah.
Hope is the showtime step on the road to disappointment.
C++ Read Document and Print Largest and Smallest Number
Source: https://cboard.cprogramming.com/c-programming/83749-largest-smallest-number.html