Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program...

19
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a simple c++ program

Transcript of Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program...

Page 1: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Overview of c++

Objectives

1. Understanding the use of the following elements in a c++ program

variables

constants

assignment

input

output

2. Writing a simple c++ program

Page 2: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Variables - How Do We Know If We Need Them?

Consider a variation of the previous problem.

INPUT -> PROCESS -> OUTPUTdim in feet calc perimeter total costCostPYard SqYards

calc cost$1.00/ft edging

+ SqYards * CostPYard

PROCESS1. input length and width2. calc perimeter3. calc square feet4. calc square yards5. input CostPYard6. calc cost of edging7. calc cost of unedged carpet8. calc total cost -> cost of carpet + cost of edging9. output total cost

Page 3: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Extreme -> Minimum Variables

#include <iostream.h>

void main()

{ const float PerFtEdging = 1.0;

float length, width, CostPYard;

cin >> length >> width >> CostPYard;

cout << 2*(length+width) * PerFtEdging +

( (length*width)/9.0) * CostPYard;

}

Once again you have a tradeoff:Less Variables

More difficult to follow

Page 4: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Extreme -> Maximum Variables

#include <iostream.h>

void main()

{ const float PerFtEdging = 1.0;

float length, width, Perimeter, CostPYard, CostEdging, CostCarpet, SqFeet, SqYards, TotalCost;

cin >> length >> width;

Perimeter = 2 * (length + width);

SqFeet = length * width;

SqYards = SqFeet/9;

cin >> CostPYard;

CostEdging = Perimeter * PerFtEdging;

CostCarpet = SqYards * CostPYard;

TotalCost = CostEdging + CostCarpet;

cout << TotalCost;

}

More VariablesIt's somewhat subjective.

Avoid UNDERuse of variables!

I have avoided comments and labelling for space on the overhead

Page 5: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

HOW Do We Tell C++ We Need Variables?

In declarations of the following form!

type variable-name;

examples:

float length;

float width;

or

float length, width;

NAMES->

begin with letter

have letters, digits, underscores

avoid c++ names (cout, cin, float)

try to use appropriate names

TYPES->

float int (strings)

(more later)

Page 6: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

float

Can be fractional

1.0 12.7 12e+6 12e-1

Are APPROXIMATE when stored in a computer

MOST Base 10 numbers CAN NOT be represented in a finite number of Base 2 digits. -> They would require an infinite amount of memory to be exact!

Understand scientific notation.

Can be very large or small

1e39 1e-39

Limited PRECISION -> 7 base 10 digits

123.456789 would be stored as

123.456779232163523127123

Page 7: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

int

Whole numbers ONLY

Are EXACT, not approximate

Limited size :

-2 billion < int < +2 billion

(roughly)

A number of kinds of integers exist in c++.

They vary by the range of size of values (amount of memory)

whether they are

signed

+3 -12 +4 -123

or unsigned

2 5 454 34 (no negatives)

For Now Use "int" ONLYBe careful when dividing integers!

Page 8: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Strings

Strings as we will use them initially are NOT variable types.

For example, we can NOT do the following:#include <iostream.h>

void main()

{ int first; // this is OK

string second; // do not try this yet

.....

}

Strings are used for output whether it is on the output device printer file screen

Strings are underlined in this example. Note no variables!..

first = 10;

cout >> "The value of first is " >> first >> "\n"

Output is

The value of first is 10

Page 9: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Inputting Data

What role does the input play in your programs?

ANSWER:

To transfer values from devices such as the keyboard, disk or mouse to the main memory of the machine.

In C++

"cin"

Consider the following simple program:

int age; // step 1

age = 10; // step 2

cin >> age; // step 3

STEP what happens value of age

1 memory allocated ???2 age value set to 10 103a program executes cin 103b user types value 23 23

Whatever value is typed by the user is placed in age.

Page 10: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

More cin

READING MULTIPLE VALUES:

Suppose you need to read 3 values.

cin >> a >> b >> c; // separate variables with >>

OR cin >> a;

cin >> b;

cin >> c;

Either. In most situations, they function the same. c++ will continue reading until it finds the data you ask for no matter how many lines it takes. Try running cinEx1.C and cinEx2.C from the public account and run both by entering the data on the same line and different lines (four times altogether).

You will see they work the same way!

1. Rules for reading char and strings require special consideration.

2. Special cases are best ignored at this point.

Page 11: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Output in c++

Lets you output to the screen any of the following:

int float string and others

The values which are printed can be either variables, constants, or (in general) expression values.

Separate each value (expression) with a <<

EXAMPLES:

cout << "\n"; // says position output on next line

cout << ‘\n’; // same effect but really different

cout << endl; // does the same thing

x = 10;

y = 7;

// outputs only the value 10

cout << x;

// outputs The value of x is 10 and a newline

cout << "The value of x is " << x << "\n";

// outputs 107 ... forgot to output spacing

cout << x;

cout << y;

Page 12: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Expressions

Don't overwhelm yourself with rules.

They'll come with practice!

Rules: (Fig. 1.11, p.28)

1. () parentheses are evaluated first

2. * / % are equal and evaluated second

+ - are equal and evaluated last

3. when operations are equal LEFT->RIGHT

Terms: X + 12

operand operator operand

Division and Mod for integers:

Think of grade-school division (w/o remainder)

EXAMPLE:

(7 / 2) * 2 + (7 % 2)

3 * 2 + 1

6 + 1

7

Page 13: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

When writing expressions, use parentheses as you learn to use the rules!

Mixed Mode Expressions

Mixing types within a c++ program generally is a problem.

Numerical expressions are an exception.

Logic dictates that a float plus an int is a float.

As you evaluate an expression, remember TYPE at each stage.

int m;

m = 5.5 + 5 / 2

5.5 + 2 // integer division

5.5 + 2.0 // convert to float

7.5 // float addition

7 // convert to int

BE SURE TO REVIEW TEXT EXAMPLES

Page 14: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Expression Example 1

P2 - P1

t2 - t1

V =The formula for the average velocity, v, of a particle traveling on a line between points p1 and p2 in time t1 and t2 is

The formula can be written and evaluated in c++ as follows:

V = (P2 - P1) / (t2 - t1)

-

/

-

V

1

3

24.5 9.0 0.0 60.0

P1 P2 t1 t2

V = (P2 - P1) / (t2 - t1) 9.0 4.5 60.0 0.0

4.5 60.0

0.075

Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al

Page 15: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Expression Example 2

Another expression:

The formula can be written and evaluated in c++ as follows:

z - (a + b / 2) + w * -y

z - (a + b / 2) + w * -y

DONE

1 -3

6

*4

/

2 +

5 -

+

Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al

8

z a b w y

8 3 9 2 -5

4 5

11

3 9 2 -5

z - (a + b / 2) + w * -y

7 10

1 . Unary operators first!

Assuming all values are integer!

Page 16: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Another expression:

The formula can be written and evaluated in c++ as follows:

z - (a + b / 2) + w * -y

z - (a + b / 2) + w * -y

DONE

1 -3

6

*4

/

2 +

5 -

+

Fig. 2.13, Problem Solving and Design in C, Addison-Wesley, by Jeri R. Hanly, et. al

8

z a b w y

8 3.0 9 2 -5

4 5

11.0

3.0 9 2 -5

z - (a + b / 2) + w * -y

7.0 10

1.0 . Unary operators first!

Assuming ONLY a is float!

Expression Example 3 - MIXED mode

Page 17: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Redirection

Your Lab will show you how to do this!

PROBLEM:

How do you make your program read a file?

helps rerun the program without having to type the data over

helps to substitute new data setsno changes to program, only how you RUN it!

1. build a data file with an editor (TESTDATA)

2. instead of running the program as

a.out

use

a.out < TESTDATA

When you run the program, you WON'T have to type the input data. It will be read from the file instead.

NO PROGRAM CHANGE

A similar method exists for writing output to a file

Page 18: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Standard Input&

Standard Output

CIN -> Reads from the standard input

COUT -> Writes to the standard output

REDIRECTION simply redefines the standard input and/or standard output

to be a file

What if you wanted to read (write) 3 files at the same time?

ANSWER:

Redirection won't work.

You need another method which we'll learn later.

Page 19: Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.

Program Structure

// Any comments // David Game

// typically AUTHOR, filename // Assignment 1

// date, assignment, etc.

#include directives #include <iostream.h>

void main ( ) void main ()

{ {

constant declarations const float x=1.2;

variable declarations int y;

executable statements y = x + 3;

(cin,cout, assignment) cout << y;

} }

Always read “Good Programming Practices” at the end of each chapter