320 likes | 342 Views
A first program. #include <iostream> using namespace std; int main() { cout <<“tHello World<br>"; return 0; }. Line 1. Pre-processor line. #include <iostream> using namespace std;. function name. argument. type of returned value. Line 2. main() function, entry point. int main().
E N D
A first program • #include <iostream> • using namespace std; • int main() { • cout <<“\tHello World\n"; • return 0; • }
Line 1. Pre-processor line #include <iostream> using namespace std;
function name argument type of returned value Line 2. main() function, entry point int main() Next Slide
Redirection (insertion) \t is a tab \n is a new line Line 3. Use cout to display information cout << “\tHello World\n”; Next Slide
Example of cout cout <<"Here is 5: "<<5<<"\n"; OutputHere is 5: 5 Cursor ends up here
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
Yet another example of cout Calculations cout <<"The sum of 8 & 5 is "<<8 + 5<<endl; OutputThe sum of 8 & 5 is 13
A Look at cout very big numbers cout <<"Big # : "<< 7000.0*7000.0<<endl; OutputBig # : 4.9e+07
A Look at coutfractions cout <<"A fraction: "<<5.0/8.0 <<endl; OutputA fraction: 0.625
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 ...
A Look at cin cout << “Enter 3 numbers: “; cin >> num1 << num2 << num3; The contents of the address named num1 is … num2 is … num3 is ...
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 *
Reserved Words • C++ is case-sensitive.Thus:cout COUT Cout cOutare all different. The reserved words are all in lowercase.
Statements • A statement controls the sequence of execution, evaluates an expression, or does nothing, and ends with a semicolon.
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
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 */
Programming Style one main() function and use consistent layout of braces int main ( ) { statements; } open indent close
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; }
Programming Style Group declarations at the beginning, just after the opening brace of main void main () {declaration statements; other statements; }
Programming Style Put blank lines before and after control structures int main () { declaration statements; statements if (expression){ statement; statement; } statements;}
Terminology: Atomic Data • Something that is no decomposable. • int • float • char • +some others
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
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
char • A single character, letter or digit. • Enclosed in single quotes • ‘A’ ‘a’ ‘1’ • Stored using ascii code.
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.
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; }
Output Enter your first name : Andreas Hello Andreas Press any key to continue
Demonstration – PDL –Code incremental development • Pendulum problem Create defining diagram
Verification Tests • Try Period = 2*Pi = 6.284 • Why? • Length = g = 9.8
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
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