A Computer Science Tapestry4.1Control, Functions, Classesz We’ve used built-in types like int and double as well as the standard class string and the streams cin and cout¾ Each type supports certain operations and has a specific range of values• What are these for the types we’ve seen so far?¾ We need more than these basic building blocks, why?z We’ve used void functions to encapsulate concepts/statements with one name, avoid repeated code, help develop programs¾ Functions with parameters are useful¾ We need functions that return values to solve more problems than we’re currently able to solveA Computer Science Tapestry4.2Types of controlz Selection: choose from among many options according to criteria the programmer codes (from which the user chooses)¾ If response is yes do this, else do that¾ If year is a leap year number of days is 366, else 365¾ If PIN is incorrect three times, keep banking card¾ If 10thcaller, we have a winnerz Repetition/iteration (next chapter), repeatedly execute statements until criteria met¾ Print twelve months of a calendar¾ Allow three attempts at PIN entry¾ Make moves in game until game is overA Computer Science Tapestry4.3Problem solving leads to programmingz Which is the better value, a 10 inch, $10.95 pizza or a 12 inch $15.95 pizza?¾ Details needed to solve the problem (no computer)?¾ What’s missing from programming repertoire?¾ Print two price/sq. in values, let user make conclusions¾ Program should determine best value after calculatingz We need selection (why?) and we’d like a function to return a value for comparison (what’s the function?)if ( PizzaValue(10,10.95) > PizzaValue(12,15.95) )cout << "10 inch pizza is better value" << endl;A Computer Science Tapestry4.4First step, the assignment operatorz Avoid repeated calculationsvoid SpherePizza(double radius, double price){double volume;volume = 4.0/3*radius*radius*radius*3.1416;double area;area = 4*radius*radius*3.1416;cout << " area = " << area << endl;cout << " volume = " << volume << endl;cout << " $/cu.in " << price/volume << endl;}z Assign a value to a variable to give it a value¾ We have used input stream to enter values for variables¾ Read the assignment operator as gets, “area gets …” • Avoids confusion with equality operator we’ll see laterA Computer Science Tapestry4.5Calculating change (see change.cpp)int main(){ int amount;int quarters, dimes, nickels, pennies;cout << "make change in coins for what amount: ";cin >> amount;quarters = amount/25;amount = amount - quarters*25;dimes = amount/10;amount = amount - dimes*10;// more code here, see the full program}z How does amount = amount - dimes*10 execute?¾ Evaluate expression on right hand side of operator =¾ Store value in variable named on left hand side¾ Problem if same variable used on both sides? Why? • Differences between reading and writing valuesA Computer Science Tapestry4.6Problems with code in change.cpp?// previous code for entering value, doing quartersdimes = amount/10;amount = amount - dimes*10;nickels = amount/5;amount = amount - nickels*5;pennies = amount;cout << "# quarters =\t" << quarters << endl;cout << "# dimes =\t" << dimes << endl;cout << "# nickels =\t" << nickels << endl;cout << "# pennies =\t" << pennies << endl;z What about output statement if there are no quarters?z What about repeated code?¾ Code maintenance is sometimes more important than code development. Repeated code can cause problems, why?A Computer Science Tapestry4.7Control via selection, the if statementvoid Output(string coin, int amount){if (amount > 0){ cout << "# " << coin << " =\t" << amount << endl;}}int main(){// code for assignment to quarters, dimes, etcOutput("quarters",quarters);Output("dimes",dimes);Output("nickels",nickels);Output("pennies",pennies);}z User enters 23 cents, what’s printed? Why?¾ Selection statement determines if code executes; test or guard expression evaluates to true or false (Boolean)A Computer Science Tapestry4.8Selection using if/else statementint main(){string name;cout << "enter name: ";cin >> name;if (name == ”Oogy"){ cout << "that’s a very nice name" << endl;}else{ cout << name << " might be a nice name" << endl;}return 0;}z What if user enters “oogy” ? or “ Oogy”z How many statements can be guarded by if or else?z What other tests/guards can be used (we’ve seen < and ==)A Computer Science Tapestry4.9More Operators: Relationalz The guard/test in an if statement must be a Boolean expression (named for George Boole)¾ Values are true and false¾ bool is a built-in type like int, double, but some older compilers don’t support it (very old)int degrees;bool isHot = false;cout << "enter temperature: ";cin >> degrees;if (degrees > 95) isHot = true; // or belowisHot = degrees > 95;z Relational operators are used in expressions to compare values: <, <=, >, >=, ==, !=, used for many types¾ See Table 4.2 and A.4 for details, precedence, etc.A Computer Science Tapestry4.10Details of Relational Operatorsz Relational (comparison) operators work as expected with intand double values, what about string and bool?23 < 45 49.0 >= 7*7 "apple" < "berry" z Strings are compared lexicographically (alphabetically) so that "ant" < "zebra" but (suprisingly?) "Ant" < "zebra" ¾ How do lengths of strings compare?¾ Why does uppercase ‘A’ come before lowercase ‘z’?¾ (Actually “Ant” < “zebra” doesn’t work, need string)z Boolean values have numeric equivalents, 1 is true, 0 is falsecout << (23 < 45) << endl;cout << ("guava" == "Guava") << endl;A Computer Science Tapestry4.11Relational Operators: details, details,…z Use parentheses liberally, or hard-to-find problems occur cout << 23 + 4 < 16 - 2 << endl;¾ Causes following error using g++, fix using parentheses rather than deciphering:invalid operands `int' and `ostream & ()(ostream &)' to binary `operator <<'z What about true/false and numeric one/zero equivalent?if (3 + 4 – 7) { cout << "hi" << endl; }else { cout << "goodbye" << endl; }A Computer Science Tapestry4.12Logical operatorsz Boolean expressions can be combined using logical operators: AND, OR, NOT¾ C++ equivalents are &&, ||, and !, respectivelyif (90 <= grade){ if (grade < 95) { cout << "that’s an A" << endl;}}¾ What range of values generates ‘A’ message? Problems?if (90 <= grade && grade < 95){ cout << "that’s an A" << endl;}A Computer Science
View Full Document