Fundamental Programming More Basics Fundamental Programming 310201 1
Fundamental Programming More Basics Fundamental Programming 310201 1
What’s Next · we started out with a minimal level of C++ syntax to get you started · Next four sessions: ·Back to Basics – more basic C++ ·Testing – what is testing, and how do you do it? ·More on Selection – everything you always wanted to know about if…else… ·More on Repetition – different looping constructs Fundamental Programming 310201 2
#include <iostream. h> · a couple of comments about this… · # lines are not compiled, they simply direct the actions of the compiler – compiler directives , we’ll meet more later… · all standards libraries are enclosed in angle brackets , libraries you develop are enclosed in double-quotes - later you’ll learn how to do this Fundamental Programming 310201 3
void main (void) · the second void above means that main has no input of output variables; actually, it is optional – you will sometimes see: void main() · also, you will sometimes see int before main – a main function that returns an integer int main() {. . . return Error. Number; } · this is used to return an error number to the operating system – 0 means no error Fundamental Programming 310201 4
Data Objects · Study Guide uses the term data object to refer collectively to variables and constants · variable : holds a value that will vary · constant : holds a value that is constant – cannot be changed in the program const int BOILING_POINT_OF_WATER = 100; const float ABSOLUTE_ZERO = -273. 15; · declared like variables, with const before data type Fundamental Programming 310201 5
Naming Rules · start with a letter: A-to-Z, a-to-z · can contain upper or lower case letters, numerals (0 -to-9), or underscore · only first 32 characters are significant · some compilers will allow you to use more · compiler may not be able to distinguish between different names with same first 32 characters · so, good idea to limit to 32 characters · avoid reserved words like int, float, etc Fundamental Programming 310201 6
Style Guide · many organisations use a style guide · the goal is consistency in program code – to make it easier to maintain · we use a simple Style Guide in this course: · all names (functions, variable and constants) start with upper case letter · variables use mixed case – capitalise first letter of each word: Student. Mark · constants are all upper case – use underscore to separate each word: ABSOLUTE_ZERO for details see: Study Guide (p. 7 -19) Fundamental Programming 310201 7
Local Data Objects · local data objects: local to the function where they are declared; not visible outside the function #include <iostream. h> objects declared in a function are local - are not visible outside function void main (void) { const float ABSOLUTE_ZERO = -273. 15; int Temperature = 0; : } Fundamental Programming 310201 8
Global Data Objects · global data objects: declared outside functions - before main; visible everywhere #include <iostream. h> const float ABSOLUTE_ZERO = -273. 15; void main (void) { : global variables } objects declared before main are global - visible everywhere are not recommended in this course – but may be used for now! Fundamental Programming 310201 9
Data Types · the choice of data type affects how much memory is used to store a value, and what you can store… short int uses 2 bytes of memory (16 bits) and can hold a maximum value of 32, 767 long int uses 4 bytes of memory and can hold a maximum value of 2, 147, 483, 648 float uses 4 bytes and can hold a maximum value of 3. 4 * 1038 double uses 8 bytes and holds a maximum value of 1. 7 * 10308 Fundamental Programming 310201 10
Data Types · the choice of real type also affects the precision of numbers you can store – the number of significant digits float provides 7 significant digits – 1. 234567890 is stored as 1. 234568 1, 234, 567, 890 is stored as 1, 234, 568, 000 double provides 15 significant digits see: Study Guide (p. 7 -12); textbook (p 41) Fundamental Programming 310201 11
Data Type Demotion · when you assign a real value to an integer variable, we say that it is demoted – try: int Int. Variable; Int. Variable = 3. 7; cout << Int. Variable; · the above code will output the value 3 · the value 3. 7 is demoted to 3 when it is assigned to an integer variable - the. 7 is truncated (chopped off) Fundamental Programming 310201 12
Data Type Promotion · when you assign an integer value to a real variable, we say that it is promoted · nothing to worry about here - no data is lost… int Int. Variable = 3; float Float. Variable; Float. Variable = Int. Variable; · no information lost! Fundamental Programming 310201 13
Expressions · watch out for this one – try this: int Int. Variable 1 = 3, Int. Variable 2 = 4; cout << Int. Variable 1 / Int. Variable 2; · this code will output 0 – both operands are integers, so an integer result is given · you can fix this by converting one of the operands to a real type: or cout << float(Int. Variable 1) / In. Variable 2; cout << 1. 0 * Int. Variable 1 / In. Variable 2; Fundamental Programming 310201 14
Expressions · one more thing about expressions – try: cout << 2 + (Variable = 3); · you might expected this to cause a compilation error – it doesn’t, it outputs 5 · in C++, an assignment is considered to evaluate to the value being assigned · this is why the compiler does not object to: if (Variable = 3) : · take care to avoid this type of error Fundamental Programming 310201 15
Activity · what will the following program output? #include <iostream. h> void main(void) { int Variable; if (Variable = 2) { cout << “True”; } else { cout << “False”; } } Fundamental Programming 310201 16
Activity Break Fundamental Programming 310201 17
Activity Feedback · the program will output: “True” #include <iostream. h> void main(void) { int Variable; if (Variable = 2) { cout << “True”; } else { cout << “False”; } } as the expression does not evaluate to 0 (it evaluates to 2), it’s True Fundamental Programming 310201 18
ASCII Coding System · ASCII coding system is used to encode text: ) 1 A a is stored as 41 49 65 97 · some values in ASCII coding system were reserved for controlling output devices 7 10 13 sound a beep - Bell move to next line – Line Feed move to start of current line – Carriage Return for details see: Study Guide (p. 7 -13) Fundamental Programming 310201 19
Special Characters · the backslash character is used to send special characters to cout: a - Bell r – Carriage Return n – Line Feed t – Horizontal Tab · example: cout << “tt. Invalid Input!ann“; · if you want to output the string “t”: cout << “\t“; for details see: Study Guide (p. 7 -25) Fundamental Programming 310201 20
More On cin · one feature of cin is that it can read multiple input values off a single input line : Sum = 0; while (Input. Value != -1) { Sum = Sum + Input. Value; cin >> Input. Value; } cout << “Sum = “ << Sum; : 1 2 3 4 -1 Sum = 10 · when extracting a value from cin, characters are read from the keyboard until it reaches a white space character – space or new-line Fundamental Programming 310201 21
More On cin · sometimes you may want to throw away all characters to the end of the input line : char Char = ' '; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; } : Enter X to exit ==> abcdef Enter X to exit ==> Enter X to exit ==> Fundamental Programming 310201 22
More On cin · to ignore all characters to the end of the input line, do this: : char Char = ' '; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; cin. ignore(80, ’n’); } : Enter X to exit ==> abcdef Enter X to exit ==> ignore up to 80 characters, or to end-of-line Fundamental Programming 310201 23
More On cin · or this… : char Char = ' ‘, Other. Char; while (Char != 'X') { cout << "Enter X to exit ==> "; cin >> Char; Other. Char = ‘ ‘; while (Other. Char != 'n') { cin. get(Other. Char); } } Enter X to exit ==> abcdef : Enter X to exit ==> get characters from keyboard until we reach end-of-line Fundamental Programming 310201 24
More On cin · as mentioned previously, we do not want to waste too much time on cin · in this course, your programs are not required to handle input data of the wrong type – if an integer is required, you can assume the user will enter an integer for more details see: Study Guide (p. 10 -18) Fundamental Programming 310201 25
More On cout · using cout, you can output more than one expression in a single statement cout << “Var 1 = “ << Var 1 << endl; · often we want to format output, like: Centigrade Fahrenheit ========== 100 212 1000 1832 100. 12 212. 22 · the iomanip library has some functions that are useful formatting output… Fundamental Programming 310201 26
#include <iostream. h> #include <iomanip. h> void main(void) { cout << 1. 23456 << endl; cout << setprecision(5); cout << 1. 23456 << endl; cout << setw(8); cout << 1. 23456 << endl; } set number of significant digits for all following output values set column width for next output value 1. 23456 1. 2346 Fundamental Programming 310201 27
Activity · what output will this program produce? #include <iostream. h> #include <iomanip. h> void main(void) { cout << "Centigrade Fahrenheitn"; cout << "=====n"; cout << setw(10) << 100 << 212 << endl; cout << setprecision(5); cout << setw(10) << 100. 123 << 212. 222 << endl; cout << setprecision(3); cout << setw(10) << 100. 1234 << 212. 22212 << endl; cout << setw(10) << 1234567. 8 << 2222254. 04 << endl; } Fundamental Programming 310201 28
Activity Break Fundamental Programming 310201 29
Activity Feedback #include <iostream. h> #include <iomanip. h> void main(void) { cout << "Centigrade Fahrenheitn"; cout << "=====n"; cout << setw(10) << 100 << 212 << endl; cout << setprecision(5); cout << setw(10) << 100. 123 << 212. 222 << endl; cout << setprecision(3); cout << setw(10) << 100. 1234 << 212. 22212 << endl; cout << setw(10) << 1234567. 8 << 2222254. 04 << endl; } Centigrade Fahrenheit ========== 100212 100. 12212. 22 100212 1. 23 e+062. 22 e+06 scientific notation used here because only 3 significant digits are output Fundamental Programming 310201 30
Activity Feedback #include <iostream. h> #include <iomanip. h> void main(void) { cout << "Centigrade Fahrenheitn"; cout << "=====n"; if we, set cout << setprecision(9); number of cout << setw(10) << 100 significant << setw(11) << 212 << endl; digits to 9 cout << setw(10) << 100. 123 << setw(11) << 212. 222 << endl; cout << setw(10) << 100. 1234 << setw(11) << 212. 22212 << endl; cout << setw(10) << 1234567. Centigrade Fahrenheit << setw(11) << 2222254. ========== } 100 212 and set width of 100. 123 212. 222 100. 1234 212. 22212 second column to 1234567. 8 2222254. 04 11, we get Fundamental Programming 310201 31
More On cout · setprecision only applies to floating point numbers – it has no effect on integer output cout << setprecision(2); cout << 1000; 1000 · scientific notation is used when the number of digits preceding decimal place exceeds the number of significant digits to output cout << setprecision(2); cout << 100. 0; 1. e+02 Fundamental Programming 310201 32
More On Assignment · Pascal has only one assignment operator: <variable> : = <expression>; · C++ has a number of assignment operators – the two statements below are equivalent: Nbr. Marks = Nbr. Marks + 1; Nbr. Marks += 1; · as are the two below: Remainder = Remainder - Candidate. Factor; Remainder -= Candidate. Factor; for more see: Textbook (p. 169) Fundamental Programming 310201 33
Comments · two types of comments – single-line, multi-line // This is a single-line comment, each line of // the comment must start with // /* This is a multi-line comment – the comment runs for any number of lines until we reach */ · recommendation: use single-line comments – then, multi-line comments can be used to comment-out lines of code : cout << “Variable 1 = “ << Variable 1; // display value of other variables cout << “Variable 2 = “ << Variable 2; : Fundamental Programming 310201 34
Comments · two types of comments – single-line, multi-line // This is a single-line comment, each line of // the comment must start with // /* This is a multi-line comment – the comment runs for any number of lines until we reach */ · recommendation: use single-line comments – then multi-line comments can be used to comment-out lines of code : /* cout << “Variable 1 = “ << Variable 1; // display value of other variables cout << “Variable 2 = “ << Variable 2; */ : Fundamental Programming 310201 35
Activity · would this not work if we had: : cout << “Variable 1 = “ << Variable 1; /* display value of other variables */ cout << “Variable 2 = “ << Variable 2; : what would be the effect of adding /* here and adding */ here Fundamental Programming 310201 36
Activity Break Fundamental Programming 310201 37
Activity Feedback · why would this not work if we had: : /* cout << “Variable 1 = “ << Variable 1; /* display value of other variables */ cout << “Variable 2 = “ << Variable 2; */ : compiler error here Fundamental Programming 310201 38
Multi-line Statements · compilers treat an end-of-line in C++ as white space – so: cout << “Variable 1 = “ << Variable 1; My. Very. Long. Variable. Name = <my very long expression>; · is same as: cout << “Variable 1 = “ << Variable 1; My. Very. Long. Variable. Name = <my very long expression>; Fundamental Programming 310201 39
That’s the end of “Back to Basics” ! Fundamental Programming 310201 40
- Slides: 40