include iostream using namespace std int main for unsigned int count 1 count 10

Include iostream using namespace std int main for

This preview shows page 20 - 24 out of 29 pages.

#include <iostream> using namespace std; int main() { for ( unsigned int count = 1; count <= 10; ++count ) // loop 10 times { cout << "1. count = " << count << endl; if ( count == 5 ) // if count is 5, continue ; // skip remaining code in loop cout << "2. count = " << count << endl; //other statements can be placed here } // end for cout << "\nUsed continue to skip printing 5" << endl; } /* 1. count = 1 2. count = 1 1. count = 2 2. count = 2 1. count = 3 2. count = 3 1. count = 4 2. count = 4 1. count = 5 1. count = 6 2. count = 6 1. count = 7 2. count = 7 1. count = 8 2. count = 8 1. count = 9 2. count = 9 1. count = 10 2. count = 10 Used continue to skip printing 5 */ 20
Image of page 20
Lesson 05 – Sample Programs //SP5_17.cpp // Summing integers with the for statement. #include <iostream> using namespace std; int main() { int total = 0; // initialize total // total even integers from 2 through 20 for ( int number = 2; number <= 20; number += 2 ) { cout << "number = " << number << endl; total += number; } cout << "Sum is " << total << endl; // display results return 0; // successful termination } /* number = 2 number = 4 number = 6 number = 8 number = 10 number = 12 number = 14 number = 16 number = 18 number = 20 Sum is 110 */ 21
Image of page 21
Lesson 05 – Sample Programs //SP5_18.cpp //This program finds the average rainfall for a week. // Input: Rainfall, in inches, for each day of one week // Output: Prints the seven rainfall amounts and their average #include <iostream> #include <iomanip> using namespace std; int main() { double rainfall; // Daily rainfall amount double sum = 0.0; // Accumulates sum of seven rainfall amounts double average; // Average of seven rainfall amounts int counter; // Loop control variable const int DAYS_IN_WEEK = 7; // Constant number of days in a week // This is the work done in the detailLoop() function // Loop for seven days for (counter = 1; counter <= DAYS_IN_WEEK; counter++) { cout << "Enter rainfall amount for Day: " << counter << ": " ; cin >> rainfall; cout << "Day " << counter << " rainfall amount is " << rainfall << " inches" << endl; sum += rainfall; } // Calculate average average = sum / DAYS_IN_WEEK; cout << fixed << setprecision(4); // This is the work done in the endOfJob() function // Output average cout << "\nAverage Rainfall amount is " << average << endl; return 0; } /* Enter rainfall amount for Day: 1: .2 Day 1 rainfall amount is 0.2 inches Enter rainfall amount for Day: 2: 0 Day 2 rainfall amount is 0 inches Enter rainfall amount for Day: 3: .5 Day 3 rainfall amount is 0.5 inches Enter rainfall amount for Day: 4: .8 Day 4 rainfall amount is 0.8 inches Enter rainfall amount for Day: 5: .1 Day 5 rainfall amount is 0.1 inches Enter rainfall amount for Day: 6: 0 Day 6 rainfall amount is 0 inches Enter rainfall amount for Day: 7: 0 Day 7 rainfall amount is 0 inches Average Rainfall amount is 0.2286 */ 22
Image of page 22
Lesson 05 – Sample Programs //SP5_19.cpp //Square Display //Range checking //for loop and while loop #include <iostream> using namespace std; int main() { int num; // Get a positive integer in the range of 1 - 15. cout << "Enter a positive integer in the range " << "of 1 - 15: " ; cin >> num; // Validate the input. while (num < 1 || num > 15) { cout << "ERROR: Invalid number.\n" ; cout << "Enter a positive integer in the range " << "of 1 - 15: " ; cin >> num; } // Display the square made of Xs.
Image of page 23
Image of page 24

You've reached the end of your free preview.

Want to read all 29 pages?

  • Fall '15
  • Marinolent

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture