473,800 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

64 New Member
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 141309
kvbreddy
23 New Member
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 New Member
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
rajesh6695
96 New Member
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 New Member
#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
rajesh6695
96 New Member
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
kvbreddy
23 New Member
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 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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

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

Similar topics

8
8244
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a program that reads 10 numbers from the user and prints out the sum of those numbers. num0 = input("Enter a number: ")
5
4474
by: Randi | last post by:
I am having problems with this question: Write a C++ while statement that will input numbers from a file until the end of file is reached. Output the lowest number in the file. Ok, I do understand the logic, I believe the following for loop solves it from keyboard input. But how do you point to any given value from a file. You can't assign anything to a variable. Little lost on this one. Thanks, Kelsey
4
4898
by: johnk | last post by:
I have a table of items, with revision numbers. I need to extract the items with highest revision number. The items may be listed several times and I don't know what the highest revision number for each item is. How do I do this?
1
5876
by: coolindienc | last post by:
I got this program running and gives me result that I want (actually not yet). Now I want to find out the lowest and highest numbers I entered. Andy advise??? number = int (input ("Enter a positive number that is below 126: ")) if number > 125: print "The number entered has to be 125 or lower.\n" number = input ("Enter a positive number that is below 126: ") else: total = 0 total += number
10
4377
by: strife | last post by:
Hi, This is a homework question. I will try to keep it minimal so not to have anyone do it for me. I am really just stuck on one small spot. I have to figure out the highest number from a users input. I give them a menu to choose from, and I am stuck on only one of the choices. When this choice is picked I must allow the user to enter how many numbers he/she wants to enter; then I must figure out which number is the highest. I have...
4
2140
by: myself2211 | last post by:
Hi, this is my first attempt at Python, what I am trying to achieve is a script that will ask the user for input, search a txt file and display the output or give not found message. Ideally I would like it to ask the user once the first output is achieved for further user input if required, otherwise exit the program. However, would be happy if it achieved the first objective. Following is as far as I can go, the problem is it is breaking...
3
10914
by: rickytb | last post by:
Hey all, I've hit a snag on a beginner Java problem involving building occupancy. I'm supposed to get user input from a GUI. My main problem is that I'm totally lost on how to take the information from the user and implement it in my GUI. I have the display set up the way I want but can't use the input. I don't quite know what to do. Whenever I hit one of the buttons I get "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"...
1
1672
by: Kris tarun | last post by:
I have a table of a retail store which has almost 13000 customers. and i want to write a query for this.. Group products based on their sales patterns. Highest, lowest, and median values. Use sale prices as an indicator as well (Do these products sell well only when they are discounted?). it also uses clustering.i dont know what to do Thanks a ton !!!
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.