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 141189
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
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?
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.
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.
Well, the code I have is let me just go ahead and post the whole thing. It isn't finish yet. -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <iostream>
-
using namespace std;
-
-
// Function Declarations
-
char getOption (void);
-
void getData (int* a, int* b, int* c, int* d, int e);
-
float calc (int option, int a, int b, int c, int d, int e);
-
-
int main (void)
-
{
-
// Local Declarations
-
char toLower;
-
int a,b,c,d,e;
-
float result;
-
-
// Statements
-
toLower = getOption ();
-
-
-
return 0;
-
} // main
-
-
/* ================= getOption =================
-
This asks what option the user will like to display
-
Such as smallest, largest, sum, average.
-
Pre getOption
-
Post Tax income, total tax, and tax due
-
calculated
-
*/
-
char getOption (void)
-
{
-
// Local Declarations
-
char toLower;
-
-
// Statement
-
printf ("\t**********************************");
-
printf("\n\t* MENU *");
-
printf("\n\t* *");
-
printf("\n\t* L. Find Lowest *");
-
printf("\n\t* H. Find Highest *");
-
printf("\n\t* S. Calculate Sum *");
-
printf("\n\t* A. Calculate Average *");
-
printf("\n\t* *");
-
printf("\n\t**********************************");
-
-
printf("\n\nPlease type your choice ");
-
printf("and key Enter: ");
-
scanf ("%c", &toLower);
-
return toLower;
-
} // getOption
-
-
/* ====================== getData ====================
-
This function reads two integers from the keyboard.
-
Pre Parameters a,b,c,d, and e are addresses
-
Post Data read into parameter addresses
-
*/
-
void getData (int* a, int* b, int* c, int* d, int e)
-
{
-
printf("Please enter five integer numbers: ");
-
scanf("%d %d %d %d %d", a, b, c, d, e);
-
return;
-
} // getData
-
-
/* ==================== doCalc ====================
-
This function determines the type of operation
-
and calls a function to perform it.
-
Pre option contains the operation
-
num1 & num2 contains data
-
Post returns the results
-
*/
-
-
float calc (int toLower, int a, int b, int c, int d, int e)
-
{
-
// Local Declarations
-
float result;
-
-
// Statements
-
switch(toLower)
-
{
-
case 1: result = findLowest (a, b, c, d, e);
-
break;
-
case 2: result = findHighest(a, b, c, d, e);
-
break;
-
case 3: result = 3.0; // Multiply
-
break;
-
case 4: if (num2 == 0.0) // Divide
-
{
-
printf("\n\a\aError: ");
-
printf("division by zero ***\n");
-
exit (100);
-
} // if
-
else
-
result = 4.0;
-
break;
-
/* Better structured programming would validate
-
option in getOption. However, we have not
-
yet learned the technique to code it there.
-
*/
-
default: printf("\aOption not available\n");
-
exit (101);
-
} // switch
-
printf("**In calc result is: %6.2f\n", result);
-
return result;
-
} // calc
-
-
/* ====================== findLowest =================
-
This function adds two numbers and returns the sum.
-
Pre a and b contain values to be added
-
Post Returns the lowest number
-
*/
-
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;
-
-
} // findLowest
-
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
-
-
-
-
-
-
- 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.
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <iostream>
-
using namespace std;
-
-
// Function Declarations
-
char getOption (void);
-
void getData (int* a, int* b, int* c, int* d, int e);
-
float calc (int option, int a, int b, int c, int d, int e);
-
-
int main (void)
-
{
-
// Local Declarations
-
char toLower;
-
int a,b,c,d,e;
-
float result;
-
-
// Statements
-
toLower = getOption ();
-
-
-
return 0;
-
} // 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. - /* ================= getOption =================
-
This asks what option the user will like to display
-
Such as smallest, largest, sum, average.
-
Pre getOption
-
Post Tax income, total tax, and tax due
-
calculated
-
*/
-
char getOption (void)
-
{
-
// Local Declarations
-
char toLower;
-
-
// Statement
-
printf ("\t**********************************");
-
printf("\n\t* MENU *");
-
printf("\n\t* *");
-
printf("\n\t* L. Find Lowest *");
-
printf("\n\t* H. Find Highest *");
-
printf("\n\t* S. Calculate Sum *");
-
printf("\n\t* A. Calculate Average *");
-
printf("\n\t* *");
-
printf("\n\t**********************************");
-
-
printf("\n\nPlease type your choice ");
-
printf("and key Enter: ");
-
scanf ("%c", &toLower);
-
return toLower;
-
} // getOption
-
-
/* ====================== getData ====================
-
This function reads two integers from the keyboard.
-
Pre Parameters a,b,c,d, and e are addresses
-
Post Data read into parameter addresses
-
*/
-
void getData (int* a, int* b, int* c, int* d, int e) // e should be pointer too (*e)
-
{
-
printf("Please enter five integer numbers: ");
-
scanf("%d %d %d %d %d", a, b, c, d, e);
-
return;
-
} // getData
-
-
/* ==================== calc ====================
-
(This header said doCalc, but function name is calc - I fixed it)
-
This function determines the type of operation
-
and calls a function to perform it.
-
Pre option contains the operation
-
num1 & num2 contains data
-
Post returns the results
-
*/
-
-
float calc (int toLower, int a, int b, int c, int d, int e) // toLower should be passed as char
-
{
-
// Local Declarations
-
float result;
-
-
// Statements
-
switch(toLower)
-
{
-
case 1: result = findLowest (a, b, c, d, e);
-
break;
-
case 2: result = findHighest(a, b, c, d, e);
-
break;
-
case 3: result = 3.0; // Multiply
-
break;
-
case 4: if (num2 == 0.0) // Divide
-
{
-
printf("\n\a\aError: ");
-
printf("division by zero ***\n");
-
exit (100);
-
} // if
-
else
-
result = 4.0;
-
break;
-
/* Better structured programming would validate
-
option in getOption. However, we have not
-
yet learned the technique to code it there.
-
*/
-
default: printf("\aOption not available\n");
-
exit (101);
-
} // switch
-
printf("**In calc result is: %6.2f\n", result);
-
return result;
-
} // calc
-
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. - /* ====================== findLowest =================
-
This function adds two numbers and returns the sum.
-
Pre a and b contain values to be added
-
Post Returns the lowest number
-
*/
-
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;
-
-
} // findLowest
-
You should add a findHighest function based on findLowest - which, by the way, will work.
The code I suggested: - 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;
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.
[quote=Ganon11] -
#include <stdio.h>
-
#include <stdlib.h>
-
#include <iostream>
-
using namespace std;
-
-
// Function Declarations
-
char getOption (void);
-
void getData (int* a, int* b, int* c, int* d, int e);
-
float calc (int option, int a, int b, int c, int d, int e);
-
-
int main (void)
-
{
-
// Local Declarations
-
char toLower;
-
int a,b,c,d,e;
-
float result;
-
-
// Statements
-
toLower = getOption ();
-
-
-
return 0;
-
} // 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... -
void getData(&a, &b, &c, &d, &e)
-
char getOption (void);
-
void getData (int* a, int* b, int* c, int* d, int* e);
-
float calc (char toLower, int option, int a, int b, int c, int d, int e);
-
Also, I was going to ask I take out void getData (int* a, int* b, int* c, int* d, int* e);
- #include <stdio.h>
-
#include <stdlib.h>
-
int main(){
-
-
int i,s,x,lim;
-
s=0;
-
for(i=0;i<=5;i++){
-
-
printf("Digite um valor: ");
-
scanf("%d",&x);
-
if(x>=s) s = x;
-
if(i==0) lim = x;
-
if(x<lim) lim = x;
-
-
}
-
-
-
printf("MAX: %d, MIN: %d",s,lim);
-
system("pause");
-
-
}
-
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |