469,329 Members | 1,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

How to find the highest and lowest number of user input.

64
Hello again guys. I have a question. I'm working on a program for my class and I'm stuck on how to find the lowest and highest number of a user input. We aren't at arrays yet so that's out of the question, but what would be another way of finding the highest and lowest. I remember hearing something in the lines of "cout <<" in class, but forgot. Also I could try the if else statement, but would seem like alot of work. I hope I am making this clear, but bottom line is how to find the lowest and highest number of a user input ( 5 inputs). Thanks in advance.
Feb 15 '07 #1
13 136827
Hi,
we can do it without need of array. Here i am trying to explain the logic...
You can implement it.

1) min and max are two unsigned integer variables.
min is initialized with possible maximum value,
max is initialized with zero.
2) Loop the following three statements for n times.
Read a number .
if the number is < min, then min = number.
if the number is > max, then max = number.

3) Finally print min, max
Feb 15 '07 #2
td0g03
64
Hi,
we can do it without need of array. Here i am trying to explain the logic...
You can implement it.

1) min and max are two unsigned integer variables.
min is initialized with possible maximum value,
max is initialized with zero.
2) Loop the following three statements for n times.
Read a number .
if the number is < min, then min = number.
if the number is > max, then max = number.

3) Finally print min, max
Hello, let me see if I am getting this right. So the user input 5 integers and that will be initialized into a,b,c,d, and e all int. Then how would I would do something like... to find the min

Expand|Select|Wrap|Line Numbers
  1. float findLowest (int a, int b,int c, int d, int e)
  2. {
  3. //    Local Definitions
  4.     int min;
  5.  
  6. //    Statements
  7.      if (a < b && a < c && a < d && a < e)
  8.     return a;
  9.     else
  10.     if (b < a && b < c && b < d && a < e);
  11.     return b;
  12.     else
  13.     if (c < a && c < b && c < d && c < e);
  14.     return c;
  15.     else
  16.     if (d < a && d < b && d < c && d < e);
  17.     return d;
  18.     else
  19.     if (e < a && e < b && e < c && e < d);
  20.     return e;
  21.  
  22.  
I'm pretty sure that I have the else, if and return wrongs. Sorry, I haven't mastered or really understand it yet. Perhaps you can correct for me so I can see my mistakes and learn from them and understand why I got it why.
Feb 15 '07 #3
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main()
  4. {
  5.     unsigned int min,max,in,buf;
  6.     min=0,max=0;
  7.  
  8.     printf("\nEnter The Numbers or press Zero to exit....");
  9.     do
  10.     {
  11.         scanf("%u",&in);
  12.         if(in!=0)
  13.         {
  14.             if(in>=max)
  15.             {
  16.                 max=in;
  17.             }
  18.             if(in<=buf)
  19.             {
  20.                 min=in;
  21.             }
  22.             buf=in;
  23.         }
  24.         else
  25.         {
  26.             printf("The Max & Min value Entered are : %u %u",max,min);
  27.             break;
  28.         }
  29.     }while(1);
  30.     return 0;
  31. }
Try this code......
Feb 15 '07 #4
td0g03
64
#include <stdio.h>
#include <conio.h>
int main()
{
unsigned int min,max,in,buf;
min=0,max=0;

printf("\nEnter The Numbers or press Zero to exit....");
do
{
scanf("%u",&in);
if(in!=0)
{
if(in>=max)
{
max=in;
}
if(in<=buf)
{
min=in;
}
buf=in;
}
else
{
printf("The Max & Min value Entered are : %u %u",max,min);
break;
}
}while(1);
return 0;
}

Try this code......

Hmm, all that codes seems to do for me is let me input unlimited amount of numbers and never actually gives me the min/max
Feb 15 '07 #5
Yes for the above code you can give n number of inputs but to stop getting input you have enter 0
Feb 15 '07 #6
rajesh,
what is there in the buf initially????? It contains garbase value. So, in the first checking of in with buf, will give unexpected result.
In ur code we don't need buf. Simply use min in place of buf and make sure min is initialized with 32767 or 65535. (Maximum value that we can give)

Yes for the above code you can give n number of inputs but to stop getting input you have enter 0
Feb 15 '07 #7
Ganon11
3,652 Expert 2GB
I hope I am making this clear, but bottom line is how to find the lowest and highest number of a user input ( 5 inputs).
You will have to have 3 int variables - call them min, max, and num. You will have to ask the user for input into num before anything else - you will then initialize min and max to num, because after one input, num is the min AND the max.

You will then have to create a loop that will execute 4 more times (a for loop will do nicely). In each execution of the loop, you will 1) ask the user for input into num, 2) check if num > max (and if so, max = num), and 3) check if num < min (and if so, min = num). Once outside of the loop, print the min/max.

If you cannot use loops, you will have to manually ask the user for input 4 more times and write the checks after each input statement.

The function you wrote will work, but it is a bit tedious - the method described here will likely be much faster.
Feb 15 '07 #8
td0g03
64
You will have to have 3 int variables - call them min, max, and num. You will have to ask the user for input into num before anything else - you will then initialize min and max to num, because after one input, num is the min AND the max.

You will then have to create a loop that will execute 4 more times (a for loop will do nicely). In each execution of the loop, you will 1) ask the user for input into num, 2) check if num > max (and if so, max = num), and 3) check if num < min (and if so, min = num). Once outside of the loop, print the min/max.

If you cannot use loops, you will have to manually ask the user for input 4 more times and write the checks after each input statement.

The function you wrote will work, but it is a bit tedious - the method described here will likely be much faster.
Hello, something like this. I am doing two different functions for just for min and one for max. I already asked the user for 5 inputs which are a,b,c,d,e. I'm sorta confused on how to do a loop. Anyway you can give me a actual code example?
Feb 15 '07 #9
Ganon11
3,652 Expert 2GB
I assume you have a section of code like

Expand|Select|Wrap|Line Numbers
  1. int num1, num2, num3, num4, num5;
  2. cout << "Enter the first number: ";
  3. cin >> num1;
  4. cout << "Enter the second number: ";
  5. cin >> num2;
  6. cout << "Enter the third number: ";
  7. cin >> num3;
  8. cout << "Enter the fourth number: ";
  9. cin >> num4;
  10. cout << "Enter the fifth number: ";
  11. cin >> num5;
Then you will display your min and max by calling your min/max functions. This is perfectly fine.

What I was suggesting is something like this:

Expand|Select|Wrap|Line Numbers
  1. int max, min, num;
  2.  
  3. cout << "Enter number 1: ";
  4. cin >> num;
  5. max = num;
  6. min = num;
  7.  
  8. for (int i = 1; i < 5; i++) {
  9.    cout << "Enter number " << i + 1 << ": ";
  10.    cin >> num;
  11.    //Check if num > max
  12.    //Check if num < min
  13. }
  14. // max and min are now the largest/smallest values entered
However, you might not have been taught loops yet, which means the original code you have will be perfectly fine.
Feb 15 '07 #10
td0g03
64
I assume you have a section of code like

Expand|Select|Wrap|Line Numbers
  1. int num1, num2, num3, num4, num5;
  2. cout << "Enter the first number: ";
  3. cin >> num1;
  4. cout << "Enter the second number: ";
  5. cin >> num2;
  6. cout << "Enter the third number: ";
  7. cin >> num3;
  8. cout << "Enter the fourth number: ";
  9. cin >> num4;
  10. cout << "Enter the fifth number: ";
  11. cin >> num5;
Then you will display your min and max by calling your min/max functions. This is perfectly fine.

What I was suggesting is something like this:

Expand|Select|Wrap|Line Numbers
  1. int max, min, num;
  2.  
  3. cout << "Enter number 1: ";
  4. cin >> num;
  5. max = num;
  6. min = num;
  7.  
  8. for (int i = 1; i < 5; i++) {
  9.    cout << "Enter number " << i + 1 << ": ";
  10.    cin >> num;
  11.    //Check if num > max
  12.    //Check if num < min
  13. }
  14. // max and min are now the largest/smallest values entered
However, you might not have been taught loops yet, which means the original code you have will be perfectly fine.
Well, the code I have is let me just go ahead and post the whole thing. It isn't finish yet.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. //    Function Declarations 
  7. char getOption (void);
  8. void getData (int* a, int* b, int* c, int* d, int e);
  9. float calc   (int option, int a, int b, int c, int d, int e);
  10.  
  11. int main (void)
  12. {
  13. //    Local Declarations 
  14.     char toLower;
  15.     int a,b,c,d,e;
  16.     float result;
  17.  
  18. //    Statements 
  19.     toLower = getOption ();
  20.  
  21.  
  22.     return 0;
  23. }    // main 
  24.  
  25. /*    ================= getOption ================= 
  26.     This asks what option the user will like to display
  27.     Such as smallest, largest, sum, average.
  28.        Pre  getOption
  29.        Post Tax income, total tax, and tax due
  30.             calculated
  31. */
  32. char getOption (void)
  33. {
  34. //   Local Declarations
  35.      char toLower;
  36.  
  37. //   Statement
  38.     printf  ("\t**********************************");
  39.     printf("\n\t*              MENU              *");
  40.     printf("\n\t*                                *");
  41.     printf("\n\t*  L. Find Lowest                *");
  42.     printf("\n\t*  H. Find Highest               *");
  43.     printf("\n\t*  S. Calculate Sum              *");
  44.     printf("\n\t*  A. Calculate Average          *");
  45.     printf("\n\t*                                *");
  46.     printf("\n\t**********************************");
  47.  
  48.     printf("\n\nPlease type your choice ");
  49.     printf("and key Enter: ");
  50.     scanf ("%c", &toLower);
  51.     return toLower;
  52. }    // getOption 
  53.  
  54. /* ====================== getData ==================== 
  55.     This function reads two integers from the keyboard.
  56.        Pre   Parameters a,b,c,d, and e are addresses
  57.        Post  Data read into parameter addresses
  58. */
  59. void getData (int* a, int* b, int* c, int* d, int e)
  60. {
  61.     printf("Please enter five integer numbers: ");
  62.     scanf("%d %d %d %d %d", a, b, c, d, e);
  63.     return;
  64. }    // getData 
  65.  
  66. /*    ==================== doCalc ====================
  67.     This function determines the type of operation
  68.     and calls a function to perform it. 
  69.        Pre   option contains the operation
  70.              num1 & num2 contains data
  71.        Post  returns the results
  72. */
  73.  
  74. float calc (int toLower, int a, int b, int c, int d, int e) 
  75. {
  76. // Local Declarations 
  77. float result;
  78.  
  79. //    Statements 
  80.     switch(toLower)
  81.         {
  82.          case 1:  result = findLowest (a, b, c, d, e);
  83.                   break;
  84.          case 2:  result = findHighest(a, b, c, d, e);
  85.                   break;
  86.          case 3:  result = 3.0;              // Multiply
  87.                   break;
  88.          case 4:  if (num2 == 0.0)           // Divide
  89.                      {
  90.                       printf("\n\a\aError: ");
  91.                       printf("division by zero ***\n");
  92.                       exit (100);
  93.                      } // if 
  94.                   else
  95.                       result = 4.0;
  96.                   break;
  97.          /* Better structured programming would validate
  98.             option in getOption. However, we have not  
  99.             yet learned the technique to code it there.
  100.          */
  101.          default: printf("\aOption not available\n");
  102.                   exit (101);
  103.         } // switch 
  104. printf("**In calc result is: %6.2f\n", result);
  105.     return result;
  106. }    // calc 
  107.  
  108. /* ====================== findLowest =================
  109.     This function adds two numbers and returns the sum.
  110.        Pre   a and b contain values to be added
  111.        Post  Returns the lowest number
  112. */
  113. float findLowest (int a, int b,int c, int d, int e)
  114. {
  115. //    Local Definitions
  116.     int min;
  117.  
  118. //    Statements
  119.     if (a < b && a < c && a < d && a < e)
  120.     return a;
  121.     else
  122.     if (b < a && b < c && b < d && a < e);
  123.     return b;
  124.     else
  125.     if (c < a && c < b && c < d && c < e);
  126.     return c;
  127.     else
  128.     if (d < a && d < b && d < c && d < e);
  129.     return d;
  130.     else
  131.     if (e < a && e < b && e < c && e < d);
  132.     return e;
  133.  
  134. }    // findLowest 
  135.  
Well, I have to use a function that will call getData (The user inputed numbers) then use findLowest to find the min number. I haven't learned looped yet, but the first code you posted will only work *I think* if it isn't in functions. Perhaps I am wrong.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  • int toLowest (int a,int b,int c,int d,int e;)
  • int num1,num2,num3,num4,num5
  •  
  • cout << a;
  • cin >> num1;
  • cout << b;
  • cin >> num2;
  • cout << c;
  • cin >> num3;
  • cout << d;
  • cin >> num4;
  • cout << e;
  • cin >> num5;
  •  
  •     
  •  
  •  
  • I most likely did it wrong.
    Feb 15 '07 #11
    Ganon11
    3,652 Expert 2GB
    Expand|Select|Wrap|Line Numbers
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. //    Function Declarations 
    7. char getOption (void);
    8. void getData (int* a, int* b, int* c, int* d, int e);
    9. float calc   (int option, int a, int b, int c, int d, int e);
    10.  
    11. int main (void)
    12. {
    13. //    Local Declarations 
    14.     char toLower;
    15.     int a,b,c,d,e;
    16.     float result;
    17.  
    18. //    Statements 
    19.     toLower = getOption ();
    20.  
    21.  
    22.     return 0;
    23. }    // main 
    Right now, all you do is get what the user wants to do with the numbers that haven't been entered yet. You should add a call to getData(&a, &b, &c, &d, &e) before getOption. After getOption, you can call calc with the toLower char and the numbers.

    Expand|Select|Wrap|Line Numbers
    1. /*    ================= getOption ================= 
    2.     This asks what option the user will like to display
    3.     Such as smallest, largest, sum, average.
    4.        Pre  getOption
    5.        Post Tax income, total tax, and tax due
    6.             calculated
    7. */
    8. char getOption (void)
    9. {
    10. //   Local Declarations
    11.      char toLower;
    12.  
    13. //   Statement
    14.     printf  ("\t**********************************");
    15.     printf("\n\t*              MENU              *");
    16.     printf("\n\t*                                *");
    17.     printf("\n\t*  L. Find Lowest                *");
    18.     printf("\n\t*  H. Find Highest               *");
    19.     printf("\n\t*  S. Calculate Sum              *");
    20.     printf("\n\t*  A. Calculate Average          *");
    21.     printf("\n\t*                                *");
    22.     printf("\n\t**********************************");
    23.  
    24.     printf("\n\nPlease type your choice ");
    25.     printf("and key Enter: ");
    26.     scanf ("%c", &toLower);
    27.     return toLower;
    28. }    // getOption 
    29.  
    30. /* ====================== getData ==================== 
    31.     This function reads two integers from the keyboard.
    32.        Pre   Parameters a,b,c,d, and e are addresses
    33.        Post  Data read into parameter addresses
    34. */
    35. void getData (int* a, int* b, int* c, int* d, int e) // e should be pointer too (*e)
    36. {
    37.     printf("Please enter five integer numbers: ");
    38.     scanf("%d %d %d %d %d", a, b, c, d, e);
    39.     return;
    40. }    // getData 
    41.  
    42. /*    ==================== calc ====================
    43.         (This header said doCalc, but function name is calc - I fixed it)
    44.     This function determines the type of operation
    45.     and calls a function to perform it. 
    46.        Pre   option contains the operation
    47.              num1 & num2 contains data
    48.        Post  returns the results
    49. */
    50.  
    51. float calc (int toLower, int a, int b, int c, int d, int e) // toLower should be passed as char
    52. {
    53. // Local Declarations 
    54. float result;
    55.  
    56. //    Statements 
    57.     switch(toLower)
    58.         {
    59.          case 1:  result = findLowest (a, b, c, d, e);
    60.                   break;
    61.          case 2:  result = findHighest(a, b, c, d, e);
    62.                   break;
    63.          case 3:  result = 3.0;              // Multiply
    64.                   break;
    65.          case 4:  if (num2 == 0.0)           // Divide
    66.                      {
    67.                       printf("\n\a\aError: ");
    68.                       printf("division by zero ***\n");
    69.                       exit (100);
    70.                      } // if 
    71.                   else
    72.                       result = 4.0;
    73.                   break;
    74.          /* Better structured programming would validate
    75.             option in getOption. However, we have not  
    76.             yet learned the technique to code it there.
    77.          */
    78.          default: printf("\aOption not available\n");
    79.                   exit (101);
    80.         } // switch 
    81. printf("**In calc result is: %6.2f\n", result);
    82.     return result;
    83. }    // calc 
    84.  
    toLower is a char, so you should receive it as a char, not an int. Also, since toLower is a char, its values are likely not 1, 2, 3, or 4 - you should change these to 'L', 'H', 'S', and 'A' accordingly.

    Also, right now, in your 3 and 4 branches (sum and average), you don't really do anything - it's a bit confusing. You try to access num2, which does not exist, and you have labeled these sections Multiply and Divide - perhaps this is a section you got from a different program which needs to be adjusted? Regardless, you may want to make a function for the sum and average of each number to simplify things.

    Expand|Select|Wrap|Line Numbers
    1. /* ====================== findLowest =================
    2.     This function adds two numbers and returns the sum.
    3.        Pre   a and b contain values to be added
    4.        Post  Returns the lowest number
    5. */
    6. float findLowest (int a, int b,int c, int d, int e)
    7. {
    8. //    Local Definitions
    9.     int min;
    10.  
    11. //    Statements
    12.     if (a < b && a < c && a < d && a < e)
    13.     return a;
    14.     else
    15.     if (b < a && b < c && b < d && a < e);
    16.     return b;
    17.     else
    18.     if (c < a && c < b && c < d && c < e);
    19.     return c;
    20.     else
    21.     if (d < a && d < b && d < c && d < e);
    22.     return d;
    23.     else
    24.     if (e < a && e < b && e < c && e < d);
    25.     return e;
    26.  
    27. }    // findLowest 
    28.  
    You should add a findHighest function based on findLowest - which, by the way, will work.

    The code I suggested:

    Expand|Select|Wrap|Line Numbers
    1. int num1,num2,num3,num4,num5
    2.  
    3. cout << a;
    4. cin >> num1;
    5. cout << b;
    6. cin >> num2;
    7. cout << c;
    8. cin >> num3;
    9. cout << d;
    10. cin >> num4;
    11. cout << e;
    12. cin >> num5;
    was meant to be standalone - from what I read, I thought you only needed to find the lowest/highest number, rather than include it as part of a calculations program.
    Feb 15 '07 #12
    td0g03
    64
    [quote=Ganon11]
    Expand|Select|Wrap|Line Numbers
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. //    Function Declarations 
    7. char getOption (void);
    8. void getData (int* a, int* b, int* c, int* d, int e);
    9. float calc   (int option, int a, int b, int c, int d, int e);
    10.  
    11. int main (void)
    12. {
    13. //    Local Declarations 
    14.     char toLower;
    15.     int a,b,c,d,e;
    16.     float result;
    17.  
    18. //    Statements 
    19.     toLower = getOption ();
    20.  
    21.  
    22.     return 0;
    23. }    // main 



    Hello, thanks for the help! I fixed the things you mention. Part of the code the teacher told me to download and edit that why divide and sub was still there. I just have to make two more functions which are average and sum which I can do both without a problem. Just that getLowest and getHighest I didn't really understand how to do. I haven't compiled the program yet, but let me edit it to see if it will run.

    I was bit confused on what you were trying to explain on

    Right now, all you do is get what the user wants to do with the numbers that haven't been entered yet. You should add a call to getData(&a, &b, &c, &d, &e) before getOption. After getOption, you can call calc with the toLower char and the numbers.
    Let me see if I got this right... So I will first I will get the numbers inputed by the user first then ask them what option they will like to do correct? Thanks again for the help.

    This is what I did...
    Expand|Select|Wrap|Line Numbers
    1. void getData(&a, &b, &c, &d, &e)
    2. char getOption (void);
    3. void getData (int* a, int* b, int* c, int* d, int* e);
    4. float calc   (char toLower, int option, int a, int b, int c, int d, int e);
    5.  
    Also, I was going to ask I take out void getData (int* a, int* b, int* c, int* d, int* e);
    Feb 15 '07 #13
    Snip
    8
    Expand|Select|Wrap|Line Numbers
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. int main(){
    4.  
    5. int i,s,x,lim;
    6. s=0;
    7. for(i=0;i<=5;i++){
    8.  
    9.                    printf("Digite um valor: ");
    10.                    scanf("%d",&x);
    11.                    if(x>=s) s = x;
    12.                    if(i==0) lim = x;
    13.                    if(x<lim) lim = x;
    14.  
    15.                   }
    16.  
    17.  
    18. printf("MAX: %d, MIN: %d",s,lim);      
    19. system("pause");  
    20.  
    21. }
    22.  
    Oct 19 '14 #14

    Post your reply

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    8 posts views Thread by hokiegal99 | last post: by
    5 posts views Thread by Randi | last post: by
    reply views Thread by zhoujie | last post: by
    By using this site, you agree to our Privacy Policy and Terms of Use.