• No results found

C++ Programming Basics Chapter 1 Lecture CSIS 10A

N/A
N/A
Protected

Academic year: 2021

Share "C++ Programming Basics Chapter 1 Lecture CSIS 10A"

Copied!
77
0
0

Loading.... (view fulltext now)

Full text

(1)

C++ Programming Basics

(2)

Agenda

Hardware Basics  The IDE

My First Program

Its all G(r)eek to me

(3)

Anatomy of a Computer

Memory Output

ALU Control Input

(4)

The CPU uses machine language

(5)

Compilers to the Rescue!

High level languages like C++ allows writing code that is easier to understand and

universally works on any CPU. circle=2.0*radius*pi;

(6)

Agenda

Hardware Basics The IDE 

My First Program

Its all G(r)eek to me

(7)

THE MECHANICS OF WRITING

A PROGRAM

1. Editing -- Writing a program

2. Compiling -- Translating from C++ into machine language 3. Linking -- Combining your program with other libraries 4. Running – Letting the computer execute a program

5. Debugging – Running step by step through a program searching for mistakes

(8)

Agenda

Hardware Basics The IDE

My First Program  Its all G(r)eek to me

(9)

Hello World !

Lets start off the traditional way

Program that prints out “Hello World” on your output console (your screen)

(10)

Steps

1. Create new source file 2. Write the code

3. Create a workspace (only in MSVC++) 4. Compile

5. Link

(11)

Agenda

Hardware Basics The IDE

My First Program

Its all G(r)eek to me 

(12)

It’s all G(r)eek to me

… actually, its C++

(13)

20,000 ft. above sea level

#include <iostream> using namespace std; int main()

{

// This is my first program

cout << "Hello World" << endl; system("pause");

(14)

20,000 leagues under the sea

#include <iostream>

Actually includes some information into your code

Contains some definitions that are needed for your code

(15)

22,000 leagues under the sea

using namespace std;

This line refers to a set of standard object name definitions

(16)

… deeper….

int main()

• This is the piece of code (function) that is operated on first when a program is executed

• What’s a function ???

(17)

… even deeper….

//This is my first program

• This is a comment you write to yourself • Useful when writing large programs

(18)

… and deeper…..

Output operator

cout << “Hi”<<endl;

cout is the console monitor (your display)

<< is the output operator. Use to chain together your output message.

“Hi”is a string literal

(19)

… getting sleepy?…..

For the Bloodshed environment…Hold the display open so you can read it

(pressing a key will continue)

system("pause");

Finish up this program, return a 0 to operating system (everything ended OK)

(20)

… and even deeper ….

{} delineates the code block

(21)

Other types of cout statement

cout<< “ my age is” <<endl << 39<<endl; cout<< “ my age is 39”;

cout<< “ what’s your’s”; Escape with \

(22)

Tom’s All Purpose Program Shell

#include <iostream> using namespace std; int main()

{

// Your code here ...

system("pause"); return 0;

}

(23)

You Do It

Modify your hello world program to print your name, address and phone number (COULD BE FAKE!) on three lines:

Tom Rebold

1600 Pennsylvania Ave 123-456-7890

(24)

Lets get moving…..

We’ll learn as we proceed

You’ll be saying “Aha!” or “Oho!” when you hear these terms again

(25)

What more ?

(26)

Variables and Declarations

Variables represent storage locations in the computer’s memory

variable = expression

Assignment is from right to left

n = 5;

(27)

Using int Variables

int main() { int m,n; m = 44; cout << “m = “ << m; n = m + 33;

(28)

Clearing the haze

int m;

m

int

(29)
(30)

Want more ?

(31)

User Input with

cin

How shall I feed in data ?

Remember cout ?

Meet cin

cin >> m;

(32)

User Input with

cin

int main() {

cout << "Enter the value of m:"; cin >> m;

(33)

User Input with

cin

int main() {

(34)

User Input with

cin

int main() {

int m;

cout << "Enter the value of m:"; cin >> m;

(35)

Your turn ()

1. from hello.cpp File>SaveAs age.cpp

2. Delete all the cout statements. Add lines to: a) Declare a variable called age

b) Display a message asking for data (such as “tell me your age”)

c) read the data into age

(36)

That’s a wrap !

What we learned so far:

How to write a basic C++ program The structure of a program

Displaying to the console (cout) Variables

(37)

C++ Programming Basics

(38)

Agenda

Review 

An Intermediate Program

Making a Program Interactive Assignment Statements

Standard Program Structure File output

(39)
(40)

Review –Identify each piece

#include <iostream> using namespace std; int main()

{

// this program stores data in a variable

int m;

cout << “Enter the value of m:”; cin >> m;

cout << “m = “ << m << endl; system(“pause”);

(41)

Variables and Assignments

Variables 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 locations

We can write a value in them

(42)

Review –what does this show?

int x;

x=6;

x=8;

(43)

Identifiers

Variable names are called identifiers Choosing variable names

Use meaningful names that represent data to be stored

First character must be

• a letter

• the underscore character _

Remaining characters must be

• letters • Numbers

(44)

Which identifiers are legal?

feet Average score

1993tax Average_score

Sum Average.score

(45)

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):

(46)

Declaring Variables

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

(47)

Declaring Variables

(Part 2)

int main() {

 int score1, score2, sum;

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

… etc …

(48)

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;

(49)

Agenda

Review

An Intermediate Program  Making a Program Interactive Arithmetic

(50)

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;

(51)

Execution – what you see

(52)

Execution – what really happens

int feet, inches;

(53)
(54)
(55)

Execution – what really happens

cout << "Height is " << inches << " in.";

feet

int

72 inches

(56)

Agenda

Review

An Intermediate Program

Making a Program Interactive  Arithmetic

(57)

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:

(58)

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;

(59)

Basic Interactive Program

int main() {

1) Declare Variables

2) Ask (prompt) for data 3) Input data

4) Calculate result

(60)

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

(61)

Combine the two…

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

// Here are your instructions

(space for your code)

(62)

Agenda

Review

An Intermediate Program

Making a Program Interactive Arithmetic 

(63)

Assignment Statements

An 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 left of the assignment operator ‘=‘

On the right of the assignment operator can be

• Constants -- age = 21;

(64)

Assignment Statements and Algebra

The ‘=‘ operator in C++ is not an equal sign

The following statement cannot be true in algebra

number_of_bars = number_of_bars + 3;

(65)

Initializing Variables

Declaring a variable does not give it a value

Giving 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; // initialize

Declaration and initialization can be combined int miles = 26, area = 0.0;

(66)

Arithmetic in C++

Basic operations + - * /

A = pr2 area=radius*radius*3.14; F = (v -a) force=(v-a)/(v+a);

(v+a)

(67)

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 ?

(68)

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

(69)

Agenda

Review

An Intermediate Program Arithmetic

(70)

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 data

3. Input data (cin) 4. Calculate result

(71)

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;

(72)

Your Turn

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

(73)

Agenda

Review

An Intermediate Program Arithmetic

(74)

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

(75)

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 errors

(76)

That’s a wrap !

What we learned today:

How to write an intermediate C++ program Standard program structure

(77)

Go back home proud !

References

Related documents

C++ basics Diagnostic real-world algorithms Core Tools User/client Implementation Roadmap Object-Oriented Programming.. Classes and

In order to determine the workability properties of fresh self-compacting concrete samples, slump flow test, L-box test and V-funnel test were carried out on the sam- ples

Surface data Surface or  gridded data Surface  Roughness User input at any  resolution, or  1 degree User input at any  resolution User input at  any 

Over the medial motor cortex, the recorded movement- related slow potentials appear to have similar temporal duration, approximately 235 ms, irrespective of the frequency of

The paper explores the priority of productive expenditure (accumulation of physical capital) and basic needs spending (expenditure on social sector) in human development strategies

‰ Optical - uses a light beam to monitor

Click to view Web Link, click Chapter 5, Click Web Link from left navigation, then click Touchpad and Pointing Stick below Chapter 5?. Other

1) The concept of &#34;acting in concert&#34;: The ECLE are of the opinion that a uniform concept for the Takeover Bids Directive, the Transparency Directive and the