Starting from:
$30

$24

Lab 3 Solution

Introduction:




Last week's lab introduced us to conditional execution, where code may or may not run based on some condition. Similarly, many times when we write code we will want some statement to be executed many times. Rather than re-writing the statements that we want to be repeated, we can employ the techniques of ​Looping ​to have the program repeat those statements as many times as needed.​.​There are three types of loops provided in C++:




while decides whether to continue looping before executing do... while decides whether to continue looping after executing for executes a fixed number of times




The purpose of this lab is to introduce you to Looping.




Directions:







1- ​Launch Code::Blocks and start a new file. Name it your_last_name_lab3.cpp.




2-​Include the standard header for this lab:




//Your Name




//CS1428




//Lab 3




3- ​Include the iostream standard library and start your main function:




#include <iostream




using namespace std;




int main() {




4- ​Practice using each of the 3 kinds of loops by copying the following code:




int myCount = 0;




bool myCondition = true;




while (myCount < 5){




myCount = myCount + 1;




//Checks the condition before entering the loop



}



cout << "myCount is now: " << myCount << endl; //should be 5




myCondition = false;




do {



myCount = myCount - 1;




myCondition = myCount 0;




}while (myCondition);










//Checks the condition after the body of the loop



cout << "myCount is now: " << myCount << endl; //should be 0




for(int i = 1; i < 4; i++) { //Loops a fixed number of times



cout << "Loop number " << i << endl;




} //should output: Loop number 1 Loop number 2 Loop number 3 on separate lines




All 3 of these loops continue to execute the code contained in their curly brackets {} until some condition is met.




The ​while ​executed until ​(myCount < 5) ​was no longer true.




The ​do… while ​executed until ​myCondition ​was ​false ​at the end of the loop​.




The ​for ​loop probably looks the weirdest of the 3. It created and integer called i, gave it the value 1, and then executed until i’s value was 4 or bigger increasing the value of i by 1 each time. It therefore loops 3 total times. The integer i does not exist outside the body of the ​for ​loop.




5- ​Rewrite the following statement with a ​while ​loop rather than a ​for ​loop:




for (int x = 64; x 2; x = x / 2){




cout << x;




}
6- ​Your client requires a simple program which can classify triangles. You should write a small segment of code which will repeatedly take input from a user (i.e. use a ​cin​statement) for the lengths of the three legs of a triangle and then print ​acute, right, obtuse, or invalid ​based on the following relationships:




Obtuse




Right




Acute




Invalid




c^2 a^2 + b^2




c^2 = a^2 + b^2




c^2 < a^2 + b^2




c = a + b



Notice that c must be the largest of the three numbers. Your code should only come up with one classification for each​​set of three numbers. You should create some way to exit the loop and inform the user of how to exit the loop when he or she is done inputing values.




7-​Your client is happy with the code you produced for problem 6 but wants their in-house development team to be able to maintain it. Go back through your solution to problem 6 and add a comment explaining each action (not necessarily each line) that is being performed. Also briefly explain the purpose of variables at their declaration.




8- Do not copy the following code.​Instead, predict the value of the int ​outputValue ​after the following code has executed. Print your prediction to the console with a ​cout ​statement.




bool goOn = true;




int oldVal = 1, outputValue = 1, temp;




while(goOn){




temp = outputValue;




outputValue = oldVal + outputValue;




oldVal = temp;




if (outputValue 10) goOn = false;




}




cout << outputValue;



9- ​Save you work. Build and Run your file. Fix any errors. Your output should look something like this:





















































































10- ​Submit your .cpp file through TRACS as an attachment.

More products