Finding out which integer is smallest
I'm
Code:
// 1 .cpp
//
// Write a program that inputs three integers from the keyboards and prints the sum, average,
// product, smallest and largest of these numbers.
//
// The screen dialog should appear as follows:
// Input three different integers: 13 27 14
// Sum is 54
// Average is 18
// Product is 4914
// Smallest is 13
// Largest is 27
// Include preprocessing directive as done below:
#include <iostream>
// Include all of the other yummy crap below
using std::cout;
using std::cin;
using std::endl;
// Remember to always start the actual program with function "main()"
int main()
{
cout << "Input your three numbers: ";
int a;
int b;
int c;
int all;
cin >> a >> b >> c;
all = a + b + c;
cout << "\nThe sum of your three integers is " << all << "\n";
all = (a + b + c) / 3;
cout << "The average of your three integers is " << all << "\n";
all = a * b * c;
cout << "The product of your three integers is " << all << "\n";
if (a < b && a < c)
cout << "Out of the three integers that you input " << a << " is the smallest of them all\n";
if (b < a && b < c)
cout << "Out of the three integers that you input " << b << " is the smallest of them all\n";
if (c < a && c < b)
cout << "Out of the three integers that you input " << c << " is the smallest of them all\n";
if (a == b)
cout << "Out of the three integers that you input " << a << " and " << b << " are tied for being the smallest numbers\n";
if (a == c && a != b)
cout << "Out of the three integers that you input " << a << " and " << c << " are tied for being the smallest numbers\n";
if (b == c && b != a)
cout << "Out of the three integers that you input " << b << " and " << c << " are tied for being the smallest numbers\n";
if ( a == b && b == c)
cout << "All of the numbers that you have entered are the exact same!, or there is an error!\n";
cin >> a;
return 0;
}
I'm learning this stuff out of a book. It's asking me tell the user which integer out of the three that they input which is smallest. Is there a math equation to find out which of the three numbers is the smallest because it says not to use anything not mentioned in the chapter :( I'm not even suppose to use &&
Re: Finding out which integer is smallest
What is even in the chapter? We can't really say anything before knowing that. It's probably just asking you to have some nested if's. Even perhaps something like:
C++ Code:
int smallest = a;
if (b < a)
smallest = b;
if (c < b)
smallest = c;
if (c < a)
smallest = c;
..etc.
chem
Re: Finding out which integer is smallest
Write a program that inputs three integers from the keyboards and prints the sum, average,
product, smallest and largest of these numbers.
anyone else?? I'm stuck on telling which number is smallest.
Re: Finding out which integer is smallest
Find the minimum of two values; then find the minimum of your selected minimum and the other value.
If you had a function MIN(a,b) which returned the lesser of two values:
Code:
int a, b, c;
min = MIN(a, b);
min = MIN(min, c);
// min is the minimum value.
your min function might do this:
Code:
if (a < b)
return a; // a is less than b
else
return b; // b is less than a OR a = b (they are both the 'min')
Hope this helps!
Re: Finding out which integer is smallest
Quote:
Originally Posted by sunburnt
Find the minimum of two values; then find the minimum of your selected minimum and the other value.
If you had a function MIN(a,b) which returned the lesser of two values:
Code:
int a, b, c;
min = MIN(a, b);
min = MIN(min, c);
// min is the minimum value.
your min function might do this:
Code:
if (a < b)
return a; // a is less than b
else
return b; // b is less than a OR a = b (they are both the 'min')
Hope this helps!
I'm srry I haven't gotten to that stuff yet and in the chapter it says i'm only suppose to use what I have learned so far. Is there like an equation or something to find out which of the numbers given is smallest?
Re: Finding out which integer is smallest
If they truely want you to use only what you've learnt so far.. that equation you're looking for should be in the book.
chem
Re: Finding out which integer is smallest
it's not oh well :( imma give up :(
Re: Finding out which integer is smallest
I have a little free time :)
Code:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
class CTest {
int a;
int b;
int c;
public:
CTest(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {};
int Sum() const {
return a+b+c;
}
int Average() const {
return Sum()/3;
}
int Product() const {
return a*b*c;
}
int Smallest() const {
int s = a;
if (b < s) s = b;
if (c < s) s = c;
return s;
}
int Largest() const {
int l = a;
if (b > l) l = b;
if (c > l) l = c;
return l;
}
};
int main() {
int a, b, c;
cout << "Input your three numbers:\n";
cin >> a >> b >> c;
CTest *pTest = new CTest(a, b, c);
cout << "Sum is " << pTest->Sum() << endl;
cout << "Average is " << pTest->Average() << endl;
cout << "Product is " << pTest->Product() << endl;
cout << "Smallest is " << pTest->Smallest() << endl;
cout << "Largest is " << pTest->Largest() << endl;
delete pTest;
return 0;
}
I think 2nd post of this topic, chemicalNova's code, is the right for you
Re: Finding out which integer is smallest
Pretty sure his book won't as yet have covered classes and dynamic memory :p Or const correctness, or initialization lists....... :p
chem
Re: Finding out which integer is smallest
Quote:
Originally Posted by chemicalNova
Pretty sure his book won't as yet have covered classes and dynamic memory :p Or const correctness, or initialization lists....... :p
chem
*** is that?
Re: Finding out which integer is smallest