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 139995
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?
...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |