|
-
Aug 31st, 2007, 04:34 PM
#1
Thread Starter
Member
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 &&
My questions might look stupid to others. After all I am still in the process of learning Visual Basic.
-
Aug 31st, 2007, 06:01 PM
#2
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Aug 31st, 2007, 10:19 PM
#3
Thread Starter
Member
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.
My questions might look stupid to others. After all I am still in the process of learning Visual Basic.
-
Sep 1st, 2007, 12:29 AM
#4
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!
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Sep 1st, 2007, 02:40 PM
#5
Thread Starter
Member
Re: Finding out which integer is smallest
 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?
My questions might look stupid to others. After all I am still in the process of learning Visual Basic.
-
Sep 1st, 2007, 05:22 PM
#6
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
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 1st, 2007, 06:28 PM
#7
Thread Starter
Member
Re: Finding out which integer is smallest
it's not oh well imma give up
My questions might look stupid to others. After all I am still in the process of learning Visual Basic.
-
Sep 1st, 2007, 10:01 PM
#8
Addicted Member
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
-
Sep 2nd, 2007, 11:33 PM
#9
Re: Finding out which integer is smallest
Pretty sure his book won't as yet have covered classes and dynamic memory Or const correctness, or initialization lists....... 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Sep 3rd, 2007, 11:23 AM
#10
Thread Starter
Member
Re: Finding out which integer is smallest
 Originally Posted by chemicalNova
Pretty sure his book won't as yet have covered classes and dynamic memory  Or const correctness, or initialization lists.......
chem
*** is that?
My questions might look stupid to others. After all I am still in the process of learning Visual Basic.
-
Sep 3rd, 2007, 11:30 PM
#11
Re: Finding out which integer is smallest
I rest my case 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|