26
Computer Programming University Of Salahaddin Engineering College Electrical Engineering 2009-2010

C++ for beginners

Embed Size (px)

Citation preview

Page 1: C++ for beginners

Computer ProgrammingUniversity Of SalahaddinEngineering College Electrical Engineering2009-2010

Page 2: C++ for beginners

What we learnWe will begin the study of the language starting with the

fundamentals of the language and simple programs; and as we explore more of the language, we will write increasingly larger programs.

By the end of this year, every student:• will learn about all the essential programming concepts• will demonstrate a good familiarity with C++ syntax• will be able to write reasonably complex procedural

programs

Page 3: C++ for beginners

What We Will LearnThe following topics will be covered in this course:

• Flow charts and algorithms• Variables and assignment• Input, output (I/O), formatting output• Basic data types• A simple C++ program• Arithmetic operators• Flow of control and loops• Program style• Procedures and functions• Local and global variables, constants• File I/O• Structures and arrays• Numerical analysis

Page 4: C++ for beginners

Problem SolvingEngineers use their knowledge of science, mathematics, and

appropriate experience to find suitable solutions to a problem. Engineering is considered a branch of applied mathematics and science. Creating an appropriate mathematical model of a problem allows them to analyze it (sometimes definitively), and to test potential solutions.

Usually multiple reasonable solutions exist, so engineers must evaluate the different design choices on their merits and choose the solution that best meets their requirements.

Page 5: C++ for beginners

How To Solve Problems1.Define the problem and see if there is a better way to

redefine it.2.Find the way how human solves it.3.Write down human’s solution4.Break down this solution to its original steps and do not

forget simplest thoughts.5. match every human activity with computer’s tool.6.If some activity don’t exist directly in computer find

alternatives.7.Group these tiny computer actions into logically related

functions.8.Combine these functions in a complete solution.9.Solution Implementation and Verification

Page 6: C++ for beginners

An Algorithm: Baking a Cake

Page 7: C++ for beginners

An Algorithm: Baking a Cake (cont)

Page 8: C++ for beginners

FlowchartA flowchart is a common type of diagram, that represents an

algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows.

Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.[1]

Page 9: C++ for beginners

Flowchart (cont)

Page 10: C++ for beginners

Flowchart (cont)

Page 11: C++ for beginners

Flowchart (cont)

Lamp Fixing Flowchart.

Page 12: C++ for beginners

Flowchart (cont)

Game of Monopoly Flowchart.

Page 13: C++ for beginners

Pseudocode This is the pseudocode for a Game of Monopoly, including one

person's move as a procedure:

Page 14: C++ for beginners

Exercise1.Find a solution for this equation x^2 + x – 9 = 18. using

pseudocode and flowchart.2.How can you replace a broken door.3.Write a pseudocode that illustrate kids going to the school

steps.4.Draw a flowchart for buying a house.

Page 15: C++ for beginners

Flowchart Example #1 Draw a flowchart to

find the sum of first 50 natural numbers

Page 16: C++ for beginners

Flowchart Example #2 Draw a flowchart to find the largest of three numbers A,B,

and C.

Page 17: C++ for beginners

Flowchart Example #2 Draw a flowchart for

computing factorial N (N!)

Page 18: C++ for beginners

Exercise1.Draw a flowchart to read a number N and print all its

divisors.2.Draw a flowchart for computing the sum of the digits of any

number 3.Draw a flowchart to find the sum of given N numbers. 4.Draw a flowchart to compute the sum of squares of integers from 1

to 50 5.Draw a flowchart to arrange the given data in an ascending

order.

Page 19: C++ for beginners

VariablesA C++ variable is a placeholder used for storing data. The

type of data may be numerical, string, character and any other type that the language uses it.

The data in a variable is called its value and it can be changed or deleted. in order to uniquely identify and keep track of a variable, we need to give each variable a name; this is called declaring a variable. it is a good idea to use meaningful names for variables. For example:

int books;// this is variable declarationIt’s common to initialize variables upon declaration. if a

variable is used in a program without being initialized, the results will be unpredictable. Variable initialization can be performed either during declaration using assignment statements as in:

int books = 0; // this is variable declaration and initialization

Page 20: C++ for beginners

Variables (cont)

Values can be assigned or stored in variables with assignment statements:

books=34; //this is assignment statement An assignment statement is an order, to the computer, to

assign the value on the right-hand side of the equal sign to the variable on the left-hand side.

Page 21: C++ for beginners

Basic Data TypesTell which continent your country is located on and which

countries are its neighbors.1- Integer Numbers (int)2- Real Numbers(double)

Page 22: C++ for beginners

Basic Data Types (cont)

3- Characters (char)char letter, symbol;letter = ‘A’;symbol=‘#’;

4- Boolean expressions (bool)bool you = true;

important noteAs a general rule, you cannot store values of one type in a

variable of another type. This is a good rule to follow, even though some C++ compilers do not enforce this type checking.

Page 23: C++ for beginners

Assignment StatementsValues can be assigned or stored in variables with assignment

statements:books=34;

An assignment statement is an order, to the computer, to assign the value on the right-hand side of the equal sign to the variable on the left-hand side. The sign (=) is called the assignment operator. (C++ operators will be covered later in the course). Assignment statements end with a semicolon.

The value on the right-hand side can also be another variable or expression:

books1=books2;In an assignment statement, first the value on the right-hand side

is evaluated and then its result is stored or assigned to the variable on the left-hand side.

Page 24: C++ for beginners

Performing OutputThe values of variables, numerical values and strings of text ,may

be output to the screen using cout as in

int books=0;cout<<books<<endl;cout<<72<<endl;cout<<“This is the output”<<endl;

In the first output statement, the value of variable books will be output on the screen, which is 0;

In the second, the numerical value 72 will be output to the screen, and in the last statement, the string “This is the output” will be output to the screen.

Page 25: C++ for beginners

Input Using cinin C++, cin is used to input values into variables. After declaring

a variable of type int,

int price;cout<<“Enter the price:”;cin>>price;cout<<“The price you entered is $“<<price<<endl;

in this program extract, first an integer is declared, then the user is asked to enter/type a value for price. This value is then input in the variable price using cin. A message is also output to the screen notifying the user of the input value.

When the program reaches a cin statement, it waits for input to be entered from the keyboard and for this value to be input into the Variable, the user must enter a new line.

Page 26: C++ for beginners

A simple C++ ProgramNow, we will write our first complete C++ program. #include <iostream.h>main( ){

int hours=0;cout<<“This is your first C++ program”<<endl;cout<<“Welcome to C++ Programming”<<endl;cout<<endl<<endl;cout<<“How many hours/week do you practice C++ ?”;cin>>hours;cout<<“You practice “<<hours<<“ per week.”<<endl;cout<<“OK, this is enough for now.”<<endl;return 0;

}