Chapter 3.11 - Programming Projects
3.4: Average Rainfall Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: The average rainfall for June, July, and August is 6.72 inches. SAMPLE RUN #1: ./ETest Enter month:May Enter rainfall for May:15 Enter month:June Enter rainfall for June:8.333333333 Enter month:July Enter rainfall for July:1.0000000 The average rainfall for May, June, and July is 8.11 inches.
#include <iostream> #include <iomanip> using namespace std; int main() { double rain1, rain2, rain3; char month1[15], month2[15], month3[15]; cout << "Enter month: "; cin >> month1; cout << "Enter rainfall for " << month1 << ": "; cin >> rain1; cout << "Enter month: "; cin >> month2; cout << "Enter rainfall for " << month2 << ": "; cin >> rain2; cout << "Enter month: "; cin >> month3; cout << "Enter rainfall for " << month3 << ": "; cin >> rain3; cout << "The average rainfall for " << month1 << ", " << month2 << ", and " << month3 << " is " << setprecision(2) << showpoint << fixed << (rain1+rain2+rain3)/3.0 << " inches." << endl; return 0; }
3.18: Pizza Pi Joe's Pizza Palace needs a program to calculate the number of slices a pizza of any size can be divided into. The program should perform the following steps: Ask the user for the diameter of the pizza in inches. Calculate the number of slices that may be taken from a pizza of that size. Display a message telling the number of slices. To calculate the number of slices that may be taken from the pizza, you must know the following facts: Each slice should have an area of 14.125 inches. To calculate the number of slices, simply divide the area of the pizza by 14.125. The area of the pizza is calculated with this formula: Area = "pi r squared" where pi is approximately 3.14159 and r is the radius (half the the diameter). SAMPLE RUN #1: ./ETest Enter pizza diameter:10 5 slices
#include <iostream> using namespace std; int main () { double pizzaDiameter; int numSlices; double area; cout << "Enter pizza diameter: " ; cin >> pizzaDiameter; area = (pizzaDiameter/2.0) * (pizzaDiameter/2.0) * 3.14159; numSlices = area/14.125; cout << numSlices << " slices "; }
3.22: Word Game Write a program that plays a word game with the user. The program should ask the user to enter the following: His or her name The name of a city His or her age The name of a college A profession A type of animal A pet's name After the user has entered these items, the program should display the following story, inserting the user's input into the appropriate locations: There once was a person named href="asfunction:_root.doTutor,7,CPP">NAME who lived in CITY. At the age of AGE, href="asfunction:_root.doTutor,7,CPP">NAME went to college at COLLEGE. href="asfunction:_root.doTutor,7,CPP">NAME graduated and went to work as a PROFESSION. Then, href="asfunction:_root.doTutor,7,CPP">NAME adopted a(n) ANIMAL named PETNAME. They both lived happily ever after! SAMPLE RUN #1: ./ETest Enter name:Al Smith Enter city:Brooklyn Enter age:43 Enter college:School of Hard Knocks Enter profession:Politician Enter animal:Pitbull Enter pet name:Toodles There once was a person named Al Smith who lived in Brooklyn. At the age of 43, Al Smith went to college at School of Hard Knocks. Al Smith graduated and went to work as a Politician. Then, Al Smith adopted a(n) Pitbull named Toodles. They both lived happily ever after!
#include <iostream> using namespace std; int main() { string userName; string userCity; int userAge; string userCollege; string userProfession; string userAnimal; string userPetName; cout << "Enter name: "; getline(cin,userName); cout << "Enter city: "; getline(cin,userCity); cout << "Enter age: "; cin >> userAge; cout << "Enter college: "; cin.ignore(100,'\n'); getline(cin,userCollege); cout << "Enter profession: "; getline(cin,userProfession); cout << "Enter animal: "; getline(cin,userAnimal); cout << "Enter pet name: "; getline(cin,userPetName); cout << "\n\nThere once was a person named " << userName << " who lived in " << userCity << ".\n"; cout << "At the age of " << userAge << ", " << userName << " went to college at " << userCollege << "." << endl; cout << userName << " graduated and went to work as a " << userProfession << ".\n"; cout << "Then, " << userName << " adopted a(n) " << userAnimal << " named " << userPetName << ".\n"; cout << "They both lived happily ever after!" << endl; return 0; }