// 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. /////////////////////////////////////////////////////////////////////////// // Exercise Set 1: Using int, string, reading and writing standard input // Goals: First introduction to programming /////////////////////////////////////////////////////////////////////////// // 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" // robust input and formatted output #include "Graphics.h" // windows, graphics, color, text, & tools #include "Random.h" // random numbers #include "Delay.h" // delays #include "Hex.h" // hex output for pointers #include "VectorMatrix.h" // double precision arrays #include "FileTool.h" // file input-output and file dialog boxes // 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 first number is: " << n << endl; // print the first number cout << "The second number is: " << m << endl; // print the second number 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 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 << "Assume feet and inches are given as whole numbers." << endl; cout << "Uses Verified Read / IOTools." << endl; int feet; // variable feet to store // the 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 to type in her name // record the input as name string town; // declare string object town // for the home town // prompt the user to type in home town and record the input as town RequestString("Type in your home town:", 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 // prompt the user to type in a person's name and record the input as name RequestString("Type in person's name:", name); // declare integer variable for person's age // prompt the user to type the age and record the input as age int age = RequestInt("Type in this person's age:"); // print a message about the name and age cout << name << " is " << age << " years old." << endl; // 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; // declare and initialize int quarters = RequestInt("Count the quarters:"); // number of quarters int dimes = RequestInt("Count the dimes:"); // number of dimes int nickels = RequestInt("Count the nickels:"); // number of nickels int pennies = RequestInt("Count the pennies:"); // number of pennies // compute total amount in cents int total = 25 * quarters + 10 * dimes + 5 * nickels + pennies; // compute and save number of whole dollars in the pile int dollars = total / 100; // compute and save the worth of the remaining change in cents int cents = total % 100; // print the result cout << "There is $" << dollars << "." << cents << " in the pile." << endl; // 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; // determine the distance to travel double distance = RequestDouble("How far are you traveling?"); // determine the average speed double speed = RequestDouble("What will be your average speed?"); // time needed in hours and fractions of hours double time = distance/speed; // record how many whoule hours are needed int hours = time; // compute the remaining fraction of hour needed for the trip time = time - hours; // convert to minutes int minutes = time*60; // print the results cout << "You will travel for " << hours << " hours and "; cout << 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 and " << endl; cout << "the length of the hike in miles (including fractions of a mile)."; cout << 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 int 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 "; cout << "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:"); // total minutes worked int totalMinutes = hours * 60 + minutes; // regular fulltime 40 hours * 60 minutes int fulltime = 40 * 60; // compute the overtime // if totalMinutes - fulltime is >0, this is the overtime // if totalMinutes - fulltime is <=0, there is no overtime int overTime = max (0, totalMinutes - fulltime); // compute the pay rate in cents per minute double rateInCentsPerMinute = (100 * rate)/60; // get total pay in cents int payInCents = (totalMinutes + 0.5 * overTime) * rateInCentsPerMinute; // compute whole dollars and the remaining cents int dollars = payInCents / 100; int cents = payInCents % 100; // print the result cout << "You earning are $" << dollars << "." << cents << " ." << endl; // code ends here }; void Exercise10(){ // Type in the race result as hours, 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 hours, minutes and seconds." << endl; int hours = RequestInt("Hours:"); int minutes = RequestInt("Minutes:"); int seconds = RequestInt("Seconds:"); double miles = RequestDouble("Race length in miles:"); // compute how many seconds did the runner run int timeInSeconds = (hours * 60 + minutes) * 60 + seconds; // compute the rate in seconds per mile double rate = timeInSeconds/miles; // convert to record whole minutes int rateInMinutes = rate/60; // and the remaining seconds int rateInSeconds = rate - rateInMinutes*60; 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(); ////////// try { // Enter the main action here within the try clause // loop to run another exercise while (Confirm("Another exercise?", true)){ // determine which exercise to run // the default moves you to the next one i = RequestInt("Exercise number: ", i); // notify user of the accepted selection cout << endl << "Exercise " << i << endl; // run the appropriate exercise switch (i){ 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(); }; // next time, continue with the next exercise i = (i+1); // we only have 10 exercises - cycle through again if (i > 10) i = 1; cout << endl; } // done } catch (string& error_message) { cout << "error: " << error_message << endl << endl; } catch (...) { cout << "error: unknown exception" << endl << endl; } ////////// // 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; }