Computer programming chapter ( 3 )

56
Prepaid By: Eng. Ibrahim Elewah Higher Technological Institute 10 th of Ramadan City 6 th of October Branch Electrical and Computer Engineering Department Main Reference HTI Student Book and “C For Dummies” by Dan Gookin 2 nd Edition Lecture Notes in 1

description

C Programming CS 002 Computer Programming

Transcript of Computer programming chapter ( 3 )

Page 1: Computer programming chapter ( 3 )

LOGO

Prepaid By: Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition

Lecture Notes in

1

Page 2: Computer programming chapter ( 3 )

Course Contents

Introduction

C Programming Higher Technological Institute2

1

Program Development2

The Essential of C Programs3

Manipulating Data with Operators 4

Reading from and Writing to Standard I/O5

Decision6

Iteration7

Arrays8

C Functions9

Page 3: Computer programming chapter ( 3 )

Course Contents

C Programming Higher Technological Institute3

The Essential of C Programs3

Constants and variables

Expressions

Arithmetic operators

Statements

Statement blocks

Data Types and Names in C

Naming a Variable

Page 4: Computer programming chapter ( 3 )

Expressions

o An expression is a combination of constants,variables, and operators that are used todenote computations

1. Taking the value contained in the drawer(variable) A

2. Multiply this value by 2

3. Subtract 1 from result obtained from 2

4. The value contained in drawer A is omitted, thenputting the result obtained from 3 into drawer A.

C Programming Higher Technological Institute4

𝑨 = 𝟐 ∗ 𝑨 − 𝟏

Page 5: Computer programming chapter ( 3 )

Arithmetic Operations

C Programming Higher Technological Institute5

Addition

DivisionMultiplication

Subtraction

Reminder

The Reminder operator % is used to obtainthe reminder of the first operand divided bythe second operand

Page 6: Computer programming chapter ( 3 )

Arithmetic Operations

C Programming Higher Technological Institute6

Addition

DivisionMultiplication

Subtraction

Reminder

𝟔% 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 =

Example

Page 7: Computer programming chapter ( 3 )

Arithmetic Operations

C Programming Higher Technological Institute7

Addition

DivisionMultiplication

Subtraction

Reminder

𝟔% 𝟒 = 𝟐 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 𝟒𝟖%𝟓 = 𝟑

Example

Page 8: Computer programming chapter ( 3 )

Constants and Variables

o As its name implies, a constant is a value that neverchanges.

o A variable, on the other hand, can be used to presentdifferent values.

o For instance, consider the following:

𝒊 = 𝟏 ;o Where the symbol i is a constant because it always has

the same value (1) and the symbol i is assigned theconstant 1.

o In other words, i contains the value of 1 after thestatement is executed.

C Programming Higher Technological Institute8

Page 9: Computer programming chapter ( 3 )

Data Types and Names

oThe C language reserves some keywordswords that have special meanings to thelanguage.

oThose reserved words should not be usedas variables, constants, or function namesin your program.

oAll C keywords must be written inlowercase letters, for instance INT will notbe treated as a keyword, it must be writtenas int.C Programming Higher Technological Institute9

Page 10: Computer programming chapter ( 3 )

The computer list of C keywords

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

C Programming Higher Technological Institute10

Page 11: Computer programming chapter ( 3 )

Naming a Variable

o Characters A through Z and a through z

o Digit characters 0 through 9, which can be used in any position except the first of a variable name.

o The underscore character _

Examplesstop_sign loop3 and_pause

C Programming Higher Technological Institute11

Valid Variable Name Can Use

Page 12: Computer programming chapter ( 3 )

Naming a Variable

o A variable name can’t contain any C arithmetic signs.

o A variable name can’t contain any dots.

o A variable name can’t contain any apostrophes.

o A variable name can’t contain any other special symbols such as *, @, #, and so on.

Examples4flags sum-result method*4

return what_size? ahmed.ali

C Programming Higher Technological Institute12

Invalid Variable Name Can NOT be Used

Page 13: Computer programming chapter ( 3 )

Data Types

C Programming Higher Technological Institute13

C Data Type

char

a, B, $, #

int

5, 17, 128

float

2.5 , 0.3

double

23433.3455

Page 14: Computer programming chapter ( 3 )

Declaration Statement

C Programming Higher Technological Institute14

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 = ‘&’ ;

int n1 = 100 ;

Page 15: Computer programming chapter ( 3 )

Declaration Statement

C Programming Higher Technological Institute15

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 ;

int n1 ;

c1 = ‘&’ ;

n1 = 100 ;

Page 16: Computer programming chapter ( 3 )

Declaration Statement

C Programming Higher Technological Institute16

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1= 32/100 ;

double d1=5e3 ;

Page 17: Computer programming chapter ( 3 )

Declaration Statement

C Programming Higher Technological Institute17

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1 ;

double d1 ;

f1 = 32/100 ;

d1 = 5e3 ;

Page 18: Computer programming chapter ( 3 )

Some special characters in C

\b BackspaceMoves the cursor to the left one character

\f Form feedGoes to the top of a new page

\n New lineCarriage return and line feeds

\r ReturnReturns to the beginning of the current line

\t TabAdvances to the next tab stop

C Programming Higher Technological Institute18

Page 19: Computer programming chapter ( 3 )

Examples

/* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

return 0;

}

C Programming Higher Technological Institute19

Page 20: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute20

Page 21: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute21

printf ( “ The character c1 is : %c \n ”, c1);

Page 22: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute22

printf ( “ The character c1 is : %c \n ”, c1);

Page 23: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute23

printf ( “ The character c1 is : %c \n ”, c1);

Page 24: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute24

printf ( “ The character c1 is : %c \n ”, c1);

New Line

Specifier

for

Character Data Type

Page 25: Computer programming chapter ( 3 )

Examples /* Example1 : Printing out characters */

# include <stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 13 */

char c1; /* declaration of the character variable c1 */

char c2; /* declaration of the character variable c2 */

c1 = ‘A’; /* assigning c1 with the character constant A */

c2 = ‘a’; /* assigning c2 with the character constant a */

printf ( “ The character c1 is : %c \n ”, c1);

printf ( “ The character c1 is : %c ” , c1);

printf ( “ while the character c2 is : %c \n”, c2);

return 0;

}

C Programming Higher Technological Institute25

Page 26: Computer programming chapter ( 3 )

You Have To Know about Data Types

C Programming Higher Technological Institute26

Type

char

int

float

double

Name

c1

n1

f1

d1

Value

‘&’

100

32/10

5e3

Specifier

%c

%d

%f

%e,%E

Page 27: Computer programming chapter ( 3 )

Examples /* The arithmetic operations on integers */

# include<stdio.h>

/* the header file for the printf () function */

main ( )

{ /* the main function body till line 15 */

int m = 3;

int n=2;

printf ( "The summation of %d and %d is : %d.\n", m, n, m+n);

printf ( "The difference between %d and %d is : %d.\n", m, n, m-n);

printf ( "The multiplication of %d by %d is : %d.\n", m, n, m*n);

printf ( "The division of %d by %d is : %d.\n", m, n, m/n);

printf ( "The remainder of division of %d by %d is : %d.\n", m, n, m%n);

return 0 ;

}

C Programming Higher Technological Institute27

Page 28: Computer programming chapter ( 3 )

Operators Precedence

Operator Sign Name

( ) Brackets

/ Division

* Multiplication

+ Addition

- Subtraction

C Programming Higher Technological Institute28

Page 29: Computer programming chapter ( 3 )

Logic Operators

C Programming Higher Technological Institute29

Operator Sign Name

| | OR

&& AND

!= NOT

Page 30: Computer programming chapter ( 3 )

Examples /* Example3 : Integer vs. floating point divisions */

# include <stdio.h> /* the header file for the printf ()

function */

main ( )

{

int n1,n2,n3;

float m1,m2,m3;

n1 = 32/10; m1= 32/10;

n2 = 32.0/10; m2= 32.0/10;

n3 = 32/10.0; m3 = 32/10.0;

return 0;

}

C Programming Higher Technological Institute30

Page 31: Computer programming chapter ( 3 )

Examples /* Example3 : Integer vs. floating point divisions */

# include <stdio.h> /* the header file for the printf () function */

main ( )

{

int n1,n2,n3; float m1,m2,m3;

n1 = 32/10; m1= 32/10;

n2 = 32.0/10; m2= 32.0/10;

n3 = 32/10.0; m3 = 32/10.0;

printf ( “ The integer division of 32/10 is : %d \n”, n1);

printf ( “The floating point division of 32/10 is : %f \n”, m1);

printf ( “The integer division of 32.0/10 is : %d \n”, n2);

printf ( “The floating point division of 32.0/10 is : %f \n”, m2);

printf ( “The integer division of 32/10.0 is : %d \n”, n3);

printf ( “The floating point division of 32/10.0 is : %f \n”, m3);

return 0;

}C Programming Higher Technological Institute31

Page 32: Computer programming chapter ( 3 )

Double Data Type

o Here are two examples:

[mantissa] e [exponent]

[mantissa] E [exponent]

Example

5000 5e3.

-300 -3e2

0.0025 2.5e-3.

C Programming Higher Technological Institute32

Specifier %e or %E

With printf ( )

Page 33: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute33

Page 34: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute34

Page 35: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute35

X = E * D – B / C + A ;

Page 36: Computer programming chapter ( 3 )

Precedence Example

X = E * D – B / C + A ;

C Programming Higher Technological Institute36

X = E * D – B / C + A ;

Page 37: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute37

X = E * D – B / C + A ;

Page 38: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute38

X = E * D – B / C + A ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 39: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute39

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20 A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 40: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute40

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20

20 – 2 + 20

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 41: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute41

X = E * D – B / C + A ;

2 * 10 – 6 / 3 + 20

20 – 2 + 20

38

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 42: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute42

Page 43: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute43

Page 44: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute44

Y = ( E * D ) – ( B / C ) + A ;

Page 45: Computer programming chapter ( 3 )

Precedence Example

.

.

.

C Programming Higher Technological Institute45

Y = ( E * D ) – ( B / C ) + A ;

Page 46: Computer programming chapter ( 3 )

Precedence Example

.

.

.

38

C Programming Higher Technological Institute46

Y = ( E * D ) – ( B / C ) + A ;

Page 47: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute47

Page 48: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute48

Z = A * ( D – B ) / ( C + E ) ;

Page 49: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute49

Z = A * ( D – B ) / ( C + E ) ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 50: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute50

Z = A * ( D – B ) / ( C + E ) ;

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 51: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute51

Z = A * ( D – B ) / ( C + E ) ;

20 * 4 / 5A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 52: Computer programming chapter ( 3 )

Precedence Example

C Programming Higher Technological Institute52

Z = A * ( D – B ) / ( C + E ) ;

20 * 4 / 5

16

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 53: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute53

Page 54: Computer programming chapter ( 3 )

Precedence Example

# include<stdio.h>

main ( )

{

int A,B,C,D,E, X, Y, Z;

A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ;

X = E * D – B / C + A ;

Y = ( E * D ) – ( B / C ) + A ;

Z = A * ( D – B ) / ( C + E ) ;

printf ( “ X= %d \n Y= %d \n Z=%d \n”, X,Y,Z);

return 0 ;

}

C Programming Higher Technological Institute54

Page 55: Computer programming chapter ( 3 )

Home Work ..!!Exercises 3 Page 44

C Programming Higher Technological Institute55

Page 56: Computer programming chapter ( 3 )

LOGO

Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

56

Main Reference

HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition