Lab 7: Repetition Structure 1-While Loop A) Counter Controlled Example 1
Lab 7: Repetition Structure 1-While Loop A) Counter Controlled Example 1
// This program demonstrates a simple while loop. // This program uses a while loop to display Hello 5 times. #include <iostream> using namespace std; int main() { int number = 1; while (number <= 5) { cout << "Hello "; number++; } cout << "\nThat's all!\n"; return 0; }
Example 2
// This program uses a while loop to display the numbers 1-5 // and their squares. #include <iostream> #include <iomanip> using namespace std; int main() { int num = 1; cout << "Number Square\n"; cout << "--------------\n"; while (num <= 5) { cout << setw(4) << num << setw(7) << (num * num) << endl; num++; // Increment counter } return 0; }
Question 1: Write a program that lets the user enter a number. The number should be multiplied by 2 and print until the number exceeds 50. Use a while loop.
// Counter telling what number to square // The final integer value to be squared
// Get and validate the last number in the table cout << "This program will display a table of integer\n" << "numbers and their squares, starting with 1.\n" << "What should the last number be?\n" << "Enter an integer between 2 and 10: "; cin >> lastNum; while ((lastNum < 2) || (lastNum > 10)) { cout << "Please enter an integer between 2 and 10: "; cin >> lastNum; } // Display the table cout << "\nNumber Square\n"; cout << "--------------\n"; num = 1; // Set the counter to the starting value while (num <= lastNum) { cout << setw(4) << num << setw(7) << (num * num) << endl; num++; // Increment the counter } return 0; }
Question 2: Write a program with a while loop that displays whether a user-entered integer is even or odd.The program should ask the user if he or she wants to test another number. The loop should repeat so long as the user enters Y or y.
// Game counter // Holds number of points for a specific game // Accumulates total points for all games
in the points for game 1 "Enter the number of points your team has earned\n"; "so far this season. Then enter -1 when finished.\n\n"; "Enter the points for game " << game << ": "; points;
// Loop as long as the end sentinel has not yet been entered while (points != -1) { // Add point just read in to the accumulator total += points; // Enter the points for the next game cout << "Enter the points for game " << ++game << ": "; cin >> points; } // Display the total points cout << "\nThe total points are " << total << endl; return 0; }
Question 3 : Write a program using a sentinel controlled while loop that accumulates a series of test scores input by the user, until -99 is entered. The program should then report how many scores were entered and the average of these scores. Do not count the end sentinel -99 as a score.
Question 4 Write a program using a flag-controlled loop that continues to read pairs of integers until it reads a pair with the property that the first integer in the pair is the same as the second. If the integers are stored in variables m and n, your loop should display each value of m and n.
Question 5 : Write a program using a EOF controlled while loop to determine the largest and the smallest integers entered. 2- do..while loop 3- for loop Question 6: If possible, rewrite the programs for Question 1- 5 using do..while loop and for loop 4- Nested loop Example:
// This program demonstrates the logic of nested loop. #include <iostream> #include <iomanip> using namespace std; int main() { cout << setw(12)<<"row"<<setw(6)<<"col"<<endl; for (int row = 0; row < 3; row++) //Outer loop { cout<<"Outer"<<setw(7)<<row<<endl; for (int col = 0; col < row; col++) //Inner loop { cout<<"Inner"<<setw(10)<<col<<endl; } //End inner loop cout << endl; } //End outer loop return 0; }
Question 7: Write a nested loop that displays the following output: 1 12 123 1234 12345 6- continue statement Example :
#include <iostream> using namespace std; int main( ) { int value = 0; while (value < 10) { value++; if (value == 4) continue; cout <<"Value = "<<value<<endl; } }
ANSWER EXERCISE LAB 7 Question 1: Write a program that lets the user enter a number. The number should be multiplied by 2 and print until the number exceeds 50. Use a while loop. Answer :
#include <iostream> using namespace std; int main() { int num; cout<<"Enter an integer : "<<endl; cin >> num; num *=2; while (num <= 50) { cout << num << endl; num *= 2; } }
Question 2: Write a program with a while loop that displays whether a user-entered integer is even or odd.The program should ask the user if he or she wants to test another number. The loop should repeat so long as the user enters Y or y. Answer :
// This program determines a use entered integer whether it is EVEN or ODD. // It uses a while loop that allows the code to repeat as many times as the // user wishes. #include <iostream> using namespace std; int main() { int num; char again='y'; while (again == 'Y' || again == 'y') { cout << "Enter a number: "; cin >> num ; if (num%2==0) cout<<"EVEN number"<<endl; else cout<<"ODD number"<<endl; cout << "Do you wish to do this again? (Y/N) "; cin >> again; }
return 0; }
Question 3 : Write a program using a sentinel controlled while loop that accumulates a series of test scores input by the user, until -99 is entered. The program should then report how many scores were entered and the average of these scores. Do not count the end sentinel -99 as a score. Answer:
#include <iostream> using namespace std; int main() { int count = 0, scores, total = 0; // Read cout << cout << cin >>
// test score counter // Holds number of test score // Accumulates total scores for all test scores
in the score for test 1 "Enter the test scores. "; "(enter -99 when finished).\n\n"; scores;
// Loop as long as the end sentinel has not yet been entered while (scores != -99) { // Add score just read in to the accumulator total += scores; count++; // Enter the score for the next test cin >> scores; } // Display the total scores cout << "\nThe number of test scores entered are " << count << endl; cout << "\nThe average of the scores is " << total/count << endl<<endl; return 0; }
Question 4 Write a program using a flag-controlled loop that continues to read pairs of integers until it reads a pair with the property that the first integer in the pair is the same as the second. If the integers are stored in variables m and n, your loop should display each value of m and n. Answer:
// This program illustrates the use of an end sentinel. It calculates // the total number of points a soccer team has earned over a series // of games. The user enters the point values, then -1 when finished. #include <iostream> using namespace std; int main() { int m,n; bool found; found=false; while (!found) { cout<<"Enter two integers : "; cin>>m>>n; cout<<"The integers are : " << m <<' '<< n << endl; if ( m == n ) { found = true; cout <<"\nSTOP. Mission Accomplish. The same pair of integers has been found.\n"; } } return 0; }
Question 5 : Write a program using a EOF controlled while loop to determine the largest and the smallest integers entered. Answer:
#include <iostream> using namespace std; int main( ) { int n,smallest,largest; cout << "The program will determine the largest and the smallest integer numbers."<< endl; cout << "Enter your integer numbers (press ^Z to stop) : "; cin >> n; smallest = n; largest = n;
while (cin >> n) { if (n > largest) largest = n; else if (n < smallest) smallest = n; } cout <<"Largest = "<< largest << endl; cout <<"Smallest = "<< smallest << endl; }
Question 6: If possible, rewrite program for Question 1- 5 using do..while loop and for loop No answers provided. Try on your own. Question 7: Write a nested loop that displays the following output 1 12 123 1234 12345 Answer :
#include <iostream> using namespace std; int main() { for (int row = 1; row <= 5; row++) //Outer loop { for (int col = 1; col <= row; col++) //Inner loop { cout << col; } //End inner loop cout << endl; } //End outer loop return 0; }
10
2. Insert a few integer numbers between -100 and 100. Determine and print how many negative numbers and how many positive numbers have been inserted. Insert the value 0 to stop inserting. 3. Let the user enter the total number of students, and each students score (use round number). Then use the for loop to determine his/her grade using the following grading scheme. 80 - 100 : A 70 - 79 : B 60 - 69 : C 50 - 59 : D 0 - 49 : E Print all the scores and their grades.
4. A particular talent competition has five judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3, are allowed. A performers final score is determined by dropping the highest and lowest score received, then averaging the three remaining scores. Write a program that uses this method to calculate a contestants score. It should include the following functions : double getJudgeData() should ask the user for a judges score, validate the score, and return a valid score. This function should be called by function main()for each of the five judges. void calcScore() should calculate and display the average of the three scores that remain after dropping the highest and the lowest scores the performer received. This function should be called just once by function main(), and should be passed the five scores. The last two functions, described below, should be called by function calcScore(), which uses the returned information to determined which of the scores to drop. int findLowest() should find and return the lowest of the five scores passed to it. int findHighest() should find and return the highest of the five scores passed to it.
11