From Pseudcode Algorithms directly to C++ programs (Chapter 7) Part 1: Mapping Pseudo-code style to C++ style input, output, simple computation, lists, while loops, if statements a bit of grammar Part 2: Details compilers, parts of a program, C++ specifics, data types 1 Ways of specifying algorithms: averaging a set of scores natural language well, first you need the scores, and how many there are, and you add them up and divide by how many there were, to get the average. unless there aren t any scores pseudo-code get howmany if howmany is not 0 then set total to 0 set counter to 1 while (counter <= howmany) get a value for onescore set total to total + onescore set counter to counter + 1 set avg to total/howmany print average is average else print no scores to average 2
cout << "how many numbers shall I average?"; cin >> howmany; if (howmany!= 0) total = 0; counter = 1; while (counter <= howmany) cin >> onescore; total = total + onescore; counter = counter + 1; avg = total/ howmany; cout << "average is" << avg; else cout << "no scores to average"; 3 Machines cannot directly execute high level programs. A=B+C LOAD Compiler STORE Assembler 0101 010 1 100 1 Linker 0101 1001 0110 Loader 4
// here is the program again that adds 2 numbers, from previous lecture #include <iostream> int main() int limit, floor, max; cout << Enter 2 numbers ; cin >> limit; cin >> floor; max = limit + floor; cout << max; comment Include directives Functions Main function Declarations Body return 0; 5 Libraries frequently used pieces of code; standard pieces of code key example: input/output code #include a directive to the compiler to find a library of code of the given name #include <iostream> // include everything in the library called // iostream cin and cout are defined in this library (our get and print ) cin >> limit; 24 <enter> cout << limit + 45; cout << all done ; cout << the value of limit is << limit << \n ; cout << all done ; 6
Definitions: function main () function a function is a named package of instructions. A function takes input (called arguments) and executes the instructions on that input, and produces ( returns ) a single result Consider calculator functions square(4) returns 16 in C++ the main() function - where your C++ instructions are located - it is called main and when it Exit code - the value returned by the main function - indicates whether the program terminated successfully return (0); - anything else means the program did not terminate successfully 7 Dev C++ program template includes all the necessary framing of the code #include <iostream.h> #include <stdlib.h> int main () // your program will go in here // system ( PAUSE ); return 0; C++ data types (there are more) Integer 7 2-5 int real 7.0 2.5-0.45 double A single character 7 a? char 8
Two classes of data items (named storage locations) Constants initialized unchangeable Variables const double timehalf = 1.5; const double doubletime = 2.0; const int reghours = 40; int double // and later in the program hoursworked, overtime; payrate; overtime = hoursworked - reghours; Names in programs are called identifiers. An identifier can consist of any combination of letters, digits, and _, except: - cannot start with a digit - cannot be same name as a C++ reserved word - is case-sensitiv 9 General format in C++ The Assignment Statement <variable> = <expression>; step 1: machine evaluates the expression step 2: machine puts it in ( assigns it ) to the named storage location const int magic=30; int floor, max, limit; cin >> floor; cin >> max; limit = floor + max * magic; cout << floor, max, limit, magic; 10
Addition + C = A + B; Arithmetic Operators Subtraction - C = A B; Multiplication * C = A * B; Division / C = A / B; Still more details. 1. operator precedence: *, / before +, - 2. integer division 3. type casting a result is converted to proper data typechar initial; answer = floor * limit - dog + max * 7; answer = (floor * limit) dog + (max * 7); 11 Comparison operators the same value as == 2 == 5 false not the same value as!= 2!= 5 true less than < 2 < 5 true greater than > 2 > 5 false less than or equal to <= 2<=5 true greater than or equal to >= 2 >=5 false Boolean Operators and && (2 < 5) && ( 10 == 10) true (2 == 5) && ( 10 == 10) false or (2 == 5) (10 == 10) true (2 == 5) (10 == 50) false not!!( 2 == 5 ) true 12