lecture 1

31
PROGRAMMING FUNDAMENTAL Engr. Haseeb Ahmad Khan

Upload: jehangir-khan

Post on 17-Feb-2017

131 views

Category:

Engineering


0 download

TRANSCRIPT

PROGRAMMING

FUNDAMENTAL

Engr. Haseeb Ahmad Khan

Instructor: Engr. Haseeb Ahmad Khan

Email: [email protected]

Course Literature

Text book

Object-Oriented Programming in C++, 3rd Edition

by Robert

Reference

Problem Solving, Abstraction, and Design Using

C++ by Frank L. Friedman, Elliot b. Koffman

Computer Programming in C/C++ by

Muhammad Tariq Siddiqi

Let us C++ by Yashavant Kanetkar

Instructions

A large number of simple operations can do

complex things

Computers require very detailed instructions

High and Low Level Programming

Languages

Low-level language (assembly, machine code)

Makes all operations explicit

Can be executed by the CPU

E.g. “Lift this foot, put it down there, lift that foot,

etc…”

High-level language (C++, Java, C#, etc)

Abstract away details

Simplify common operations

E.g. “Walk from here to there.”

1-6

From a High-level Program to an

Executable File

a) Create file containing the program with a text

editor

b) Run preprocessor to convert source file

directives to source code program

statements

c) Run compiler to convert source program

statements into machine instructions

d) Run linker to connect hardware-specific

library code to machine instructions,

producing an executable file

1-7

From a High-level Program to an

Executable File

Steps b)–d) are often performed by a single command or button click

Each step detects certain types of errors

Errors detected at any step will prevent

execution of the following steps

1-8

From a High-level Program to an

Executable File

Object Code

Linker

Executable Code

Source Code

Preprocessor

Modified Source Code

Compiler

1-9

Input, Processing, and Output

Three steps that many programs perform

1) Gather input data

- from keyboard

- from files on disk drives

2) Process the input data

3) Display the results as output

- send it to the screen or a printer

- write it to a file

Problem Solving

Programming is a process of problem solving

Good problem solving techniques need to be

followed to become a good programmer

One common problem-solving technique

includes:

Analyzing the problem

Outlining problem requirements

Designing steps to solve the problem

Algorithm

Algorithm

A step by step problem solving process in

which a solution is arrived at in a finite amount

of time

Let’s design an algorithm to find the perimeter

and area of a rectangle

What information do we need to calculate the

perimeter?

Length

Width

How do you calculate perimeter?

Perimeter = 2 x (length + width)

Algorithm

Algorithm to calculate the perimeter of

rectangle is:

Get the length of the rectangle

Get the width of the rectangle

Find the perimeter using the equation

Perimeter = 2 x (length + width)

Algorithms

Algorithm (or in short Algo) to calculate pay of a sales person at a store

Every sales person has a base salary. Every salesperson receive a bonus at the end of the month on the criteria:

If the salesperson has worked in store for less than 5 years, $10 is paid for every year salesperson has worked there

If the salesperson has worked in store for more than 5 years, $20 is paid for every year salesperson has worked here

Algorithm Continued

Salesperson can get additional bonus if:

The sales made are between $5000-$10000,

salesperson receives 3% commission on sale

The sales made are greater than $10000,

salesperson receives 6% commission on sale

Lets suppose:

baseSalary denotes the base salary

noOfServiceYears denotes the number of

years salespersons has worked in the store

totalSale denotes the total sales made by the

salesperson

additionalBonus denotes the additional bonus

Algorithm Continued

1. Get the baseSalary

2. Get noOfServiceYears

3. Calculate bonus using:

if (noOfServiceYears is less than or equal to 5

years)

bonus = 10 x noOfServiceYears

Otherwise

bonus = 20 x noOfServiceYears

4. Get totalSales

Algorithm Continued

5. Calculate addtionalBonus using:

If (total Sale is less than 5000)

additionalBonus = 0

Otherwise if (total sale is greater than or equal to

5000 but less than 10000)

additionalBonus = totalSale x 0.03

Otherwise

addtionalBonus = totalSale x 0.06

6. Calculate payCheck using:

payCheck = baseSalary + bonus +

additionalBonus

The Programming Process

1. Define what the program is to do

▫ Often in terms of inputs and outputs

2. Program design

▫ Break large problems into smaller sub-problems

▫ Design steps to solve those sub-problems

3. Write the program code and compile

▫ The compiler will detect syntax errors in the code

4. Run the program with test data for input

▫ Test data should validate whether the program does what it was designed to do

5. Repeat steps 2-4 until the program is successful

Getting Started

• C++ vs. English

• Syntax: rules about how tokens form statements

• Semantics: what the statements “mean” to the computer (what they instruct it to do)

C++ English

Token Word, punctuation

Statement Sentence

Block Paragraph

Getting Started

• Function: a named block

• By convention, every program must have a function named main

• The simplest C++ program: main()

{

}

• When the program runs, it executes the statements in the main function block

First C++ Program

/* My first simple C++ program */

main ()

{

cout<<“Welcome to C++!”<<endl;

}

Comments

Braces indicate start

and end of main

All C++ programs have a main function;

they also start at main

Function to print to screen

What to print

End of line

End of

statement

Building Statements

• Statements are made by combining tokens

according to the syntax rules

▫ C++ statements end with a ;

• There are several types of tokens

▫ Literals – piece of data

▫ Special symbols

▫ Keywords – reserved words

▫ Identifiers – variable identifiers

Literals

A piece of data

Integer 12

Floating-point Number 3.14

Character (surrounded by single quotes)

'A'

String (surrounded by double quotes) "Hello"

Print Statements

• Prints information on the computer screen

cout << "Hello there!";

cout << "Hello " << "there!";

cout << "Hello ";

cout << "there!";

• To get multiple lines of output on screen use endl

cout << "Hello, there!" << endl;

2-23

Print Statements

• Can print any literal

cout << 17 << “ desks”;

cout << “The letter” << „A‟;

cout << 178.33;

2-24

Comments

• Comments are for the reader, not the compiler

• Two types:

▫ Single line // This is a C++ program. It prints the sentence:

// Welcome to C++ Programming.

▫ Multiple line /*

You can include comments that can

occupy several lines.

*/

A C++ Program

#include <iostream.h>

#include <conio.h>

main()

{

cout << "My first C++ program." << endl;

cout << "The sum of 2 and 3 = " << 5 << endl;

cout << "7 + 8 = " << 7 + 8 << endl;

getch();

}

What do you think is

the output?

Sample C++ program

// This is a C++ Program

#include <iostream.h>

#include <conio.h>

main()

{

/* The output of this program is

just a stupid string

*/

cout << “Welcome to C++." << endl;

getch();

}

Every C++ program has a function called

main

Can you find 2 examples of

comments in the program?

Special Symbols

Special symbols

+

-

*

/

.

;

?

,

<=

!=

==

>=

Reserved Words (Keywords)

Reserved words, keywords, or word symbols

Include:

int

float

double

char

const

void

return

Identifiers

Consist of letters, digits, and the underscore

character (_)

Must begin with a letter or underscore

C++ is case sensitive

NUMBER is not the same as number

Two predefined identifiers are cout and cin

Unlike reserved words, predefined identifiers

may be redefined, but it is not a good idea

I hear and I forget,

I see and I remember,

I do and I understand

-Chinese Proverb