Presentation is loading. Please wait.

Presentation is loading. Please wait.

A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }

Similar presentations


Presentation on theme: "A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }"— Presentation transcript:

1

2 A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }

3 Line 1. Pre-processor line #include using namespace std;

4 function name type of returned value argument Line 2. main() function, entry point int main() Next Slide

5 Line 3. Use cout to display information cout << “\tHello World\n”; \t is a tab \n is a new line Redirection (insertion) Next Slide

6 Programs with simple input and output

7 cout <<"Here is 5: "<<5<<"\n"; Output Here is 5: 5 Example of cout Cursor ends up here

8 cout <<"A big number:\t"<<70000<<endl; Output A big number: 70000 Another example cout Tab moves to next position Another way of Forcing a new line

9 cout <<"The sum of 8 & 5 is "<<8 + 5<<endl; Output The sum of 8 & 5 is 13 Yet another example of cout Calculations

10 cout <<"Big # : "<< 7000.0*7000.0<<endl; Output Big # : 4.9e+07 A Look at cout very big numbers

11 cout <<"A fraction: "<<5.0/8.0 <<endl; Output A fraction: 0.625 A Look at cout fractions

12 Getting information from the user using cin, usually paired with cout cout > num1; The contents of the address named num1 is... Extraction operator

13 cout << “Enter 3 numbers: “; cin >> num1 << num2 << num3; The contents of the address named num1 is … num2 is … num3 is... A Look at cin

14 Reserved Words Words that have special meanings in the language. They must be used only for their specified purpose. Using them for any other purpose will result in a error. e.g. coutdoifswitch cinwhileelsereturn *

15 Reserved Words C++ is case-sensitive. Thus: cout COUTCoutcOut are all different. The reserved words are all in lowercase.

16 Statements A statement controls the sequence of execution, evaluates an expression, or does nothing, and ends with a semicolon.

17 Statements { cout <<"A fraction: "<<5.0/8.0 <<endl; cout <<"Big # : "<< 7000.0*7000.0<<endl; cout <<8 + 5 <<" is the sum of 8 & 5\n"; cout << “Hello world”; } There are 4 statements in this block. Block denoted by Curly braces

18 Comments, for PDL to Code These are important parts of a program. Two types of comments // ignore rest of line /* ignore between these */ or /* ignore over These lines */

19 Programming Style one main() function and use consistent layout of braces int main ( ) { statements; } indent open close

20 Declaration statements Any variable must be declared (announced) before it is used. This is a law of C/C++. Declaration instructs compiler to reserve memory to store values #include Using namespace std; int main() { int num1; cout << “Enter a number: “; cin >> num1; cout << “You entered number “ << num1 << endl; return 0; } declaration

21 Programming Style Group declarations at the beginning, just after the opening brace of main void main () { declaration statements; other statements; }

22 Programming Style Put blank lines before and after control structures int main () { declaration statements; statements if (expression){ statement; statement; } statements; }

23 Terminology: Atomic Data Something that is no decomposable. int float char +some others

24 int Any number +ve or –ve without a decimal point. Will truncate (cut off) any decimal points E.g. 5/8 is 0 not 0.625* big cause of errors E.g 15/2 is 7 not 7.5

25 float or double Sometimes called real. Contains decimal point. Precision Accuracy, note computers have problems with accuracy of floating point numbers. E.g. Pi Default is 6 significant digits Exponential notation E.g. 7000.0 is 7.0e+03

26 char A single character, letter or digit. Enclosed in single quotes ‘A’‘a’‘1’ Stored using ascii code.

27 Simple strings with char [] To input non numeric data, like names, and addresses, use character arrays. char FirstName[20] stores a string of up to 20 characters.

28 Simple String Example #include using namespace std; int main(void) { char FirstName[10]; cout << "Enter your first name : "; cin >> FirstName; cout << "Hello " << FirstName << endl; return 0; }

29 Output Enter your first name : Andreas Hello Andreas Press any key to continue

30 Demonstration – PDL –Code incremental development Pendulum problem Create defining diagram InputsProcessesOutputs periodIntitialise g,Pi. Get period Compute Plength Plength

31 Verification Tests Try Period = 2*Pi = 6.284 Why? Length = g = 9.8

32 PDL for pendulum 1. Assign 9.8 to g 2. Assign 3.142 to Pi 3. Prompt for a period in seconds 4. Calculate the length in metres 5. Display length for required period Use formula (given) length = g*(period/2*Pi) 2

33 Start Visual C++ environment Create console project in new solution Select Empty Project in Application Settings Create new C++ source file – add to project Enter skeleton program Check it works! Enter design as comments Add program description comment at top Test! Expand design one line at a time testing as you go


Download ppt "A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }"

Similar presentations


Ads by Google