Chapter 4 Homework Code

advertisement
Programming Challenges
Phillip Lee March 13, 2015
1. Minimum/Maximum
Write a program that asks the user to enter two numbers. The program should use the conditional
operator to determine which number is the smaller and which is the larger.
Input
Number1
Number2
Processing
If (number1 > number2)
#include <iostream>
using namespace std;
int main()
{
int number1;
int number2;
int Max, Min;
cout << "Enter two different integers separated by a space: ";
cin >> number1 >> number2;
Min = (number1 < number2) ? (number1) : (number2);
Max = (number1 > number2) ? (number1) : (number2);
cout << "\nThe smaller number is " << Min << ".\n";
cout << "The larger number is " << Max << ".\n";
system("pause");
return 0;
}
Output
Min
Max
2. Roman Numeral Converter
Write a program that asks the user to enter a number within the range of 1 through 10.
Use a switch statement to display the Roman numeral version of that number.
Input Validation: Decide how the program should handle an input that is less than 1 or greater than 10.
Input
Number
Processing
Case 1: ….
Break;
Case 2: …..
Break;
Output
I,II,III,IV,V,VI,VII, VIII, IX, X
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please Enter a Number from 1 to 10: ";
cin >> number;
if (number > 10 || number < 1)
cout << "Invalid Selection\n";
else
{
switch (number) //Switch is used when you have more than 10 options.
{
case 1: cout << "Roman numeral: I \n";
break;
case 2: cout << "Roman numeral: II\n";
break;
case 3: cout << "Roman numeral: III\n";
break;
case 4: cout << "Roman numeral: IV\n";
break;
case 5: cout << "Roman numeral: V\n";
break;
case 6: cout << "Roman numeral: VI\n";
break;
case 7: cout << "Roman numeral: VII\n";
break;
case 8: cout << "Roman numeral: VIII\n";
break;
case 9: cout << "Roman numeral: IX\n";
break;
case 10: cout << "Roman numeral: X\n";
break;
}
}
system("pause");
return 0;
}
3. Magic Dates
The date June 10, 1960, is special because when we write it in the following format, the month times the
day equals the year.
6/10/60
Write a program that asks the user to enter a month (in numeric form), a day, and a two digit year. The
program should then determine whether the month times the day is equal to the year. If so, it should
display a message saying the date is magic. Otherwise, it should display a message saying the date is not
magic.
Input
Int month, day, year;
Processing
Day * Month = Year;
Then, Year == Year;
Code:
#include <iostream>
using namespace std;
int main()
{
int day, month, year, magicOrNoMagic;
cout << "Please Enter a Month:\n";
cin >> month;
cout << "Please enter a day:\n";
Output
“The Date is Magic” or “The Date
is Not Magic”
cin >> day;
cout << "Please enter a two digit year:\n";
cin >> year;
magicOrNoMagic = month * day;
if (magicOrNoMagic == year)
cout << "Date is Magic!\n";
else
cout << "Date is Not Magic!\n";
system("pause");
return 0;
}
Input Validation: Think about what legal values the program should accept for month and day.
4. Areas of Rectangles
The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length
and width of two rectangles. The program should then tell the user which rectangle has the greater area,
or if the areas are the same.
Input
Length1, length2, width1,width2,
Processing
A1 = Length1 * Width1;
A2 = Length2* Width2;
#include <iostream>
using namespace std;
int main()
{
int length1, width1, area1, rectangle1;
int length2, width2, area2, rectangle2;
cout << "Please enter width for Rectangle One: ";
cin >> width1;
cout << "Please enter length for Rectangle One: ";
cin >> length1;
area1 = width1 * length1;
cout << "Please enter width for Rectangle Two: ";
Output
Area1, Area2,
Compares 2 rectangles to see if
equal or larger.
cin >> width2;
cout << "Please enter length for Rectangle Two: ";
cin >> length2;
area2 = width2 * length2;
if (area1 == area2)
cout << "Rectangle(One) & Rectangle(Two) are equal!\n";
else if (area1 > area2)
cout << "Rectangle(one) is greater!\n";
else if (area1 < area2)
cout << "Rectangle(two) is greater!\n";
system("pause");
return 0;
}
Download