DOC PREVIEW
CSUN COMP 106 - Fifth Programming Exercise

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The basic if statementCollege of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoFifth Programming ExerciseObjectiveThis exercise should give you the ability to write code using the various structures for if statements in C++. You will also see the results of two common errors in if statements: (1) placing an extra semicolon at the end of the if statement and (2) using a single equal sign instead of a double equal sign for the relational operator that tests for equality. Next you will look at Boolean variables used for data validation. Finally, you will have the opportunity to write some code that uses if statements, starting from a verbal description of an algorithm.Background for ExerciseDuring class, we have seen various formats for C++ if statements as shown below.The basic if statementif ( <condition> ){< true statement block >}<next statement executed if conditionis false>The if-elseif statementif ( <condition1> ){< block executed if condition1 is true>}else if ( <condition2> ){< block executed if condition2 is true>}else if ( <conditionN> ){< block executed if conditionN is true --- can have many elseif blocks>}else{< executed is no conditions are true block >}<transfer control here when statements after first true condition are completed>The if-else statementif ( <condition> ){< true statement block >}else{< flase statement block >}<next statement executed after true or false block>The <condition> in the structures above is a Boolean expression that is evaluated as true or false. Boolean expressions are created by a combination of relational operators (<, > <=, >=, ==, and !=) and logical operators (!, &&, ||). The relational operators allow us to determine the truth orfalsity about relations between variables. E. g., hours < 40 will be true is the variable hours is Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448Email: [email protected] 8348 Fax: 818.677.7062greater than 40; it will be false otherwise. We also saw that we could define Boolean variables with the C++ type bool. An example of this is shown below.bool goodData = x >= xMin && x <= xMax;We can use either of the following approaches to do an if test for good data.bool goodData = x >= xMin && x <= xMax;if ( goodData)orif( x >= xMin && x <= xMax )It is simpler to use the single statement immediately above. However, the use of the Boolean variable goodData clarifies what we are doing and can be used in later if tests to continue the calculation if the data are good.Overview of the exerciseThis project has four tasks. The first two are copy and paste assignments; the third requires you to create a program from scratch for solving a quadratic equation, ax2 + bx + c = 0, that will account for all possible results depending on the input data. The fourth task is a simple follow up to task three to illustrate the possible roundoff error in solving the quadratic equation. The instructions below discuss the items that you should place on the submission file, to which you will copy all your listings and the screen displays from the program execution. See the notes for the second exercise for details preparing the submission file. Make sure that your name and yourlab day are on the top of your submission file.Specific tasksTask one: Code review and errors – Copy the code below into Visual C++ then compile and execute it without any changes, except for entering your name and today’s date in the initial comments./* Programmer: Date: Comp 106 Exercise Five If Statements Task one*/#include <iostream>using namespace std;int main(){ // Examine output from errors (incorrect semicolon and // incorrect relational operator for "is equal to" ) int x = 1; if ( x == 10); { cout << "First if block is executed only when x = 10.\n" << "Here x = " << x << endl << endl; } if ( x = 7 ) { cout << "True block from second if is executed when x = 7.\n"Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448Email: [email protected] 8348 Fax: 818.677.7062<< "Here x = " << x << endl << endl; } else { cout << "False block from second if is executed when x != 7.\n" << "Here x = " << x << endl << endl; } }Note the errors in the two if statements and observe the output these errors cause when you run the program. Make sure that you understand these errors so that you can avoid them in the future. Correct the errors and run the code a second time. For both runs, copy the code and the screen output to the submission fileTask two: Data validation with Boolean variables – Copy the code below into Visual C++ then compile and execute it for the input cases shown following the code. (Enter your name and today’s date in the comments.) /* Programmer: Date: Comp 106 Exercise Five If Statements Task two*/#include <iostream>using namespace std;int main(){ // Data validation example with boolean variables const int yMin = 0, yMax =100; int y; cout << "Enter a value for y between " << yMin << " and " << yMax << ": "; cin >> y; bool belowMin = y < yMin; bool aboveMax = y > yMax; bool badData = belowMin || aboveMax; bool goodData = !badData; bool goodData2 = !belowMin && !aboveMax; cout << "\n Your input y = " << y << "\nMaximum y value = " << yMax << "\nMinimum y value = " << yMin << "\n aboveMax = " << aboveMax << "\n belowMin = " << belowMin << "\n badData = " << badData << "\n goodData = " << goodData << "\n goodData2 = " << goodData2 << "\n\nOutput with boolalpha set\n" << boolalpha << "\n Your input y = " << y << "\nMaximum y value = " << yMax << "\nMinimum y value = " << yMin << "\n aboveMax = " << aboveMax << "\n belowMin = " << belowMin << "\n badData = " << badData << "\n goodData = " << goodDataJacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448Email: [email protected] 8348 Fax: 818.677.7062<< "\n goodData2 = " << goodData2; if ( goodData ) { double x = 13 + 72 * y; cout << "\n\nFor input y = " << y << ", x = " << x << endl << endl; return EXIT_SUCCESS; } else { cout << "\n\nInput out of range; program will halt.\n\n"; return


View Full Document

CSUN COMP 106 - Fifth Programming Exercise

Download Fifth Programming Exercise
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Fifth Programming Exercise and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Fifth Programming Exercise 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?