EECE 230 Homework Chapter 1- Multiple Problems, Spring 2015

American University of Beirut · Introduction to Programming · 2 pages · Dr. Wassim Masri

All EECE 230 files
EECE 230 Homework Chapter 1- Multiple Problems, Spring 2015, page 1

Stuck on a question? A tutor can walk you through it.

EECE 230 Homework Chapter 1- Multiple Problems, Spring 2015, page 2

More EECE 230 files

Show the text of this chapter
Assignment 1 - Basics You must use the debugger 1) Type in the following code and insert breakpoints at the lines designated with a *. Run the program with n = 2, 5, 7, 10, 12 and at each breakpoint examine the values of n, sum, and i. Get familiar with the debugger menu “Debug” and the debugger shortcuts: F5, F9, F10 etc. #include <iostream> using namespace std; void main() { int n; cout << "Please enter a number between 5 and 10" << endl; cin >> n; * if ( (n < 5) || (n > 10)) { * cout << "You entered an out of range value. Bye Bye..." << endl; return; } int sum = 0; for (int i = 0; i < n; i++) { * sum = sum + i; } * cout << "For n = " << n << " sum = " << sum << endl; *} 2) Write a program that: a) Asks the user to enter the x and y coordinates of two points. b) Computes the distance between them. c) Displays the coordinates and the computed distance. You can use the “double sqrt(double x)” function by including <cmath> • sqrt(x) calculates the non-negative square root of x, for x >= 0.0 • For example sqrt(2.25) is 1.5 Below is a sample output: This program prints the distance between two points Enter the x and y coordinates of the first point: 3 7 Enter the x and y coordinates of the second point: 9 3 The distance between (3, 7) and (9, 3) is 7.2111 1) Write a program to calculate the monthly payments you would have to make over a given number of years to pay off a loan at a given interest rate compounded continuously. Your program should read the number of years t, the principal P, and the annual interest rate r. The desired value is given by the formula P.ert/(t*12). Test your program with a $20,000 loan over 5 years at interest rates 5%, 5.5%. 6%, 6.5% and 7%. 2) Write a program that takes a date as input and prints the day of the week that date falls on. Your program should take three inputs: m (month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the Gregorian calendar: y0 = y - (14 - m) / 12 x = y0 + y0/4 - y0/100 + y0/400 m0 = m + 12 * ((14 - m) / 12) - 2 d0 = (d + x + (31*m0)/ 12) mod 7 For example, on what day of the week was August 2, 1953? y = 1953 - 0 = 1953 x = 1953 + 1953/4 - 1953/100 + 1953/400 = 2426 m = 8 + 12*0 - 2 = 6 d = (2 + 2426 + (31*6) / 12) mod 7 = 2443 mod 7 = 0 (Sunday) 3) Write a program that does cost calculation of trips. These costs include: Air Cost, Lodging, Meal, and Miscellaneous Cost (park entrance fee, local transportation etc) as follows: Everyday cost = lodging cost per day + meal cost per day + Misc Cost Total cost = (Air cost + Everyday cost * Number of days) * Number of travelers In particular, this program will prompt users for their:  Air cost  Lodging cost per day  Meal cost per day  Miscellaneous cost  Number of days  Number of travelers A typical run would look like this: Enter air cost: 471.2 Enter lodging cost per day:60 Enter meal cost per day:50 Enter miscellaneous cost per day: 50 Enter number of days:5 Enter number of travelers:3 Your cost per day per traveler is $160.00 Your total cost for the trip is $3813.60