// Example Program 10 // The main objective of this program is to // give you an idea how to make use of a simple // relational expression to control the execution // of a while loop. // Problem statement: // Write a program that asks the user to enter the // present value of a car and displays the number of // years it takes for the car's trade-in value to be // $500 or below. Assume that the trade-in value of a // car is about 70% of its value one year ago. // Trace the program by hand with present value of // 1000 dollars. #include int main() { double present_value, trade_in_value; int no_of_years = 0; // Promt the user cout << "Please enter the present value of the car:"<> present_value; trade_in_value = present_value; while (trade_in_value > 500) { trade_in_value = 0.7 * trade_in_value; ++ no_of_years; cout << no_of_years << " " << trade_in_value << endl; } cout << "The car will have the trade-in value 500.00 " << " or less in " << no_of_years << " years" << endl; return 0; } // Example 11. // The main objective of this example program // is to demonstrate the use of if statements // in a program // Problem statememt: Write a program to determine // the average score, and the number A's in a class. // Input: Input is a file ("score.in") containing the // student ID and the test score for each student in // the class. // Output: The program displays on the screen // the number of students, the average score, and // the number of A's. #include #include int main() { int student_id; int test_score; int count = 0, a_count = 0; double total = 0.0, average_score; ifstream infile; infile.open("score.in"); infile >> student_id >> test_score; while (infile) { ++ count; total = total + test_score; if (test_score >= 90) ++ a_count; infile >> student_id >> test_score; } average_score = total / count; cout << "The number of students in the class: " << count << endl; cout << "The average score of the class: " << average_score << endl; cout << "The number of A's : "<< a_count; infile.close(); return 0; } // Example 12. // The main objective of this example program // is to demonstrate the use of if-else statements // in a program // Problem statememt: Write a program to determine // the letter grade of a student in a class and also // the number A's, B's, C's, D's, and F's. // Input: Input is a file ("score.in") containing the // student ID and the test score for each student in // the class. // Output: The program creates an output file ("grade.out") // with student ID's and letter grades and also the program // saves the number of A's, B's, C's, D's, and F's in the // same output file. #include #include int main() { int student_id; int test_score; int a_count = 0, b_count = 0, c_count = 0; int d_count = 0, f_count = 0; ifstream infile; ofstream outfile; infile.open("score.in"); outfile.open("grade.out"); outfile << "Student ID Grade"<> student_id >> test_score; while (infile) { outfile << " "<< student_id<<" "; if (test_score >= 90) { ++ a_count; outfile << 'A' << endl; } else if(test_score >= 80) { ++ b_count; outfile << 'B' << endl; } else if(test_score >= 70) { ++ c_count; outfile << 'C' << endl; } else if(test_score >= 60) { ++ d_count; outfile << 'D' << endl; } else { ++ f_count; outfile << 'F' << endl; } infile >> student_id >> test_score; } outfile << "-------------------"< #include int main() { int month, day, year, calc_day, weekday; ifstream infile; ofstream outfile; infile.open("inday.dat"); outfile.open("outday.dat"); outfile << "This program determines the day of the" << " week for any given date"<> month >> day >> year; while (infile) { // Change month for January or February // and change year to previous year if (month < 3) { month = month + 12; -- year; } calc_day = day + 2*month + (3*(month+1)/5) + year + year/4 - year/100 + year/400 + 1; weekday = calc_day % 7; // Undo the changes for month and year // for January and Februay if (month > 12) { month = month - 12; ++ year; } outfile <<"The date " << setw(2)<> month >> day >> year; } infile.close(); outfile.close(); return 0; }