1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

41
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A

Transcript of 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

Page 1: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

1

C++ Programming BasicsChapter 2 Lecture

CSIS 10A

Page 2: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

2

Agenda

Review An Intermediate Program

Making a Program Interactive

Assignment Statements

Standard Program Structure

File output

Debugging

Page 3: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

3

Review –what does this show?

int x;x = 8; cout << "x=" <<x<<endl;cout << x << endl << "=x";

Page 4: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

4

Review –Identify each piece#include <iostream>using namespace std;

int main(){

// this program stores data in a variableint m;cout << “Enter the value of m:”;cin >> m;cout << “m = “ << m << endl;

system(“pause”); return 0;}

Page 5: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

5

Variables and AssignmentsVariables are like small blackboards

We can write a number on them

We can change the number

We can erase the number

C++ variables are names for memory locationsWe can write a value in them

We can change the value stored there

We cannot erase the memory location• Some value is always there

Page 6: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

6

Review –what does this show?

int x;x=6;x=8;cout << "x = " << x;

Page 7: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

7

IdentifiersVariable names are called identifiersChoosing variable names

Use meaningful names that represent data to be storedFirst character must be

• a letter• the underscore character _

Remaining characters must be• letters• Numbers• underscore character

Page 8: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

8

Which identifiers are legal?

feet Average score

1993tax Average_score

Sum Average.score

Valid?

FINAL

final

Page 9: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

9

Keywords

Keywords (also called reserved words)Are used by the C++ language

Must be used as they are defined in the programming language

Cannot be used as identifiers

Examples (75 altogether):• main, while, if, new, for, do, class, struct, cin, cout

Page 10: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

10

Declaring VariablesBefore use, variables must be declared

Tells the compiler the type of data to store

Examples: int one_weight, total_weight;

int represents whole numbers• Could store 1, 4, -345, etc.

• one_weight and total_weight are both of type int

• Many other types, but for now, just use int!!!

Page 11: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

11

Declaring Variables (Part 2)

int main(){

int score1, score2, sum; cin>> … etc … sum=score1+score2; … etc …

return 0; }

Put all your variable declarations at the top of main

Page 12: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

12

Declaring Variables (Part 3)

Declaration syntax:Type_name Variable_1 , Variable_2, . . . ;

Declaration Examples:int m_score, total_score;

float moon_distance;

int age, num_students;

char grade;

ONLY USE TYPE int FOR NOW!!!

Page 13: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

13

Agenda

Review

An Intermediate Program Making a Program Interactive

Arithmetic

Standard Program Structure

Debugging

Page 14: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

14

An intermediate C++ program:// height.cpp// Convert height in feet to inches.#include <iostream>using namespace std;int main(){int feet, inches;feet = 6;inches = feet * 12;cout << "Height is " << inches << " in.";return 0;}

Page 15: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

15

Execution – what you see

Height is 72 in.

Page 16: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

16

Execution – what really happensint feet, inches;

feet

int

int

?inches

?

Page 17: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

17

Execution – what really happensfeet=6;

feet

int

int

?inches

6

Page 18: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

18

Execution – what really happensinches = feet * 12;

feet

6*12

int

int

72inches

6.0

Page 19: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

19

Execution – what really happenscout << "Height is " << inches << " in.";

feet

int

int

72inches

6.0

Height is 72 in.

Page 20: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

20

Agenda

Review

An Intermediate Program

Making a Program Interactive Arithmetic

Standard Program Structure

Debugging

Page 21: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

21

Interactive Program

The previous program always runs exactly the same way (Height is 72 in)

We’d like to be able to “put in” the data we want to process

So we add two more lines. One asking for data: cout<<“What is height in feet?”<<endl;

The other takes input: cin >> feet;

Page 22: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

22

An intermediate C++ program:// height.cpp// Convert height in feet to inches.#include <iostream>using namespace std;int main(){int feet, inches;cout << “What is height in feet?” << endl; //********cin >> feet; //********inches = feet * 12; cout << "Height is " << inches << " in.";return 0;}

Page 23: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

23

Basic Interactive Program

int main()

{

1) Declare Variables

2) Ask (prompt) for data

3) Input data

4) Calculate result

5) Display result with message

return 0;

} Go back a slide and number the lines

Then open Lab1Basics.cpp and do prob1

Page 24: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

24

A clever use of comments

Inside Lab1Basics.cpp are 10 programming problems…how to keep separate?

Remember // ignore to end of line

Another type of comment works in “blocks” :/* start a block comment

ignored

end a block comment with */

Page 25: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

25

Combine the two…

/**** Problem 1 ********

// Here are your instructions

(space for your code)

// *****End Problem 1 *****/

/**** Problem 2 ********

(etc..)

Make // to work

on this problemchange back to /

when done

TRY IT NOW! Do problems 1-3 in Lab1Basics.cpp

Page 26: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

26

Agenda

Review

An Intermediate Program

Making a Program Interactive

Arithmetic Standard Program Structure

Debugging

Page 27: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

27

Assignment StatementsAn assignment statement changes the value of a variable

total_weight = one_weight + number_of_bars; • total_weight is set to the sum one_weight + number_of_bars

Assignment statements end with a semi-colon

The single variable to be changed is always on the leftof the assignment operator ‘=‘

On the right of the assignment operator can be• Constants -- age = 21;• Variables -- my_cost = your_cost;• Expressions -- area = length * width;

Page 28: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

28

Assignment Statements and Algebra

The ‘=‘ operator in C++ is not an equal signThe following statement cannot be true in algebra

number_of_bars = number_of_bars + 3;

In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3

Page 29: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

29

Initializing Variables

Declaring a variable does not give it a valueGiving a variable its first value is initializing the variable

Variables are initialized in assignment statements

int miles, area; // declare the variables miles = 26; // initialize the variable

area=0; // initializeDeclaration and initialization can be combined

int miles = 26, area = 0.0;Variables that are going to be input using cin>> or calculated later do not need to be initialized

Page 30: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

30

Arithmetic in C++

Basic operations + - * /A = r2 area=radius*radius*3.14;

F = (v -a) force=(v-a)/(v+a);

(v+a)

More on arithmetic later. C++ has some interesting properties to be explored next chapter.

Page 31: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

31

Can you guess what is stored in x?

int a, b=3, c, x;

a=b*2;

c=a+3;

x=c-2;

Program flow is sequential (top to bottom)

a b c x

? 3 ? ?

6 3 ? ?

6 3 9 ?

6 3 9 7

Page 32: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

32

Your turn…what is stored in x?

int a, b, c, x=3;

a=2;

x=3+a;

x=x+a;

Program flow is sequential, order matters!!

a b c x

Page 33: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

33

Agenda

Review

An Intermediate Program

Arithmetic

Standard Program Structure Debugging

Page 34: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

34

Standard Program Structure

To solve most problems, your main() program will generally look like this (conceptually)

1. Declare variables for input, result and intermediate data

2. Ask for data3. Input data (cin)4. Calculate result5. Output result (cout)

Page 35: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

35

Another interactive C++ program:// Convert input number of nickels and dimes// to cents.#include <iostream>using namespace std;

int main(){ int nickels, dimes, cents; cout << "Enter number of nickels and dimes: "; cin >> nickels >> dimes; cents = 5 * nickels + 10 * dimes; cout << nickels << " nickels and " << dimes << " dimes " << "= " << cents << " cents " << endl; return 0;}

Notice cascading input

Page 36: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

36

Your Turn

Run the previous code, found in Lab1Basics problem4, and verify it works correctly

Then modify the program to input dimes and quarters and calculate the equivalent cents

Page 37: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

37

Agenda

Review

An Intermediate Program

Arithmetic

Standard Program Structure

Debugging

Page 38: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

38

Syntax Errors // errors.cpp#include <iostream> using namespace std;

int main(){ int n; cout << "Hello" << endl; N = 2; cout << "n eqalls " << n cout << "So long << endl; system("pause"); return 0;}

This program has 3 errors

Run Problem 5 and see what they are

Page 39: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

39

Tracking them down

Syntax—double click on the first error message

it takes you to that line of code

Look at or above that line for possible errors

Fix it and try again

Errors often create other errorsJust fix the first one, then recompile

Page 40: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

40

That’s a wrap !

What we learned today:How to write an intermediate C++ program

Standard program structure

Assignments and Basic Arithmetic

How to debug your program

Page 41: 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

41

Go back home proud ! You’re a C++ programmer !