Results 1 to 11 of 11

Thread: Finding out which integer is smallest

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    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.

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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:
    1. int smallest = a;
    2.  
    3. if (b < a)
    4.     smallest = b;
    5. if (c < b)
    6.     smallest = c;
    7. if (c < a)
    8.     smallest = c;
    ..etc.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    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.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    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?
    My questions might look stupid to others. After all I am still in the process of learning Visual Basic.

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    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.

  8. #8
    Addicted Member Kal-El's Avatar
    Join Date
    Jun 2007
    Location
    Fortress of solitude
    Posts
    179

    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

    «Source Code»«plugins»«skin»«fav apps»«Winamp en español»«Nsis en español»
    So what if your signature move is driving a tractor? I think it's adorable. - Lois ("Crimson")
    Don't forget to when your question is resolved ...and the people who help

  9. #9
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    51

    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 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.

  11. #11
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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
  •  



Click Here to Expand Forum to Full Width