530 likes | 826 Views
Chapter 4 - Loops. Chapter Objectives Introduce the concept of executing code more than one time Introduce the concept of For Loops Introduce the concept of While Loops Introduce the concept of Do Loops Reinforce condition evaluation presented in previous chapter Introduce nested loops.
E N D
Chapter 4 - Loops • Chapter Objectives • Introduce the concept of executing code more than one time • Introduce the concept of For Loops • Introduce the concept of While Loops • Introduce the concept of Do Loops • Reinforce condition evaluation presented in previous chapter • Introduce nested loops The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops • Chapter Objectives • Introduce the concept of executing code more than one time • Introduce the concept of For Loops • Introduce the concept of While Loops • Introduce the concept of Do Loops • Reinforce condition evaluation presented in previous chapter • Introduce nested loops • Another important characteristic of a powerful programming language is the ability to perform a statement or series of statements over and over again. The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops • 4.1 For Loops • We usually select a for loop when the problem is easily definable into the following distinct sections: • a starting condition(s) • a condition when to stop • a statement(s) to execute as the body of the loop • an increment statement. • The for loop is very versatile with lots of options. This does not have to be confusing. The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops • 4.1 For Loops • The following is a template to use for the for loop: • for(initial expression; looping condition; looping statement) • body of loop; • Follow this sequence for successful evaluation of a for loop: • 1. The initial expression is evaluated. It sets the starting value for the loop and can have more than one value if a comma is used to separate the statements. • 2. The looping condition is evaluated. If false, the loop terminated. • 3. The body of the loop executes. • 4. The looping statement is evaluated. This generally changes a variable that stores the looping index or iteration. • 5. Go to step 2 • Programmer's Notebook • Be aware that a loop may not execute the body at all, or for an infinite amount of time if the loop expression is either initially false or always true. The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Example outputing #’s 1-10 #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; //counter to track the # from 1-10 //Loop from 1 to 10, outputting each on a separate line for(LoopCounter=1; LoopCounter<=10; LoopCounter++) cout<<LoopCounter<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-1 #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=0; LoopCounter<10; LoopCounter++) cout<<++LoopCounter<<endl; } Output: 1 3 5 7 9 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-2 #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=0; LoopCounter<10; LoopCounter++) cout<<LoopCounter++<<endl; } Output: 0 2 4 6 8 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-3 #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=0; LoopCounter++<10; LoopCounter++) cout<<LoopCounter<<endl; } Output: 1 3 5 7 9 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-4 #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=0; ++LoopCounter<10; LoopCounter++) cout<<LoopCounter<<endl; } Output: 1 3 5 7 9 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Multiple Initial Values For loops may require more than one initial statement. While the additional initialization could be written directly before the for loop, it makes more sense to include them within it. By adding a comma in between statements, multiple statements can be listed and are executed in order. See the previous code rewritten with the shortcut added: #include <stdlib.h> #include <iostream.h> void main() { int Sum; int LoopCounter; for(LoopCounter=1, Sum=0; LoopCounter <= 10; LoopCounter++) Sum+=LoopCounter; //note we also added the += operator cout<<Sum; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Multiple Initial Values //Example adding all #'s between LowNum & HighNum #include <stdlib.h> #include <iostream.h> void main() { int LowNum,HighNum; int Sum; int LoopCounter; cout<<"Enter the lower number"<<endl; cin>>LowNum; cout<<"Enter the higher number"<<endl; cin>>HighNum; for(LoopCounter=LowNum, Sum=0;LoopCounter<=HighNum;LoopCounter++) Sum+=LoopCounter; cout<<Sum; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Multiple Initial Values //Example outputting all #'s between LowNum & HighNum and their squares #include <stdlib.h> #include <iostream.h> void main() { int LowNum,HighNum; int LoopCounter; cout<<"Enter the lower number"<<endl; cin>>LowNum; cout<<"Enter the higher number"<<endl; cin>>HighNum; for(LoopCounter=LowNum;LoopCounter<=HighNum;LoopCounter++) cout<<LoopCounter<<'\t'<<LoopCounter*LoopCounter<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Poorly Formatted Loops It is really possible to write the entire for loop inside one statement, although this leads to extremely unreadable code. #include <stdlib.h> #include <iostream.h> void main() { for (int LoopCounter=0;++LoopCounter<10;cout<<LoopCounter<<endl,LoopCounter++); } It is also possible to have an empty loop expression. This would create an infinite loop, since no condition is specified. for(;;) The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Grouping Statements Just as in conditional statements, statements within a set of curly braces are treated as one unit. #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=0; LoopCounter<5; LoopCounter++) { cout<<"This is the first statement when LoopCounter = " << LoopCounter <<endl; cout<<"This is the second statement when LoopCounter = " << LoopCounter <<endl; } cout<<"This is the statement outside the loop"<<endl; } Output: This is the first statement when LoopCounter = 0 This is the second statement when LoopCounter = 0 This is the first statement when LoopCounter = 1 This is the second statement when LoopCounter = 1 This is the first statement when LoopCounter = 2 This is the second statement when LoopCounter = 2 This is the first statement when LoopCounter = 3 This is the second statement when LoopCounter = 3 This is the first statement when LoopCounter = 4 This is the second statement when LoopCounter = 4 This is the statement outside the loop The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-5 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter; for(LoopCounter=1;LoopCounter<5;LoopCounter++) cout<<LoopCounter<<endl; cout<<LoopCounter+4<<endl; cout<<LoopCounter<<endl; } Output: 1 2 3 4 9 5 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.1 For Loops - Drill 4-6 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter; for(LoopCounter=1;LoopCounter<5;LoopCounter++) { cout<<LoopCounter<<endl; cout<<LoopCounter+4<<endl; } cout<<LoopCounter<<endl; } Output: 1 5 2 6 3 7 4 8 5 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops Sometimes a for loop is burdensome if we doesn’t require all the different sections of the for loop. Although we can leave them blank, often it is easier to use another type of loop. The most common is the while loop. The following is a template for a while loop: while (condition) { body of loop; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Example The following is a while loop to sum all the numbers from 1 to 10: #include <iostream.h> #include <stdlib.h> void main() { int Num=1; int Sum=0; while (Num<=10) { Sum+=Num; Num++; } cout<<Sum; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Example - Another way The following is a while loop to sum all the numbers from 1 to 10: #include <iostream.h> #include <stdlib.h> void main() { int Num=1; int Sum=0; while (Num<=10) Sum+=Num++; cout<<Sum; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Example Here is another example of a while loop to display the number and its squares. #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter=1; while (LoopCounter<=10) { cout<<LoopCounter<<'\t'<<LoopCounter*LoopCounter<<endl; LoopCounter++; } } Output: 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Drill 4-7 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter=1; while(LoopCounter<=10) cout<<LoopCounter++<<endl; } 1 2 3 4 5 6 7 8 9 10 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Drill 4-8 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter=1; while(LoopCounter++<=10) cout<<LoopCounter++<<endl; } 2 4 6 8 10 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Drill 4-9 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter=1; while(++LoopCounter<=10) cout<<LoopCounter++<<endl; } Output: 2 4 6 8 10 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.2 While Loops - Drill 4-10 #include <iostream.h> #include <stdlib.h> void main() { int LoopCounter=1; while(++LoopCounter<=10) cout<<++LoopCounter<<endl; } Output: 3 5 7 9 11 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops The final type of loop is a do loop. A do loop is used when you require a loop to execute at least one time. The format of a do loop is as follows: do { body of loop; } while (looping condition); The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops Look at the following loop that will present a message asking whether or not to continue. This process will continue as long as a ‘y’ or ‘Y’ is entered by the user. #include <stdlib.h> #include <iostream.h> void main() { char Ans; do { cout<<"The Steelers Rule!!!\n"; cout<<"Do you want another greeting?\n"; cout<<"Enter y for yes, n for no\n" cout<<"and then press return: "; cin>>Ans; } while (Ans == 'y' || Ans == 'Y'); cout << "Good bye!\n"; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops If you were required to write a program that will ask the user for ten numbers and output the maximum number, there are many ways to approach the problem. //Maximum Value - Version 1 #include <stdlib.h> #include <iostream.h> #define NumValues 10 void main() { int Max=0; //The maximum value so far int Curr; //The currently read value int LoopCounter; for (LoopCounter = 0; LoopCounter < NumValues; LoopCounter++) { cout<<"Enter a number:"<<endl; cin>>Curr; if (Curr > Max) Max = Curr; } cout<<"The maximum is "<<Max<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops //Maximum Value - Version 2 #include <stdlib.h> #include <iostream.h> #define NumValues 10 void main() { int Max; //The maximum value so far int Curr; //The currently read value int LoopCounter; cout<<"Enter a number:"<<endl; cin>>Max; for (int LoopCounter = 1; LoopCounter < NumValues; LoopCounter++) { cout<<"Enter a number:"<<endl; cin>>Curr; if (Curr > Max) Max = Curr; } cout<<"The maximum is "<<Max<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops //Maximum Value – Version 3 #include <stdlib.h> #include <iostream.h> void main() { int Max; //The maximum value so far int Curr; //The currently read value char Answer; cout<<"Enter a number:"<<endl; cin>>Max; cout<<"Do you wish to continue?"<<endl; cin>>Answer; while (Answer != 'Q' && Answer != 'q') { cout<<"Enter a number: "<<endl; cin>>Curr; if (Curr > Max) Max = Curr; cout<<"Do you wish to continue?"<<endl; cin>>Answer; } cout<<"The maximum is "<<Max<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops Let's see how we would write a program that will ask the user a YES/NO question twenty times. The program should track the number of YES results and the number of NO results. #include <stdlib.h> #include <iostream.h> #define NumValues 20 void main() { int NumYes=0; //Yes responses int NumNo=0; //No Response int LoopCounter; char Curr; //The currently read value for (LoopCounter = 0; LoopCounter < NumValues; LoopCounter++) { cout<<"Do you think the Steelers will win the Superbowl?"; cin>>Curr; if ((Curr == 'Y') || (Curr == 'y')) NumYes++; if ((Curr == 'N') || (Curr == 'n')) NumNo++; } cout<<"The number of yes responses = " << NumYes <<endl; cout<<"The number of no responses = " << NumNo <<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Survey Program - Version 2 #include <stdlib.h> #include <iostream.h> #define NumValues 20 void main() { int NumYes=0; //Yes responses int NumNo=0; //No Response int NumInvalid=0; //Invalid Responses int LoopCounter; char Curr; //The currently read value for (LoopCounter = 0; LoopCounter < NumValues; LoopCounter++) { cout<<"Do you think the Steelers will win the Superbowl?"; cin>>Curr; if ((Curr == 'Y') || (Curr == 'y')) NumYes++; else if ((Curr == 'N') || (Curr == 'n')) NumNo++; else NumInvalid++; } cout<<"The number of yes responses = " << NumYes <<endl; cout<<"The number of no responses = " << NumNo <<endl; cout<<"The number of invalid responses = " << NumInvalid <<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Survey Program - Version 3 #include <stdlib.h> #include <iostream.h> #define NumValues 20 void main() { int NumYes=0; //Yes responses int NumNo=0; //No Response int LoopCounter; char Curr; //The currently read value for (LoopCounter = 0; LoopCounter < NumValues; LoopCounter++) { cout<<"Do you think the Steelers will win the Superbowl?"; cin>>Curr; if ((Curr == 'Y') || (Curr == 'y')) NumYes++; else if ((Curr == 'N') || (Curr == 'n')) NumNo++; else LoopCounter--; } cout<<"The number of yes responses = " << NumYes <<endl; cout<<"The number of no responses = " << NumNo <<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-11 Write a program that displays the numbers from 1 to 15 on the screen. Solution: #include <stdlib.h> #include <iostream.h> void main() { int LoopCounter; for (LoopCounter=1; LoopCounter<=15; LoopCounter++) cout<< LoopCounter <<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-12 Write a program that displays the numbers from 15 to 1 on the screen. Solution: #include<stdlib.h> #include<iostream.h> void main() { int LoopCounter; for (LoopCounter=15; LoopCounter>=1; LoopCounter--) cout<<LoopCounter<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-13 Write a program that asks the user for a value. It should start by displaying the value entered by the user and decrease it by .5. Continue displaying and decreasing the value by .5 as long as the value is positive. Solution: #include<stdlib.h> #include<iostream.h> void main() { float LoopCounter; cout<<"Enter a number"<<endl; cin>> LoopCounter; while (LoopCounter > 0) { cout<< LoopCounter <<endl; LoopCounter = LoopCounter -.5; } } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-14 Write a program to read integers from the user. Display their sum. Continue to do this until the sum of the three numbers entered by the user is negative. Solution: //Reads integers Value1, Value2, Value3. Prints their sum. //Continues until the sum of Value1, Value2 and Value3 //is negative. #include<stdlib.h> #include<iostream.h> void main() { int Value1, Value2, Value3; int Sum=1; while (Sum >= 0) { cout<<"Enter three numbers"<<endl; cin>>Value1>>Value2>>Value3; Sum=Value1+Value2+Value3; cout<<"There sum is "<<Sum<<endl; } } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-15 Write a program that displays every odd number from 1 to 100. Solution: #include<stdlib.h> #include<iostream.h> void main() { int LoopCounter; for(LoopCounter =1; LoopCounter < 100; LoopCounter +=2) cout<<count<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-16 Write a program that asks for grades until a ‘-1’ is entered. Have the program compute and print the average of the grades to the nearest integer. Solution: #include <stdlib.h> #include <iostream.h> void main() { int Sum=0; int NumGrades=0; int Grade; do { cout<<"Enter a grade:"<<endl; cin>>Grade; if (Grade != -1) { Sum+=Grade; NumGrades++; } } while (Grade != -1); cout<<Sum/NumGrades; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.3 Do Loops - Drill 4-17 Write a program that will display all the even numbers between two values entered by the user. The list should include the values entered by the user if they are even. This one is harder because you need to test if the 1st number is even. Solution: #include<stdlib.h> #include<iostream.h> void main() { int LoopCounter; int Value1; int Value2; cout<<"Enter the starting value\n"; cin>>Value1; cout<<"Enter the ending value\n"; cin>>Value2; if (Value1%1 == 1) //check to see if it is odd Value1++; for(LoopCounter=Value1; LoopCounter <= Value2; LoopCounter +=2) cout<<LoopCounter<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.4 Nested Loops Just as we nested if statements, we may need to nest loops. Let's start with a simple unnested loop that will roll a pair of dice ten times. #include <stdlib.h> #include <iostream.h> void main() { int Die1, Die2, Sum=0; int LoopCounter; for (LoopCounter=0; LoopCounter<10; LoopCounter++) { Die1=rand()%6+1; Die2=rand()%6+1; cout<<Die1<<'\t'<<Die2<<endl; Sum+=Die1+Die2; } cout<<"SUM="<<Sum<<endl; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.4 Nested Loops - Drill 4-18 #include <iostream.h> #include <stdlib.h> void main() { int InnerLoopCounter, OuterLoopCounter; for(int OuterLoopCounter=0; OuterLoopCounter<3; OuterLoopCounter++) { for(int InnerLoopCounter=0; InnerLoopCounter<4; InnerLoopCounter++) { cout<<OuterLoopCounter<<' '<<InnerLoopCounter<<endl; } } } Solution: 0 0 0 1 0 2 0 3 1 0 1 1 1 2 1 3 2 0 2 1 2 2 2 3 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.4 Nested Loops - Drill 4-19 #include <iostream.h> #include <stdlib.h> void main() { int InnerLoopCounter; int OuterLoopCounter; for(int OuterLoopCounter=0 ; OuterLoopCounter<5; OuterLoopCounter++) { for(int InnerLoopCounter=OuterLoopCounter; InnerLoopCounter<5; InnerLoopCounter++) { cout<< OuterLoopCounter <<' '<< InnerLoopCounter <<endl; } } } Solution: 0 0 0 1 0 2 0 3 0 4 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 4 4 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.4 Nested Loops - Drill 4-20 #include <iostream.h> #include <stdlib.h> void main() { int InnerLoopCounter; int OuterLoopCounter; OuterLoopCounter=0; while (OuterLoopCounter<5) { cout<<OuterLoopCounter++<<'\t'; InnerLoopCounter=0; while(InnerLoopCounter<3) { cout<<++InnerLoopCounter<<'\t'; } cout<<endl; } } Solution: 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.4 Nested Loops - Drill 4-21 Write a program that draws a rectangle based on a width and height entered by the user. The rectangle should be drawn using the '*' character. If the input is 7 and 3, then the output should look as follows: Solution: #include <stdlib.h> #include <iostream.h> void main() { int Width; int Height; int HeightCounter; int WidthCounter; cout<<"Enter the width of the rectangle:"<<endl; cin>>Width; cout<<"Enter the height of the rectangle:"<<endl; cin>>Height; for(int HeightCounter=0;HeightCounter<Height;HeightCounter++) { for(int WidthCounter=0;WidthCounter<Width;WidthCounter++) cout<<"*"; cout<<endl; } } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.5 Early Loop Termination - Break & Control Keywords Sometimes we execute a loop and realize that it should not continue to process for a reason other that the looping expression evaluating to false. C++ allows us to handle cases like these with the break statement. Programmer' Notebook Caution should be taken when using the break statement. Using it instead of a properly written loop will lead to less readable code and cause maintenance problems as your programs get large. The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.5 Early Loop Termination - Break & Control Keywords Observe the following example of a program that will read values from the user and multiply them together. When a 0 is entered the program should stop. //Example of using a break statement #include <iostream.h> #include <stdlib.h> void main() { //Declare Variables int Product=1; int UserInput; //Loop Until 0 is entered do { cout<<"Enter an Integer, Enter 0 to quit"<<endl; cin>>UserInput; if (UserInput == 0) break; Product *=UserInput; } while(UserInput != 0) //Output the result cout<<"The Product is "<<Product; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.5 Early Loop Termination - Break & Control Keywords Another way of controlling a loop, is using the continue statement. Using it will allow the user to skip the remainder of the loop and continue with normal loop processing from there. See the following example. #include <iostream.h> #include <stdlib.h> #define NumValues 10 void main() { //Declare Variables int Product=1; int UserInput; int LoopCounter; //Loop 10 times for (LoopCounter = 0; LoopCounter < NumValues; LoopCounter++) { //Gather input from user cout<<"Enter an Integer "<<endl; cin>>UserInput; //Ensure input is valid if (UserInput == 0) continue; //Multiply value by previous value, if we get here! Product *=UserInput; } cout<<"The Product is "<<Product; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.6 Case Study Problem Description: An entrepreneur operating a day labor pool divides his workers into 3 skill levels. Unskilled workers receive $8.15 per hour; semiskilled workers $12.95 per hour and skilled workers $18.60 per hour. Write a program that calculates a worker's daily pay showing the dollar sign and the amount formatted to two decimal places. Test your program for an input of odd, even and fractional hours. Your program input should consist of the hours worked and a skill level indicator. The program should run continuously until terminated by the operator, at which time the sum of all wages paid that day should be displayed. Solution 1 - While Loops: #include <stdlib.h> #include <iostream.h> #include <iomanip.h> #define LWage (float) 8.15 #define MWage (float) 12.55 #define HWage (float) 18.60 void main() { float Hours = 1; float Pay; float Rate; float Sum = 0.0; int SkillIndicator; The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.6 Case Study Solution 1 - While Loops: cout << setprecision(2); cout << setiosflags(ios :: showpoint); cout << setiosflags(ios :: fixed); //Needed only for large numbers while(Hours > 0) //hours must have a starting value { cout << endl << endl << "Enter negative value to end or"; cout << endl <<"Enter hours worked -> "; cin >> Hours; if (Hours >= 0) { cout << "Enter skill indicator (1,2,3) -> "; cin >> SkillIndicator; if (SkillIndicator == 1) Rate = HWage; else if (SkillIndicator == 2) Rate = MWage; else if (SkillIndicator == 3) Rate = LWage; else { cerr << "You entered an invalid skill indicator"; continue; } The C++ Coach - Essentials for Introductory Programming - 2000
Chapter 4 - Loops 4.6 Case Study Solution Do - While Loops: #include <stdlib.h> #include <iostream.h> #include <iomanip.h> #include <ctype.h> #define LWage (float) 8.15 #define MWage (float) 12.55 #define HWage (float) 18.60 void main() { float Hours = 1; float Pay; float Rate; float Sum = 0.0; char SkillIndicator; cout << setprecision(2); cout << setiosflags(ios :: showpoint); cout << setiosflags(ios :: fixed); //Needed only for large numbers The C++ Coach - Essentials for Introductory Programming - 2000