// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Exercises 1: Using int, string, reading and writing standard input // Goals: First introduction to programming // STUDENT NAME: ¥?¥?¥ // STUDENT ID: ¥?¥?¥ // DATE: ¥?¥?¥ /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "RGBNames.h" #include "Random.h" // Enter project specific include files here as well as classes and functions // that you choose to define in the main shell rather than in separate files // prototypes void Exercise1(); void Exercise2(); void Exercise3(); void Exercise4(); void Exercise5(); void Exercise6(); void Exercise7(); void Exercise8(); void Exercise9(); void Exercise10(); // definitions void Exercise1(){ // Ask user to type in two numbers // Print the product // Use raw read pattern // code starts here cout << "We will compute the product of two numbers." << endl; int n; // declare integer variable n for the 1st number cout << "Type in a number: "; // prompt the user to type in a number cin >> n; // record the typed number as n int m; // declare integer variable m for the 2nd number cout << "Type in a number: "; // prompt the user to type in a number cin >> m; // record the typed number as m cout << "The product is " << n * m << endl; // print the product // code ends here cin.ignore(); }; void Exercise2(){ // Ask user to type in her name and her home town // Print 'Name from Town' // Use raw read pattern. // code starts here cout << "Test input of name and home town using cin." << endl; string name; // declare string object name for person's name cout << "Type in your name: "; // prompt the user to type in her name cin >> name; // record the input as name string town; // declare string object town for the home town cout << "Type in your home town: "; // prompt the user to type in her home town cin >> town; // record the input as town cout << name << " from " << town << endl; // print 'Myname from Mytown' // code ends here }; void Exercise3(){ // Ask the user to type in the length in feet and inches // Print the length in inches // code starts here cout << "Convert feet and inches into total inches." << endl; cout << "Uses Verified Read / IOTools." << endl; int feet; // variable feet to store number of feet feet = RequestInt("How many feet?"); // initialize feet from user input int inches = RequestInt("How many inches?"); // declare and initialize inches from user input int total = feet * 12 + inches; // store the product in variable total cout << "Length in inches is " << total << endl; // print the result // code ends here }; void Exercise4(){ // Ask the user to type in his name and his home town // Print 'Name from Town' // Note: Here you can use names with two or more words // code starts here cout << "Test input of name and town using Verified Read." << endl; string name; // declare string object name for person's name RequestString("Type in your name:", name); // prompt the user to type in her name // record the input as name string town; // declare string object town for the home town RequestString("Type in your home town:", town); // prompt the user to type in her home town // record the input as town cout << name << " from " << town << endl; // print 'Myname from Mytown' // code ends here }; void Exercise5(){ // Ask the user to type in name and age // Print 'Name is xx years old' // code starts here cout << "Input name and age, print a message." << endl; string name; // declare string object name for person's name RequestString("Type in person's name:", name); // prompt the user to type in a person's name // record the input as name int age = RequestInt("Type in this person's age:"); // declare integer variable for person's age // prompt the user to type the age // record the input as age cout << name << " is " << age << " years old." << endl; // print the desired sentence // code ends here }; void Exercise6(){ // Read in how many quarters, dimes, nickles, and pennies are there in a pile // Print how much money is there in dollars and cents // code starts here cout << "Program to compute the total worth of coins." << endl; int quarters = RequestInt("Count the quarters:"); // declare and initialize number of quarters int dimes = RequestInt("Count the dimes:"); // declare and initialize number of dimes int nickels = RequestInt("Count the nickels:"); // declare and initialize number of nickels int pennies = RequestInt("Count the pennies:"); // declare and initialize number of pennies int total = 25 * quarters + 10 * dimes + 5 * nickels + pennies; // compute total amount in cents int dollars = total / 100; // compute and save number of whole dollars int cents = total % 100; // compute the remaining change in cents cout << "There is $" << dollars << "." << cents << " in the pile." << endl; // print the result // code ends here }; void Exercise7(){ // You plan to visit a friend in another town // You know the distance in miles // Figure out how long will you have to drive at a given speed to get there // code starts here cout << "Compute the duration of a trip, given distance in miles" << endl; cout << "and speed in miles per hour." << endl; double distance = RequestDouble("How far are you traveling?"); // distance traveled int speed = RequestInt("What do you think will be your average speed?"); // average speed double timeInHours = distance/speed; // time needed in hours // and fractions of hours int hours = timeInHours; // whole hours needed int minutes = (distance - hours * speed)/speed*60; // compute the remaining time // and convert to minutes // *** observe left to right rule of arithmetic cout << "You will travel for " << hours << " hours and " << minutes << " minutes." << endl; // code ends here }; void Exercise8(){ // Your hike description gives you the starting and ending elevation (in feet) // and the length of the hike (one way) in miles // Print how many feet did you climb for each mile of the hike // code starts here cout << "Compute average rate of climb on a hike, " << endl; cout << "given the starting and eneding elevation in feet " << endl; cout << "and the length of the hike in miles (including fractions of a mile)." << endl; // next three lines are self-explanatory through choice of names and prompts int start = RequestInt("Starting elevation (in feet):"); int finish = RequestInt("Final elevation (in feet):"); double length = RequestDouble("Length of the hike (in miles):"); int rise = finish - start; // compute the height of the climb double rate = rise / length; // compute the rate of climb cout << "On average, you will climb " << rate << " feet per mile." << endl; // code ends here }; void Exercise9(){ // You are going to compute your gross pay for the past week // Hours over 40 are paid at 1.5 times the regular hourly rate // // Note: This program uses a built-in function m = max(a, b); // This function assigns to the variable m the value of the larger // of the two quantities a or b // code starts here cout << "Computes pay, given hours and minutes worked and the hourly pay rate." << endl; cout << "Overtime over 40 hours is paid at 1.5 times hourly rate." << endl; // next three lines are self-explanatory through choice of names and prompts int hours = RequestInt("How many hours did you work?"); int minutes = RequestInt("and how many more minutes?"); double rate = RequestDouble("Enter your hourly wage:"); int totalMinutes = hours * 60 + minutes; // compute the overtime // if totalMinutes - 40 hours * 60 minutes is >0, this is the overtime // if totalMinutes - 40 hours * 60 minutes is <=0, there is no overtime int overTime = max (0, totalMinutes - 40 * 60); // multiply by 100 to get pay in cents // divide by 60 because rate is hourly and time is in minutes int payInCents = 100 * (rate * totalMinutes + 0.5 * rate * overTime) / 60; // compute whole dollars and the remaining cents int dollars = payInCents / 100; int cents = payInCents % 100; // rpint the result cout << "You earning are $" << dollars << "." << cents << " ." << endl; // code ends here }; void Exercise10(){ // Type in the race result as minutes and seconds // Type in the race distance in miles (with decimals) // Print average speed per mile // code starts here cout << "Computes average time to run a mile in a race, " << endl; cout << "given the race distance in miles (with decimals) " << endl; cout << "and the finish time in minutes and seconds." << endl; int minutes = RequestInt("Minutes:"); int seconds = RequestInt("Seconds:"); double miles = RequestDouble("Race length in miles:"); int timeInSeconds = minutes * 60 + seconds; double rate = timeInSeconds/miles; // compute the rate in seconds per mile int rateInMinutes = rate/60; // convert to record whole minutes int rateInSeconds = rate - rateInMinutes*60; // and the remaining seconds cout << "You ran the race at the rate of " << rateInMinutes << ":"; cout << rateInSeconds << " per mile." << endl; // code ends here }; int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows InitializeConsole(); // Build graphics window 0 // GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 // ConsolePlaceBelow(0); // Give the console the focus for user interaction // MakeConsoleForeground(); ////////// // Enter the main program here int i = 1; while (Confirm("Another exercise?", true)){ // loop to run another exercise i = RequestInt("Exercise number: ", i); // determine which exercise to run // the default moves you to the next one cout << endl << "Exercise " << i << endl; // notify user of the accepted selection switch (i){ // un the appropriate exercise case 1: { Exercise1(); break; } case 2: { Exercise2(); break; } case 3: { Exercise3(); break; } case 4: { Exercise4(); break; } case 5: { Exercise5(); break; } case 6: { Exercise6(); break; } case 7: { Exercise7(); break; } case 8: { Exercise8(); break; } case 9: { Exercise9(); break; } case 10: { Exercise10(); break; } default: Exercise1(); }; i = (i+1); // next time, continue with the next exercise if (i > 10) // we only have 10 exercises - cycle through again i = 1; cout << endl; } // done ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; }