• No results found

Intro to Programming

N/A
N/A
Protected

Academic year: 2021

Share "Intro to Programming"

Copied!
33
0
0

Loading.... (view fulltext now)

Full text

(1)

Intro to Programming Week # 2

Variables/Data type Lecture # 4 & 5

By: Saqib Rasheed

Department of Computer Science & Engineering Air University

(2)

// First Program

// Author Saqib Rasheed

#include <iostream>

using namespace std;

int main() {

cout << "Hello world!";

return 0;

}

A First Program - Greeting.cpp

(3)

Greeting Output

By Saqib Rasheed 3

(4)

Whitespace

#include <iostream>

using

namespace std;

int main () { cout

<<

“Hello word!\n”

; return

0;}

(5)

• Reference Chapter # 2:

C++ Programming Basics by Robert Lafore

By Saqib Rasheed 5

(6)

Escape Sequence

• Escape sequences are used to represent certain special characters within string

literals and character literals.

• \n new line

• \t horizontal tab (8 spaces)

• \’ single quote

• \” double quote

• \? question mark

• \v vertical tab

• \a audible bell

(7)

Example # 2

#include <iostream>

using namespace std;

int main ( ) {

cout << “\t Welcome to Air University \n“;

cout<<“ \n\t School of Engineering”;

cout<<“it can be\n used \t between\n the string”;

return 0;

}

New Line Tab

By Saqib Rasheed 7

(8)

Variables

• A storage box, its type defines the kind of stuff you store in it

– E.g. shoe box  used for shoes

– Jewelry box  used for jewelry

– CD kit  used for CDs

(9)

Variable

• Variable is a box

• In computer, a storage box is a memory location on RAM

By Saqib Rasheed 9

(10)

Memory in Computer

• First you ask computer to give you a box and you also have to specify what type of box you want

Occupied Memory Spaces

Free Memory Spaces

Each little box is a memory

location

(11)

Asking for Memory

Type of Box box name;

Type is a bit

messy, it can’t be shoe box or

jewelry

Could be anything, alpha, bravo, charlie

integer character double float

bool (etc)

Some rules apply 

By Saqib Rasheed 11

(12)

Rules for Variable name

Rules to Write Variable.

1. First Letter is always Character

2. Underscore _ (Not Recommended) 3. Can’t use real numbers (Syntax Errorr) int num1;

(13)

Rules for Variable name

In a program a variable has:

1. Name 2. Type 3. Size 4. Value

int num = 10;

Assignment Operator

By Saqib Rasheed 13

(14)

Rules of variables

• int n1;

• int n1,n2,n3;

• int n1; int n2; int n3;

• float a1; int b1; double c1;

(15)

Type Size (bytes) Range-Minimum Range- Maximum

 Character 1 -128 127

Unsigned Char 1 0 255 Signed Char 1 -128 127

Integer 4 -2,147,483,648 2,147,483,647 Signed Int 4 0 ,4294,967,295

Unsigned Int 4 -2,147,483,648 2,147,483,647 Short Int 2 -32,768 32,767

Signed short Int 2 0 65,535

Unsigned Short Int 2 -32,768 32,767

Long Int 4 -2,147,483,648 2,147,483,647 Signed Long Int 8 0 4,294,967,295

Unsigned Long Int 8 -2,147,483,648 2,147,483,647 Float 4 1.8E-38 3.4E+38

Double 8 2.2E-308 1.8E+308 Long Double 8 2.2E-308 1.8E+308

Bool 1 True/False

By Saqib Rasheed 15

(16)

How To get Value from User

How to get value from user we use

cin>>x;

Also defined in <iostream> library file

16

(17)

Displaying age

#include<iostream>

using namespace std;

int main (){

float a1,a2;

cout<<“Enter age for student 1=”;

cin>>a1;

cout<<“Enter age for student 2=”;

cin>>a2;

cout<<a1;

cout<<a2;

return 0;

} By Saqib Rasheed 17

(18)

Displaying age

#include<iostream>

using namespace std;

int main (){

float a1,a2;

cout<<“Enter age for student 1=”;

cin>>a1;

cout<<“Enter age for student 2=”;

cin>>a2;

cout<<“\nAge of Student 1 = ”<<a1;

cout<<“\nAge of Student 2 = ”<<a2;

return 0;

(19)

Displaying age

#include<iostream>

using namespace std;

int main (){

float a1,a2;

cout<<“Enter age for student 1=”;

cin>>a1;

cout<<“Enter age for student 2=”;

cin>>a2;

cout<<“\nAge of Student 1 = ”<<a1;

cout<<“\nAge of Student 2 = “ <<a2;

return 0;

} By Saqib Rasheed 19

(20)

Displaying age

#include<iostream>

using namespace std;

int main (){

float a1,a2;

cout<<“Enter age for student 1=”;

cin>>a1;

cout<<“Enter age for student 2=”;

cin>>a2;

cout<<“\nAge of Student 1 = ”<<a1;

cout<<“\nAge of Student 2 = “;

cout <<a2;

return 0;

(21)

Average age Example

#include<iostream>

using namespace std;

int main (){

float a1,a2,a3,a4,a5;

cout<<“Enter age for student 1=”;

cin>>a1;

cout<<“Enter age for student 2=”;

cin>>a2;

: :

float sum, avg;

sum = a1+a2+a3+a4+a5;

Avg = sum / 5;

cout<<“Average age = ”<<avg;

return 0;

}

By Saqib Rasheed 21

(22)

Arithmetic Operators

• Common

– Addition + – Subtraction - – Multiplication * – Division /

– Mod %

• Note

– No exponentiation operator – Single division operator

– Operators are overloaded to work with more than one type of object

Write m*x + b not mx + b

(23)

Integer Division

• Integer division produces an integer result

– Truncates the result

• Examples

– 3 / 2 evaluates to 1 – 4 / 6 evaluates to 0 – 10 / 3 evaluates to 3

By Saqib Rasheed 23

(24)

Mod

• Produces the remainder of the division

• Examples

– 5 % 2 evaluates to 1 – 12 % 4 evaluates to 0 – 4 % 5 evaluates to 4

(25)

Operators and Precedence

• Consider mx + b

Consider m*x + b which of the following is it equivalent to

(m * x) + b

m * (x + b)

Operator precedence tells how to evaluate expressions

Standard precedence order

() Evaluate first, if nested innermost done first

* / % Evaluate second. If there are several, then evaluate from left-to-right

+ - Evaluate third. If there are several, then evaluate from left-to-right

By Saqib Rasheed 25

(26)

Operator Precedence

• Examples

20 - 4 / 5 * 2 + 3 * 5 % 4

(4 / 5)

((4 / 5) * 2)

((4 / 5) * 2) (3 * 5)

((4 / 5) * 2) ((3 * 5) % 4) (20 -((4 / 5) * 2)) ((3 * 5) % 4) (20 -((4 / 5) * 2)) + ((3 * 5) % 4)

(27)

27 By Saqib Rasheed

Precedence

21.2 192 19.2

24 8

X = 2 + 4 * 6 ( 10 – 2 ) / 10 Y = 10 / 5 * 2 ( 8 - 2 ) + 6

Write Code for this equation

12 3

5

(28)

Quadratic Equation

• In algebra

y = ax2 + bx + c

• In C

y = a*x*x + b*x + c

(29)

29 By Saqib Rasheed

#include <iostream>

using namespace std int main ( ) {

int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ;

cout << " x = " ; cout << x ;

cout<< " Y = " <<y;

cout << " z =x + y " << x+y ; }

(30)

• int x, y, z ;

• int x; int y; int z ;

• char n1, n2 = ‘a’;

• float num1, num2 = 30.8, n3;

• double n3;

Declaration of Variables

(31)

Calculator Code

#include <iostream>

using namespace std int main ( ) {

int num1, num2, res;

cout<<“Enter Number 1 =”;

cin>>num1;

cout<<“Enter Number 2 =”;

cin>>num2;

res = num1 + num2;

cout<<“Addition Result = ”<<res;

res = num1 - num2;

cout<<“Subtraction Result = ”<<res;

res = num1 * num2;

cout<<“Multiplication Result = ”<<res;

res = num1 / num2;

cout<<“Division Result = ”<<res;

res = num1 % num2;

cout<<“Modulation Result = ”<<res;

} 31

(32)

Assignment # 1

• Get five equations and write code for each equation

– The depth of equation should be at least 6 numbers – Get these numbers from user (Key board)

– Display the equation properly (Before ) – Display the Result

• Instructions

– Individual assignment – Due date is next class

– Copied assignments will be marked zero – Late assignments not accepted

– Submit hard copies along with out put (Screen Shots)

(33)

• Reference Chapter # 2:

C++ Programming Basics by Robert Lafore

https://sites.google.com/site/saqibrashied/cplusplus

By Saqib Rasheed 33

References

Related documents

It is a 2 nd level Data Flow Diagram where after entering STUDENT LOGIN page he/she can select a book issue option where after entering the book detail, he/she can select the

Some of our guests whom we welcome in our special edition are Yaşar Çelen the partner of TTG Travel, Suat Akgül Executive Board President of Faros Group, Ali Akkaş the founder

[r]

There is very little documented evidence of the use of ePortfolios in early childhood education (ECE) and as such little is known about how they could impact on parent

• Supervised Spectral: a WCFG learned from structured strings using the algorithm of sec- tion 3.2.. We choose as basis the most frequent insides and outsides observed in the

“And then shall appear the sign of the Son of man in heaven: and then shall all the tribes of the earth mourn, and they shall see the Son of man coming in the clouds of heaven

When the trust income is distributed to a beneficiary and when there are items of capital gain allocated to principal, the principal may bear an increased tax

Mosquito larvae shared 42% (816 bacterial OTUs) of the bacterial communities with the water column and bulrush leaves (Figure 5) and these shared OTUs accounted for 99% of all