1 / 32

A first program

A first program. #include &lt;iostream&gt; using namespace std; int main() { cout &lt;&lt;“tHello World<br>&quot;; return 0; }. Line 1. Pre-processor line. #include &lt;iostream&gt; using namespace std;. function name. argument. type of returned value. Line 2. main() function, entry point. int main().

mullinsd
Download Presentation

A first program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A first program • #include <iostream> • using namespace std; • int main() { • cout <<“\tHello World\n"; • return 0; • }

  2. Line 1. Pre-processor line #include <iostream> using namespace std;

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

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

  5. Programs with simple input and output

  6. Example of cout cout <<"Here is 5: "<<5<<"\n"; OutputHere is 5: 5 Cursor ends up here

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

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

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

  10. A Look at coutfractions cout <<"A fraction: "<<5.0/8.0 <<endl; OutputA fraction: 0.625

  11. Getting information from the user using cin,usually paired withcout Extraction operator cout << “Enter a number: “;cin >> num1; The contents of the address named num1 is ...

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

  13. 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. cout do if switch cin while else return *

  14. Reserved Words • C++ is case-sensitive.Thus:cout COUT Cout cOutare all different. The reserved words are all in lowercase.

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

  16. 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 4statements in this block. Block denoted by Curly braces

  17. 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 */

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

  19. declaration 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 <iostream> Using namespace std; int main() { int num1; cout << “Enter a number: “; cin >> num1; cout << “You entered number “ << num1 << endl; return 0; }

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

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

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

  23. 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

  24. 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

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

  26. 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.

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

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

  29. Demonstration – PDL –Code incremental development • Pendulum problem Create defining diagram

  30. Verification Tests • Try Period = 2*Pi = 6.284 • Why? • Length = g = 9.8

  31. 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

  32. 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

More Related