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.
13 141309
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
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 -
float findLowest (int a, int b,int c, int d, int e)
-
{
-
// Local Definitions
-
int min;
-
-
// Statements
-
if (a < b && a < c && a < d && a < e)
-
return a;
-
else
-
if (b < a && b < c && b < d && a < e);
-
return b;
-
else
-
if (c < a && c < b && c < d && c < e);
-
return c;
-
else
-
if (d < a && d < b && d < c && d < e);
-
return d;
-
else
-
if (e < a && e < b && e < c && e < d);
-
return e;
-
-
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.
- #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......
#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
Yes for the above code you can give n number of inputs but to stop getting input you have enter 0
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
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.
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?
Ganon11 3,652
Recognized Expert Specialist
I assume you have a section of code like - int num1, num2, num3, num4, num5;
-
cout << "Enter the first number: ";
-
cin >> num1;
-
cout << "Enter the second number: ";
-
cin >> num2;
-
cout << "Enter the third number: ";
-
cin >> num3;
-
cout << "Enter the fourth number: ";
-
cin >> num4;
-
cout << "Enter the fifth number: ";
-
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: - int max, min, num;
-
-
cout << "Enter number 1: ";
-
cin >> num;
-
max = num;
-
min = num;
-
-
for (int i = 1; i < 5; i++) {
-
cout << "Enter number " << i + 1 << ": ";
-
cin >> num;
-
//Check if num > max
-
//Check if num < min
-
}
-
// 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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: ")
|
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
|
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?
|
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
|
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...
| |
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...
|
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"...
|
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 !!!
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |