[prog] C++

wolf wolf at wolfrising.net
Fri Dec 12 21:03:36 EST 2003


Oh thank you : )

so then largest is:

int largest;

         if(integer1 > integer2) {
                 if(integer1 > integer3) {
                         largest = integer1;
                 }
                 else {
                         largest = integer3;
                 }
         }
         else if(integer2 > integer3) {
                 largest = integer2;
         }
         else {
                 largest = integer3;
         }

          std::cout << "The largest is " << largest << std::endl;


On Dec 12, 2003, at 7:49 PM, Jacinta Richardson wrote:

> On Fri, 12 Dec 2003, wolf wrote:
>
>> I'm trying to teach myself a bit of C++ following along with a book,
>> the sample asks to have three numbers entered in via the screen
>> that print back the sum, average, product, largest, and smallest of 
>> the
>> numbers. I think I have it working for the first three
>> (sum, average, and product) but I'm having trouble trying to figure 
>> out
>> how to make it select the largest and smallest of the
>> three numbers.  Would anyone know how to do this? Here's what I've 
>> come
>> up with so far that seems to return the correct
>> responses.  I'm at the very beginning part of being a beginner : )
>> Thanks : )
>
> I think it means you should reuse the numbers you're given rather than
> keeping asking for more numbers.  So something more like:
>
>> #include <iostream>
>>
>> int main()
>>
>> {
>>
>>          //sum
>>          int integer1;
>>          int integer2;
>>          int integer3;
>>          int sum;
>>
>>          std::cout << "Enter the first integer\n";
>>          std::cin >> integer1;
>>
>>          std::cout << "Enter the second integer\n";
>>          std::cin >> integer2;
>>
>>          std::cout << "Enter the third integer\n";
>>          std::cin >> integer3;
>>
>>          sum = integer1 + integer2 + integer3;
>>
>>          std::cout << "The sum is " << sum << std::endl;
>>
>>         // average
>>          int average;
>>
>>          average = (integer1 + integer2 +integer3) / 3;
>>
>>          std::cout << "The average is " << average << std::endl;
>
>>          //product
>>          int product;
>>          std::cin >> integer3;
>>
>>          product = integer1 * integer2 * integer3;
>>
>>          std::cout << "The product is " << product << std::endl;
>>
>>          return 0;
>>
>> }
>
> As for finding out the smallest number you'll need to use comparisons 
> and
> conditionals.  The if conditional is usually written as follows:
>
> if (condition) {
> 	BLOCK
> }
> else if (condition) {
> 	BLOCK
> }
> else {
> 	BLOCK
> }
>
> Where a BLOCK is zero or more programming statements, these can include
> further conditionals and other stuff. The else if and the else sections
> are optional.  Numerical comparison operators include < (less than), >
> (greater than), == (equal to).
>
> To find the smallest number I would do something like this.
>
> 	int smallest;
>
> 	if(integer1 < integer2) {
> 		if(integer1 < integer3) {
> 			smallest = integer1;
> 		}
> 		else {
> 			smallest = integer3;
> 		}
> 	}
> 	else if(integer2 < integer3) {
> 		smallest = integer2;
> 	}
> 	else {
> 		smallest = integer3;
> 	}
>
> Although there are many more solutions to this problem.
>
> All the best,
>
> 	Jacinta
>
> --
>    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
>     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
>     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |
>   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
> (il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |
>



More information about the Programming mailing list