Allaboutbonnietran.files.wordpress.com



Bonnie Tran, 3/18/2016Programming Challenges chapter 5 Solution1. Characters for the ASCII CodesWrite a program that uses a loop to display the characters for each ASCII code 32 through127. Display 16 characters on each line with one space between characters.Input Process Output For(int i = 32; I <= 127, i++){Cout << i << (char)i;}A table #include <iostream>#include <iomanip>#include <string>using namespace std;int main(){string line;cout << setw(6) << "DEC" << setw(6) << "CHAR" << endl;line.assign(12,'*');cout << line << endl;for (int i = 32; i <= 127; i++){cout << setw(6) << i << setw(6) << (char)i << endl;}system("pause");return 0;}2. Sum of NumbersWrite a program that asks the user for a positive integer value and that uses a loop to validate the input. The program should then use a second loop to compute the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will ?nd the sum of 1, 2, 3, 4, … 50.Input Process Output Int userNum;Int total;While(invalid){ Input valid number}For(start at one; less than equal to userNum; increment num){ Total = total + num;}Total from 1 to userNum is total.#include <iostream>using namespace std;int main(){//Declare variables.int userNum;int total = 0;//Get input values.cout << "Enter a positive integer number. ";cin >> userNum;//Validate input with loop.while (userNum > 0){for (int num = 1; num <= userNum; num++){total += num;}cout << "The total of all the numbers from 1 to " << userNum << " is " << total << "." << endl;cout << "Enter a positive integer number. ";cin >> userNum;}system("pause");return 0;}3. Distance TraveledThe distance a vehicle travels can be calculated as follows:distance = speed * timeFor example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.Write a program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. It should then use a loop to display the total distance traveled at the end of each hour of that time period. Here is an example of the output:What is the speed of the vehicle in mph? 40How many hours has it traveled? 3Hour Miles Traveled--------------------------------1 402 803120Input Process Output Int speed, hours;For(int i = 1; i <=hours; hours++){Distance = speed * i;Cout << i << distance;}Int distance;#include <iostream>#include <iomanip>using namespace std;int main(){//Declare variables.int speed, hours, distance;char again;do{//Get input values.cout << "How many miles per hour did you travel? " << endl;cin >> speed;cout << "How many hours did you travel for? " << endl;cin >> hours;//process.cout << setw(4) << "hours" << setw(11) << "distance" << endl;cout << "---------------" << endl;for (int i = 1; i <= hours; i++){//Display.cout << setw(3) << i << setw(9) << (speed * i) << endl;}cout << "Would you like to do it again? Enter(Y / N)" << endl;cin >> again;} while (toupper(again) == 'Y');system("pause");return 0;}4. Celsius to Fahrenheit TableIn one of the Chapter 3 Programming Challenges you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures from 0 to 20 and their Fahrenheit equivalents.F = 9/5C + 32Input Process Output Double F;For(double C = 0; C < 21; C++){F = 9/5C + 32Cout << display C and F temperature}F/C table#include <iostream>#include <iomanip>using namespace std;int main(){//Declare Variables.double F;//Process.cout << setw(14) << "TEMPERATURE" << endl;cout << "................." << endl;cout << setw(5) << "C" << setw(6) << "F" << endl;cout << "-----------------" << endl;for (double C = 0; C < 21; C++){F = (9.0 / 5) * C + 32;//Display.cout << fixed << showpoint << setprecision(1);cout << setw(5) << C << setw(7) << F << endl;}system("pause");return 0;} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download