Question...!

How do I find the large and small number with 5 integers!? All I know is what is "Greater/less to" with 2 integers and the "IF" statements.
Last edited on
What 5 numbers? That is, how are they stored? Are you using an array, or are they 5 separate variables, or are you trying to prompt the user for five numbers then determine the largest and smallest from that?
What 5 numbers? That is, how are they stored? Are you using an array, or are they 5 separate variables, or are you trying to prompt the user for five numbers then determine the largest and smallest from that?


Sorry I mean "5 integers". Prompt the user for 5 integers.
Ok,
Use a for loop and an array to prompt and store the int's.
Then define two variables, largest and smallest.
Use a second for loop to iterate through the array and compare the current position to the largest encountered number and the smallest encountered number. If the current value is larger than the largest, set the largest to the current. Same logic for the smallest one...
Ok,
Use a for loop and an array to prompt and store the int's.
Then define two variables, largest and smallest.
Use a second for loop to iterate through the array and compare the current position to the largest encountered number and the smallest encountered number. If the current value is larger than the largest, set the largest to the current. Same logic for the smallest one...


Ummmmm ok.... I haven't learn array/loop... I'm like at the beginning of learning C++. The question I asked you was part of my book exericse. :\
Hmm,
Are you familiar with variables yet?
Yes
look what i did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
int integer1,diameter,circumference,area;
cout << "Enter the Radius\n";
cin >> integer1;
circumference = 2 * 3.14 * integer1;
cout << "Circumference: " << circumference << endl;
diameter = 2 * integer1;
cout << "Diameter: " << diameter << endl;
area = 3.14 * (integer1 * integer1);
cout << "Area: " << area << endl;
return 0;
}
Last edited on
Alright.
Try creating three variables, greatest, smallest, and userIn.
You can then use cin to get user input and store it to userIn.
All you have to do then is compare userIn to greatest. If userIn is greater than greatest, update greatest to equal userIn. If userIn is less than smallest, update smallest to equal userIn. Do this five times and you are good to go.
The first time you do that though, you need to set greatest and smallest to userIn as the first input will be both the largest and smallest input so far...
can you give me an example? For me its hard to understand without the examples. Thank you!
I don't really want to do your homework for you... But I'll get you started in the right direction.
1
2
3
4
5
6
7
8
9
10
11
...
int userIn, max, min; //define vars
cin>>userIn; //Get 1st number
max=userIn; //Set Maximum to first number
min=userIn; //Set Minimum to first number
cin>>userIn; //Get next number
if (userIn>max) //Check if user entered amount bigger than current maximum
  max=userIn; //If user entered amount bigger than current maximum, update current maximum
if (userIn<min) //Check if user entered amount smaller than current minimum
  min=userIn; //If user entered amount smaller than current minimum, update current minimum
...
For "userIn", is that like integer1,integer2,and integer3? Do they hold integers?
Yes, userIn is a integer just like the rest. It's just named different.
You still there? I need help... I did it but I only do it with 3 integers. What did I do wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
int int1,int2,int3,sum,average,product,small,large;
cout << "Enter 3 integers\n";
cin >> int1,int2,int3;
sum = int1 + int2 + int3;
cout << "Sum: " << sum << endl;
average = (int1 + int2 + int3) / 3;
cout << "Average: " << average << endl;
product = int1 * int2 * int3;
cout << "Product: " << product << endl;
if (int1>large) large = int1;
if (int1<small) small = int1;
if (int2>large) large = int2;
if (int2<small) small = int2;
if (int3>large) large = int3;
if (int3<small) small = int3;
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
int main()
{
int userIn, sum, product, small, large;
cout << "Enter 5 integers\n";
cin >> userIn;
sum=userIn;
product=userIn;
small=userIn;
large=userIn;

cin >> userIn;
sum=sum+userIn;
product=product*userIn;
if (userIn<small)
  small=userIn;
if (userIn>large)
  large=userIn;

cin >> userIn;
sum=sum+userIn;
product=product*userIn;
if (userIn<small)
  small=userIn;
if (userIn>large)
  large=userIn;

cin >> userIn;
sum=sum+userIn;
product=product*userIn;
if (userIn<small)
  small=userIn;
if (userIn>large)
  large=userIn;

cin >> userIn;
sum=sum+userIn;
product=product*userIn;
if (userIn<small)
  small=userIn;
if (userIn>large)
  large=userIn;

cout << "Sum: " << sum << endl;
cout << "Average: " << sum/5 << endl;
cout << "Product: " << product << endl;
cout << "Smallest: " << small << endl;
cout << "Largest: " << large << endl;
return 0;
}


Or if you want to simplify it into a for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
int main()
{
int userIn, sum, product, small, large;
cout << "Enter 5 integers\n";
cin >> userIn;
sum=userIn;
product=userIn;
small=userIn;
large=userIn;

for (int i=1; i<5; i++) {
  cin >> userIn;
  sum=sum+userIn;
  product=product*userIn;
  if (userIn<small)
    small=userIn;
  if (userIn>large)
    large=userIn;
}

cout << "Sum: " << sum << endl;
cout << "Average: " << sum/5 << endl;
cout << "Product: " << product << endl;
cout << "Smallest: " << small << endl;
cout << "Largest: " << large << endl;
return 0;
}
I think what's best for me is to learn what are loops/array...!
Last edited on
They are indeed useful :)
#include <iostream>
using namespace std;
int main()
{
int userIn[5],small, large;
cout << "Enter 5 integers\n";
for(int a=0;a<5;a++)//enter 5 numbers
{
cin >> userIn[a];
}
small=userIn[0];
large=userIn[0];

for (int b=1; b<5; b++) {
if (userIn[b]<small)
small=userIn[b];
if (userIn[b]>large)
large=userIn[b];
}
cout << "Smallest: " << small << endl;
cout << "Largest: " << large << endl;

return 0;
}
Topic archived. No new replies allowed.