+ All Categories
Home > Documents > A first program 1. #include 2. using namespace std; 3. int main() { 4. cout

A first program 1. #include 2. using namespace std; 3. int main() { 4. cout

Date post: 13-Dec-2015
Category:
Upload: gwenda-owen
View: 227 times
Download: 1 times
Share this document with a friend
Popular Tags:
32
A first program 1. #include <iostream> 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }
Transcript

A first program

1. #include <iostream>

2. using namespace std;

3. int main() {

4. cout <<“\tHello World\n";

5. return 0;

6. }

Line 1. Pre-processor line

#include <iostream>

using namespace std;

function name

type of returned value

argument

Line 2. main() function, entry point

int main()

Next Slide

Line 3. Use cout to display information

cout << “\tHello World\n”;

\t is a tab \n is a new line

Redirection (insertion)

Next Slide

Programs with simple input and output

cout <<"Here is 5: "<<5<<"\n";

Output

Here is 5: 5

Example of cout

Cursor ends up here

cout <<"A big number:\t"<<70000<<endl;

Output

A big number: 70000

Another example cout

Tab moves to nextposition

Another way ofForcing a new line

cout <<"The sum of 8 & 5 is "<<8 + 5<<endl;

Output

The sum of 8 & 5 is 13

Yet another example of cout

Calculations

cout <<"Big # : "<< 7000.0*7000.0<<endl;

Output

Big # : 4.9e+07

A Look at cout very big numbers

cout <<"A fraction: "<<5.0/8.0 <<endl;

Output

A fraction: 0.625

A Look at cout fractions

Getting information from the user using

cin,usually paired with cout

cout << “Enter a number: “;cin >> num1;

The contents of the address named num1

is ...

Extraction operator

cout << “Enter 3 numbers: “;

cin >> num1 << num2 << num3;

The contents of the address named num1 is …num2 is …num3 is ...

A Look at cin

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 switchcin while else return

*

Reserved Words

C++ is case-sensitive.

Thus:

cout COUT Cout cOut

are 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 4 statements in this block.

Block denoted byCurly 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;

}

indent

open

close

Declaration statementsAny 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;

}

declaration

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 structuresint 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

Inputs Processes Outputs

period Intitialise g,Pi.

Get period

Compute Plength

Plength

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 solutionSelect Empty Project in Application SettingsCreate new C++ source file – add to projectEnter skeleton programCheck it works!Enter design as commentsAdd program description comment at topTest!Expand design one line at a time testing as you go


Recommended