PDA

View Full Version : Grrrr


kbum
05-29-2003, 03:04 PM
Hello. I am reading a book ( C++ How to program fourth edition) and so far I've learned how to write some simple programs and use the "if" statement. At the end of the chapter, there are some exercises. Here is one of them:

Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in this chapter.

I've learned how to use simple input and output statements, fundamental data types, arithmetic operators, the precedence of arithmetic operators and the if statement. How would I write a program that would do that using only this techniques? Here is the one I wrote but doesn't work(it is no done yet because I was trying to print only the smallest integer)


#include <iostream>


using std::cin;
using std::cout;
using std::endl;

int main()
{
int num1;
int num2;
int num3;
int num4;
int num5;
int smallest;


cout << "Input five different integers: ";

cin >> num1 >> num2 >> num3 >> num4 >> num5;
// Tests num1
if ( num1 < num2 )
smallest = num1;

if ( num1 > num2 )
smallest = num2;

if ( num1 < num3 )
smallest = num1;

if ( num1 > num3 )
smallest = num3;

if ( num1 < num4 )
smallest = num1;

if ( num1 > num4 )
smallest = num4;

if ( num1 < num5 )
smallest = num1;

if ( num1 > num5 )
smallest = num5;

// Tests num2
if ( num2 < num1 )
smallest = num2;

if ( num2 > num1 )
smallest = num1;

if ( num2 < num3 )
smallest = num2;

if ( num2 > num3 )
smallest = num3;

if ( num2 < num4 )
smallest = num2;

if ( num2 > num4 )
smallest = num4;

if ( num2 < num5 )
smallest = num2;

if ( num2 > num5 )
smallest = num5;

// Tests num3
if ( num3 < num1 )
smallest = num3;

if ( num3 > num1 )
smallest = num1;

if ( num3 < num2 )
smallest = num3;

if ( num3 > num2 )
smallest = num2;

if ( num3 < num4 )
smallest = num3;

if ( num3 > num4 )
smallest = num4;

if ( num3 < num5 )
smallest = num3;

if ( num3 > num5 )
smallest = num5;

// Tests num4
if ( num4 < num1 )
smallest = num4;

if ( num4 > num1 )
smallest = num1;

if ( num4 < num2 )
smallest = num4;

if ( num4 > num2 )
smallest = num2;

if ( num4 < num3 )
smallest = num4;

if ( num4 > num3 )
smallest = num3;

if ( num4 < num5 )
smallest = num4;

if ( num4 > num5 )
smallest = num5;

// Tests num5
if ( num5 < num1 )
smallest = num5;

if ( num5 > num1 )
smallest = num1;

if ( num5 < num2 )
smallest = num5;

if ( num5 > num2 )
smallest = num2;

if ( num5 < num3 )
smallest = num5;

if ( num5 > num3 )
smallest = num3;

if ( num5 < num4 )
smallest = num5;

if ( num5 > num4 )
smallest = num4;

cout << "Smallest is " << smallest << endl;

return 0;
}

Strike
05-29-2003, 04:20 PM
Well, you've got the right idea, but you're working too hard :) You only have to compare the largest/smallest of each pair of numbers once to figure out which are the largest and which are the smallest. Here's some pseudocode:


# Set the smallest and largest of the first two first, then
# compare to the rest of the integers
if num1 > num2:
largest = num1
smallest = num2
else:
largest = num2
smallest = num1

if largest < num3:
largest = num3

if largest < num4:
largest = num4

if largest < num5:
largest = num5

if smallest > num3:
smallest = num3

if smallest > num4:
smallest = num4

if smallest > num5:
smallest = num5


(tee hee, that wasn't only pseudocode, that was Python! :p)

kbum
05-30-2003, 11:33 AM
Thank you Strike. There is another exercise and this one I have NO CLUE how to solve. It tells me to write a program that inputs a five-digit number, separates the number into its individual digits and prints the digits separated from one another by three spaces each. Then it gives me a hint, to use the integer division and modulus operators. I don't want you to write the program for me. Can you just give me one more hint so I can put my head to work? Thanks

Kamikaze!
05-30-2003, 11:47 AM
Use modulus to "scrape" the individual digits off:

int number = 56335;

// number % 10000 returns 6335 (the remainder),
// 56335 - 6335 = 50000,
// 50000 / 10000 = 5;
int lastdigit = (number - (number % 10000)) / 10000;

And so on.

kbum
05-30-2003, 01:18 PM
I did it :D Thank you Kamikaze! Here is the code


#include <iostream>

using namespace std;

int main()
{
int number;
int first_digit;
int second_digit;
int third_digit;
int fourth_digit;
int fifth_digit;

cout << "Input a five-digit number: ";

cin >> number;

first_digit = (number - (number % 10000)) / 10000;

second_digit = (number % 10000 - (number % 1000)) / 1000;

third_digit = (number % 1000 - (number % 100)) / 100;

fourth_digit = (number % 100 - (number % 10)) / 10;

fifth_digit = (number % 10 - (number % 1)) / 1;

cout << first_digit << " " << second_digit << " "
<< third_digit << " " << fourth_digit << " "
<< fifth_digit << endl;

return 0;
}