computer programming all chapters

284
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

Upload: ibrahim-ahmed

Post on 20-Jan-2015

243 views

Category:

Documents


7 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Computer programming all chapters

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 all chapters

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 all chapters

Course Contents

Introduction

Higher Technological Institute3

1

Computer Program

Programming Language

Machine Language

Assembly Language

High Level Language

The C Programming Language

Interpreter, Compiler and Assembler

C Programming

Page 4: Computer programming all chapters

Computer Program

A Computer Program is a sequence of instructions written to perform a specified task with a computer. The program has an executable form that the computer can use directly to execute the instructions.

The Executable program in its human-readable form is called thesource code. Computer source code is often written by computerprogrammers. It is written in a programming language.

Source Code may be converted into an executable file ( anexecutable program or a binary) by a compiler and later executed by a central processing unit. Alternatively, computer programs may be executed with the aid of an interpreter, or may be embedded directly into hardware.

Higher Technological Institute4C Programming

Page 5: Computer programming all chapters

Programming Language

A Programming language is an artificial language designed to communicate instructions to a computer.

Most programming language are purely textual; they use sequences of text including words, numbers and punctuation.

The programming languages are divided into three groups:

1. Machine Language

2. Assembly Language

3. High Level Language

Higher Technological Institute5C Programming

Page 6: Computer programming all chapters

Machine Language

Essentially, computers really understand only one language, which consists of zeroes and ones, also known as machine language.

Buto Difficult and take a Long Time to Read and Write

o Difficult to Detect and Correct the Errors

o It is also called Binary CodeC Programming

Page 7: Computer programming all chapters

Assembly Language

Higher Technological Institute7

o It uses some Mnemonic English codes instead of Binary Codes.

o Assembly language programs are easier to Readand Write than machine language programs.

o It Needs an Assembler to translate Mnemonic codes into Machine Language.

ExampleADD 5,9

SUB 7,3

C Programming

Page 8: Computer programming all chapters

Assembly Language

Higher Technological Institute8

o It uses some Mnemonic English codes instead of Binary Codes.

o Assembly language programs are easier to Readand Write than machine language programs.

o It Needs an Assembler to translate Mnemonic codes into Machine Language.

ExampleADD 5,9

SUB 7,3

But in Large ProgramsWriting a program in assemblylanguage can prove extremelyTime-consuming, and complicated.

C Programming

Page 9: Computer programming all chapters

High Level Language

Higher Technological Institute9

Because writing machine- or assembly-language

programs was so Difficult and Confusing, peopledeveloped additional languages that look more likehuman languages

With names such as JAVA, BASIC, and C.

o High Level Language needs Compiler or Interpreter.

C Programming

Page 10: Computer programming all chapters

High Level Language

Saving your programs into library and invoke them in next programs

Higher Technological Institute10

Readability

Maintainability

Portability

Reusability

Programs are easy to Read

Programs are easy to Maintain

Programs are easy to port across Different computer Platforms

C Programming

Page 11: Computer programming all chapters

C Programming Language

1. High Level Language.

2. C allows you to get control of computer Hardware.

3. Many other high level language developed based on C.

4. The American National Standard Institute ANSIdefine the standard for the C programming language.

5. Your HTI Book focuses on the functions defined in ANSI

See Page 3

Higher Technological Institute11C Programming

Page 12: Computer programming all chapters

Interpreter, Compiler and Assembler

Higher Technological Institute12

Interpreter

Compiler

Assembler

High Level

Language

Lower Level Language

Assembly Language

Binary Language

High Level

Language Lower Level Language

Computer

Instruction Binary 0’s and 1’s

Read more Pages 3 and 4

C Programming

Page 13: Computer programming all chapters

Your First C Program

/* This is my First C Program */

#include<stdio.h>

int main ()

{

printf(" Hello World! \n ") ;

return 0 ;

}Higher Technological Institute13C Programming

Page 14: Computer programming all chapters

Comments

/* This is my First C Program */

o The compiler ignore everything between two slashes and asterisks.

o Just to help you document your program.

o C compilers allows to write comments like

Higher Technological Institute14

/*This comment does not increase the size of the executable file

(Binary Code), nor does it affect the performance speed

*/

C Programming

Page 15: Computer programming all chapters

Your First C Program

/* This is my First C Program */

#include<stdio.h>

int main ()

{

printf(" Hello World! \n ") ;

return 0 ;

}Higher Technological Institute15C Programming

Page 16: Computer programming all chapters

Header Files

#include<stdio.h>o This Header Files required by include directive

o Header Files extensions .h means header

o The stdio.h header file numerous prototypes and macros to perform input or output I/O for C Program

Higher Technological Institute16

stdio.h Header File

C Programming

Page 17: Computer programming all chapters

Your First C Program

/* This is my First C Program */

#include<stdio.h>

int main ()

{

printf(" Hello World! \n ") ;

return 0 ;

}Higher Technological Institute17C Programming

Page 18: Computer programming all chapters

Main Function

int main ()

{

return 0 ;

}Higher Technological Institute18

o Very special function

o Each C Program MUST have a main() function

o The main() function syntax as follows

C Programming

Page 19: Computer programming all chapters

Your First C Program

/* This is my First C Program */

#include<stdio.h>

int main ()

{

printf(" Hello World! \n ") ;

return 0 ;

}Higher Technological Institute19C Programming

Page 20: Computer programming all chapters

Printing On Screen

printf(" Hello World! \n ") ;

Higher Technological Institute20

o To print a text or numbers printf is used.

o Syntax printf(“ ”);

o The new line character \n

o To print Hello World! write the following statement

C Programming

Page 21: Computer programming all chapters

Your First C Program

/* This is my First C Program */

#include<stdio.h>

int main ()

{

printf(" Hello World! \n ") ;

return 0 ;

}Higher Technological Institute21C Programming

Page 22: Computer programming all chapters

Higher Technological Institute22

References

C Programming

Page 23: Computer programming all chapters

References

Logic Design A Higher Technological Institute23

Page 24: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute24

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 25: Computer programming all chapters

Course Contents

Higher Technological Institute25

Program Development2

Phases of Computer Program Development

Problem Definition and Analysis

Algorithms and Flowcharts

Program Coding

Program Execute and Testing

Exercise

C Programming

Page 26: Computer programming all chapters

Problem Definition and Analysis

o The problem which you write a program for should becompletely understood by gathering informationabout it.

o This information is the program input.

o How information is processed to give the requiredoutput.

oExample

Higher Technological Institute26

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

C Programming

Page 27: Computer programming all chapters

Problem Definition and Analysis

o The General Quadratic Equation

Higher Technological Institute27

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

C Programming

Page 28: Computer programming all chapters

Problem Definition and Analysis

o The General Quadratic Equation

𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

Higher Technological Institute28

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

C Programming

Page 29: Computer programming all chapters

Problem Definition and Analysis

o The General Quadratic Equation

𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =

−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

Higher Technological Institute29

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

C Programming

Page 30: Computer programming all chapters

Problem Definition and Analysis

o The General Quadratic Equation

𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =

−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

Higher Technological Institute30

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

Discriminator

C Programming

Page 31: Computer programming all chapters

Problem Definition and Analysis

o The General Quadratic Equation

𝒙 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

𝒙𝟏 =−𝒃 + 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =

−𝒃 − 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄𝒊𝒇 𝑫 < 𝟎 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒂𝒓𝒆 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚𝒊𝒇 𝑫 > 𝟎 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒄𝒂𝒏 𝒃𝒆 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆𝒅

Higher Technological Institute31

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

Discriminator

C Programming

Page 32: Computer programming all chapters

Problem Definition and Analysis

o How information is processed to give the requiredoutput ?

𝒂 , 𝒃 𝒂𝒏𝒅 𝒄 𝒂𝒓𝒆 𝑮𝒊𝒗𝒆𝒏 = 𝑰𝒏𝒑𝒖𝒕𝒔

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄 Question D > 0 ?

YES 𝒙𝟏 =−𝒃+ 𝒃𝟐−𝟒𝒂𝒄

𝟐𝒂𝒂𝒏𝒅 𝒙𝟐 =

−𝒃− 𝒃𝟐−𝟒𝒂𝒄

𝟐𝒂

NO 𝒙𝟏 𝒂𝒏𝒅 𝒙𝟐 𝒂𝒓𝒆 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚

Higher Technological Institute32

𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎

C Programming

Page 33: Computer programming all chapters

Algorithms and Flowcharts

Higher Technological Institute33C Programming

Page 34: Computer programming all chapters

Flowchart

Higher Technological Institute34

Preparation

Start, Stop ( Begin , End)

Input , Output

Processing Program Instruction

Decision

Connector

Comment( Instruction that change the program)

C Programming

Page 35: Computer programming all chapters

Flowcharts

Higher Technological Institute35

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

C Programming

Page 36: Computer programming all chapters

Flowcharts

Higher Technological Institute36

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

C Programming

Page 37: Computer programming all chapters

Flowcharts

Higher Technological Institute37

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

C Programming

Page 38: Computer programming all chapters

Flowcharts

Higher Technological Institute38

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

YES NO

C Programming

Page 39: Computer programming all chapters

Flowcharts

Higher Technological Institute39

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

YES NO

C Programming

Page 40: Computer programming all chapters

Flowcharts

Higher Technological Institute40

𝑫 = 𝒃𝟐 − 𝟒𝒂𝒄

𝒙𝟏,𝟐 =−𝒃 ± 𝒃𝟐 − 𝟒𝒂𝒄

𝟐𝒂

YES NO

C Programming

Page 41: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute41

Example 1

Draw a flow chart to

read two numbers and

compare between both

if they are equal or not.

C Programming

Page 42: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute42

Example 1

Draw a flow chart to

read two numbers and

compare between both

if they are equal or not.

C Programming

Page 43: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute43

Example 2

Draw a flow chart to generate

a table of squares and cubes

of integers from 1 to 9.

C Programming

Page 44: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute44

Example 2

Draw a flow chart to generate

a table of squares and cubes

of integers from 1 to 9.

C Programming

Page 45: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute45

Example 3

Draw a flow chart to

determine the minimum

value of three input

numbers.

C Programming

Page 46: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute46

Example 3

Draw a flow chart to

determine the minimum

value of three input

numbers.

C Programming

Page 47: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute47

Example 4

Draw a flow chart to generates the famous sequence

of numbers called the Fibonacci series.

Here are the first few terms of the series:

0 1 1 2 3 5 8 13 21 34 55

C Programming

Page 48: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute48

Example 4

Draw a flow chart to generates the famous sequence

of numbers called the Fibonacci series.

Here are the first few terms of the series:

0 1 1 2 3 5 8 13 21 34 55

Each term is found by adding the two previous

ones:

1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.

C Programming

Page 49: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute49

Example 4

Draw a flow chart to generates the famous sequence

of numbers called the Fibonacci series.

Here are the first few terms of the series:

0 1 1 2 3 5 8 13 21 34 55

Each term is found by adding the two previous

ones:

1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.

𝒇𝟎 = 𝟎 , 𝒇𝟏 = 𝟏 𝒂𝒏𝒅 𝒇𝒏 = 𝒇𝒏−𝟏 + 𝒇𝒏−𝟐

Terminate the program after calculation the first 10

numbers of the Fibonacci series and print them.

C Programming

Page 50: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute50

Example 4

𝒇𝟎 = 𝟎𝒇𝟏 = 𝟏𝒇𝒏 = 𝒇𝒏−𝟏 + 𝒇𝒏−𝟐

0

1

1

2

3

5

8

13

21

34

55

C Programming

Page 51: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute51

Example 5

It is desired to compute income Tax for a given

income as per the following table:

Income Amount of tax

Up to Rs 35000 Nil

From Rs 35001

To Rs 60000

20% of income

in Excess of Rs 35000

From Rs 60001

To Rs 120000

Rs 5000 + 30% of income in Excess of Rs 60000

More than Rs 120000Rs 23000+ 40% of income in

Excess of Rs 120000

C Programming

Page 52: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute52C Programming

Page 53: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute53

Example 6

It is desired to sum 20 values read from a data

statement. This can be done by means of a loop. It

uses the following algorithm for summation.

Sj+1 = Sj + A

Where: j = 1,2,3,..20Sj+1 : The new sum

Sj : The old sum

A : The variable whose values are to be added

C Programming

Page 54: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute54C Programming

Page 55: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute55

Example 7

Draw a flowchart of Program

that Calculate and Display

The Factorial of n

C Programming

Page 56: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute56

Example 7

Draw a flowchart of Program

that Calculate and Display

The Factorial of n𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏

C Programming

Page 57: Computer programming all chapters

Flowcharts Examples

Higher Technological Institute57

Example 7

Draw a flowchart of Program

that Calculate and Display

The Factorial of n

𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏

C Programming

Page 58: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute58

Example 8

Draw a flowchart to compute all possible products of

X and Y

X and Y varying from 0 through 9

(100 products)

Home Work ..!

0x0=0 1x0=0 … 9x0=0

0x1=0 1x1=1 … 9x1=9

0x2=0 1x2=2 … 9x2=18

0x3=0 1x3=3 … 9x3=27

. .

. .

0x9=0 1x9=9 … 9x9=81

Page 59: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute59

Example 8

0x0=0 1x0=0 … 9x0=0

0x1=0 1x1=1 … 9x1=9

0x2=0 1x2=2 … 9x2=18

0x3=0 1x3=3 … 9x3=27

. .

. .

0x9=0 1x9=9 … 9x9=81

Page 60: Computer programming all chapters

Algorithms

C Programming Higher Technological Institute60

Page 61: Computer programming all chapters

Program Coding

o In this phase, the algorithm ( or Flowchart ) is transferred into program using codes of one of programming language.

o Machine Language ( Binary Code ).

o Assembly Language.

o High Level Language.

C Programming Higher Technological Institute61

Page 62: Computer programming all chapters

Program Executing and Testing

C Programming Higher Technological Institute62

Editing

Compiling

Linking

Executing

Page 63: Computer programming all chapters

Program Executing and Testing

Means to use any editingprogram to convert the program intoelectronic form and save it with asuitable and valid name with theextension (.c)

The program becomes an electronic source code

C Programming Higher Technological Institute63

Editing

Page 64: Computer programming all chapters

Program Executing and Testing

Means to use a compilerprogram matches with theprogramming language the sourceprogram written with, to detect thelinguistic errors and convert thesource code program into a binarycodes.

The program becomes an objectprogram, saved with the source codename but with the extension (.0).

C Programming Higher Technological Institute64

Compiling

Page 65: Computer programming all chapters

Program Executing and Testing

The program when it is writtencontains some functions, as printf( )function which is saved in the library ofthe compiler and may be someexternal functions written to do certainjobs by the program after linking withlibraries functions is converted to anexecutable program, with the samename entered by the user but with theextension .exe (dot exe).

C Programming Higher Technological Institute65

Linking

Page 66: Computer programming all chapters

Program Executing and Testing

The program in this step i.e.,after linking is ready to be run ordebugged on the computer.

C Programming Higher Technological Institute66

Executing

Page 67: Computer programming all chapters

Program Executing and Testing

C Programming Higher Technological Institute67

Editing

Compiling

Linking

Executing

Page 68: Computer programming all chapters

Steps of Obtaining an Executable Program

C Programming Higher Technological Institute68

Page 69: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute69

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 70: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute70

The Essential of C Programs3

Constants and variables

Expressions

Arithmetic operators

Statements

Statement blocks

Data Types and Names in C

Naming a Variable

Page 71: Computer programming all chapters

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 Institute71

𝑨 = 𝟐 ∗ 𝑨 − 𝟏

Page 72: Computer programming all chapters

Arithmetic Operations

C Programming Higher Technological Institute72

Addition

DivisionMultiplication

Subtraction

Reminder

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

Page 73: Computer programming all chapters

Arithmetic Operations

C Programming Higher Technological Institute73

Addition

DivisionMultiplication

Subtraction

Reminder

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

Example

Page 74: Computer programming all chapters

Arithmetic Operations

C Programming Higher Technological Institute74

Addition

DivisionMultiplication

Subtraction

Reminder

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

Example

Page 75: Computer programming all chapters

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 Institute75

Page 76: Computer programming all chapters

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 Institute76

Page 77: Computer programming all chapters

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 Institute77

Page 78: Computer programming all chapters

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 Institute78

Valid Variable Name Can Use

Page 79: Computer programming all chapters

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 Institute79

Invalid Variable Name Can NOT be Used

Page 80: Computer programming all chapters

Data Types

C Programming Higher Technological Institute80

C Data Type

char

a, B, $, #

int

5, 17, 128

float

2.5 , 0.3

double

23433.3455

Page 81: Computer programming all chapters

Declaration Statement

C Programming Higher Technological Institute81

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 = ‘&’ ;

int n1 = 100 ;

Page 82: Computer programming all chapters

Declaration Statement

C Programming Higher Technological Institute82

Typechar

int

float

double

Namec1

N1

F1

d1

Value‘&’

100

32/10

5e3

char c1 ;

int n1 ;

c1 = ‘&’ ;

n1 = 100 ;

Page 83: Computer programming all chapters

Declaration Statement

C Programming Higher Technological Institute83

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1= 32/100 ;

double d1=5e3 ;

Page 84: Computer programming all chapters

Declaration Statement

C Programming Higher Technological Institute84

Typechar

int

float

double

Namec1

n1

f1

d1

Value‘&’

100

32/10

5e3

float f1 ;

double d1 ;

f1 = 32/100 ;

d1 = 5e3 ;

Page 85: Computer programming all chapters

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 Institute85

Page 86: Computer programming all chapters

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 Institute86

Page 87: Computer programming all chapters

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 Institute87

Page 88: Computer programming all chapters

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 Institute88

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

Page 89: Computer programming all chapters

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 Institute89

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

Page 90: Computer programming all chapters

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 Institute90

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

Page 91: Computer programming all chapters

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 Institute91

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

New Line

Specifier

for

Character Data Type

Page 92: Computer programming all chapters

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 Institute92

Page 93: Computer programming all chapters

You Have To Know about Data Types

C Programming Higher Technological Institute93

Type

char

int

float

double

Name

c1

n1

f1

d1

Value

‘&’

100

32/10

5e3

Specifier

%c

%d

%f

%e,%E

Page 94: Computer programming all chapters

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 Institute94

Page 95: Computer programming all chapters

Operators Precedence

Operator Sign Name

( ) Brackets

/ Division

* Multiplication

+ Addition

- Subtraction

C Programming Higher Technological Institute95

Page 96: Computer programming all chapters

Logic Operators

C Programming Higher Technological Institute96

Operator Sign Name

| | OR

&& AND

!= NOT

Page 97: Computer programming all chapters

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 Institute97

Page 98: Computer programming all chapters

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 Institute98

Page 99: Computer programming all chapters

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 Institute99

Specifier %e or %E

With printf ( )

Page 100: Computer programming all chapters

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 Institute100

Page 101: Computer programming all chapters

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 Institute101

Page 102: Computer programming all chapters

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 Institute102

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

Page 103: Computer programming all chapters

Precedence Example

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

C Programming Higher Technological Institute103

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

Page 104: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute104

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

Page 105: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute105

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

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 106: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute106

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

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

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 107: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute107

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 108: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute108

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 109: Computer programming all chapters

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 Institute109

Page 110: Computer programming all chapters

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 Institute110

Page 111: Computer programming all chapters

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 Institute111

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

Page 112: Computer programming all chapters

Precedence Example

.

.

.

C Programming Higher Technological Institute112

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

Page 113: Computer programming all chapters

Precedence Example

.

.

.

38

C Programming Higher Technological Institute113

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

Page 114: Computer programming all chapters

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 Institute114

Page 115: Computer programming all chapters

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 Institute115

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

Page 116: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute116

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

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 117: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute117

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

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 118: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute118

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

20 * 4 / 5A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 119: Computer programming all chapters

Precedence Example

C Programming Higher Technological Institute119

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

20 * 4 / 5

16

A = 20

B = 6

C = 3

D = 10

E = 2

( )

*

/

+ -

Page 120: Computer programming all chapters

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 Institute120

Page 121: Computer programming all chapters

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 Institute121

Page 122: Computer programming all chapters

Home Work ..!!Exercises 3 Page 44

C Programming Higher Technological Institute122

Page 123: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute123

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 124: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute124

Manipulating Data with Operators 4

Page 125: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute125

Errors Types

Arithmetic Assignment Operators

Unary Minus Operator

The Cast Operator

Manipulating Data with Operators 4

Page 126: Computer programming all chapters

Error Types

C Programming Higher Technological Institute126

Syntax

Execution

Logic

Page 127: Computer programming all chapters

Error Types

They are errors that youhave made in the form

Or syntax of the language

o Spelling a keyword incorrectly

o Errors that are detectedduring running of a programare shown in a dialog box thatappears and the error ishighlighted in the program.

C Programming Higher Technological Institute127

Syntax

Example

Page 128: Computer programming all chapters

Error Types

If the program has no syntax errors. The computer can execute it.

During execution, execution errors may be detected.

o If a number is divided by zerocauses an execution error.

Whenever an execution erroris detected, the computer displays adialog box with an error messageand stops executing the program.

C Programming Higher Technological Institute128

Execution

Example

Page 129: Computer programming all chapters

Error Types

If the output of the programdoes not agree with what isexpected, this is logic error.

The computer cannot detectsuch an error because it does notknow the logic of the programshould be.

So, it is your responsibility todetect logic errors in the program.

C Programming Higher Technological Institute129

Logic

Page 130: Computer programming all chapters

Error Types

C Programming Higher Technological Institute130

Syntax

Execution

Logic

Page 131: Computer programming all chapters

Arithmetic Assignment Operators

o Here the statement causes the value of the right-hand-operand to be assigned to the memory location of the left-hand-operand.

o Statement writes the value of the right-hand-operand (5) into the memory location of the integer variable a (which is the left-hand-operand in this case).

C Programming Higher Technological Institute131

left-hand-operand = right-hand-operand

a=5 ;

b=a=5 ;

Page 132: Computer programming all chapters

Assignment and Arithmetic Operators

Operator Description

+ = Addition Assignment Operator

- = Subtraction Assignment Operator

* = Multiplication Assignment Operator

/ = Division Assignment Operator

% = Remainder Assignment Operator

C Programming Higher Technological Institute132

Page 133: Computer programming all chapters

Equivalence of Statements

Statement Equivalence

x+ = y; x = x + y;

x- = y; x = x - y;

x* = y; x = x * y;

x/ = y; x = x / y;

x% = y; x = x % y;

C Programming Higher Technological Institute133

z = z * x + y ; z *= x + y ; ?

Page 134: Computer programming all chapters

Equivalence of Statements

Statement Equivalence

x+ = y; x = x + y;

x- = y; x = x - y;

x* = y; x = x * y;

x/ = y; x = x / y;

x% = y; x = x % y;

C Programming Higher Technological Institute134

z = z * x + y ; z *= x + y ; ≠

Page 135: Computer programming all chapters

Example# include <stdio.h> main ( ){ int x, y, z; x =1; y =3; z= 10;printf (“Given x = %d, y = %d, and z = %d,\n”, x, y, z);x= x + y;printf ( “ x= x +y assigns %d to x;\n”, x);x = 1; x+= y ; printf ( “ x+= y assigns %d to x;\n”, x);x = 1; z = z* x + y;printf ( “z = z*x+y assigns %d to z;\n”, z); z = 10; z = z* (x + y);printf ( “z = z*( x+ y) assigns %d to z;\n”, z);z = 10; z *= x + y;printf ( “z *= x+ y assigns %d to z;\n”, z);return 0; }

C Programming Higher Technological Institute135

Page 136: Computer programming all chapters

Unary Minus Operator

o Given an integer, you can get its negation by changing the sign of the integer by using, -, the minus operator, which is called the unary minus operator.

o Differ between the unary minus operator and the subtraction operator.

o The first – symbol is used as the subtraction operator while the second –symbol is the unary minus operator.

C Programming Higher Technological Institute136

x = 1.234 ; -x equals -1.234

z = x- -y; OR z = x- (-y);

Page 137: Computer programming all chapters

Incrementing or Decrementing by ONE

C Programming Higher Technological Institute137

X++ ++X

X-- --X

IncrementP

ost-

Decrement

Pre

-

Page 138: Computer programming all chapters

Example# include <stdio.h> main ( ){int x=5; printf (“ X++ %d ,\n”, x++);printf (“++X %d ,\n”, ++x);printf (“ X-- %d ,\n”, x--);printf (“ --X %d ,\n”, --x);return 0; }

C Programming Higher Technological Institute138

Page 139: Computer programming all chapters

Relational Operators

Operator Description

== Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

C Programming Higher Technological Institute139

Page 140: Computer programming all chapters

oAll relational expressions produce a result of either 0 or 1.

oGiven x = 3 and y = 5, for instance, the relational expression x < y gives a result of 1

C Programming Higher Technological Institute140

x * y < z + 3 (x * y) < ( z + 3 )

Relational Operators

Page 141: Computer programming all chapters

Example# include <stdio.h> main ( ){ int x, y;

double z;

x = 7; y = 25; z = 24.46;

printf (“ Given x = %d, y = %d and z = %2f, \n”, x, y, z);printf ( “ x > = y produces: %d \n”, x > =y);printf ( “x = = y produces: %d \n”, x = =y);printf ( “x < y produces: %d \n”, x < y); printf ( “x > y produces: %d \n”, x > y);printf ( “x != y - 18 produces: %d \n”, x ! =y -18); printf ( “x + y ! = z produces: %d \n”, x +y ! = z); return 0; }

C Programming Higher Technological Institute141

Page 142: Computer programming all chapters

This is algebraically True and is supposed to return 1.

o However, the expression returns 0 which means that the equal to relationship does not hold.

o This is because the truncation of the integer division – that is , 1 /2 – produces 0 not 0.5.

C Programming Higher Technological Institute142

1 / 2 + 1 / 2 = = 1 1 Or 0

Relational Operators

Page 143: Computer programming all chapters

The Cast Operator

o You can convert one data type to a different oneby prefixing and cast operator to the operand.

o The general form of the cast operator is

o Here data-type specifies the data type you wantto convert to x is a variable ( or, expression)that contains the value of the current data type.

o You have to include the parentheses (and) tomake up a cast operator.

C Programming Higher Technological Institute143

(data-type) x

Page 144: Computer programming all chapters

Example# include <stdio.h> main ( ){int x, y;

x = 7;

y = 5;

printf ( “ Given x = %d, y = %d, \n”, x, y);

printf ( “ x / y produces: %d \n”, x /y);

printf ( “(float) x / y produces: %f \n”, (float) x / y);

return 0 ;

}

C Programming Higher Technological Institute144

Page 145: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute145

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 146: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute146

Reading from and Writing to Standard I/O5

Page 147: Computer programming all chapters

scanf ( ) functiono The scanf ( ) function is a very important function in C.

o It is used to read data from the standard input device; the keyboard.

o The syntax for the scanf ( ) function is :

o Example : to input two variables integer x and float y

C Programming Higher Technological Institute147

scanf(“ % % ”, & & );

scanf(“ %d %f ”, &x &y );

Page 148: Computer programming all chapters

scanf ( ) functiono The scanf ( ) function is a very important function in C.

o It is used to read data from the standard input device; the keyboard.

o One thing you need to be aware of; is that the scanf ( ) function doesn’t actually start reading the input untilthe Enter key is pressed.

o Data entered from the keyboard is placed in an inputbuffer.

o When the Enter key is pressed, the scanf () function looks for its input in the buffer.

C Programming Higher Technological Institute148

Page 149: Computer programming all chapters

printf( ) functiono The printf ( ) function is a very important function in C.

o It is used to print out messages on the screen.

o The syntax for the printf ( ) function is

o Example : to print two variables integer x and float y

C Programming Higher Technological Institute149

printf(“ % % ”, );

printf(“ %d %f ”, x y );

Page 150: Computer programming all chapters

printf( ) functiono The printf ( ) function is a very important function in C.

o It is used to print out messages on the screen.

o The number of expressions is determined by thenumber of the format specifiers inside the firstargument.

o The format specifiers and the expressions arematched in order from left to right, and you shoulduse exactly the same number of expressions as thenumber of format specifiers within the format string.

C Programming Higher Technological Institute150

Page 151: Computer programming all chapters

Adding the Minimum Field Width

o A integer is added between the percent sign (%) and the letter in a format specifier to specify the minimum field width and ensures that the output reaches the minimum width.

o For example in %10f

10 is a minimum field width specifier thatensures that the output is at least 10 characterspacers wide.

C Programming Higher Technological Institute151

printf(“ %10f ”, x );

Page 152: Computer programming all chapters

Example

C Programming Higher Technological Institute152

# include <stdio.h>

main ( )

{

int x=12; int y=12345;

printf (“%d \n”, x);

printf (“%d \n”, y);

printf (“%5d \n”, x);

printf (“%05d \n”, x);

printf (“%2d \n”, y);

return 0;

}

Page 153: Computer programming all chapters

Example

C Programming Higher Technological Institute153

# include <stdio.h>

main ( )

{

int x=12; int y=12345;

printf (“%d \n”, x);

printf (“%d \n”, y);

printf (“%5d \n”, x);

printf (“%05d \n”, x);

printf (“%2d \n”, y);

return 0;

}

12

12345

12

00012

12345

Page 154: Computer programming all chapters

Aligning Output

o By default, all output is placed on the Right edge ofthe field, as long as the field width is longer than thewidth of the output.

o You can change this and force output to be left-justified.

o To do so, you need to prefix the minimum fieldspecifier with the minus sign (-).

o Specifies the minimum field width as 12, and justifiesthe output from the left edge of the field.

C Programming Higher Technological Institute154

%-12d

Page 155: Computer programming all chapters

Example

C Programming Higher Technological Institute155

# include <stdio.h>

main ( )

{

int x,y,z,m,n; x=1; y=12; z=123;

m=1234; n=12345;

printf (“%8d %-8d \n”, x, x );

printf (“%8d %-8d \n”, y, y );

printf (“%8d %-8d \n”, z, z );

printf (“%8d %-8d \n”, m, m );

printf (“%8d %-8d \n”, n, n );

return 0;

}

Page 156: Computer programming all chapters

Example

C Programming Higher Technological Institute156

# include <stdio.h>

main ( )

{

int x,y,z,m,n; x=1; y=12; z=123;

m=1234; n=12345;

printf (“%8d %-8d \n”, x, x );

printf (“%8d %-8d \n”, y, y );

printf (“%8d %-8d \n”, z, z );

printf (“%8d %-8d \n”, m, m );

printf (“%8d %-8d \n”, n, n );

return 0;

}1 1

12 12

123 123

1234 1234

12345 12345

Page 157: Computer programming all chapters

The Precision Specifier

o You can put a period (.) and an integer right after theminimum field width specifier.

o The combination of the period and the integer makeup a precision specifier.

o The precision specifier is another important specifieryou can use to determine the number of decimalplaces for floating-point numbers, or to specify themaximum field width for integers or strings.

o The minimum field width length is specified as 10character long, and the number of decimal places isset to 3.

C Programming Higher Technological Institute157

%10.3f

Page 158: Computer programming all chapters

The Precision Specifier

o You can put a period (.) and an integer right after theminimum field width specifier.

o The combination of the period and the integer makeup a precision specifier.

o The precision specifier is another important specifieryou can use to determine the number of decimalplaces for floating-point numbers, or to specify themaximum field width for integers or strings.

o Remember, the default number of decimal places is 6.

C Programming Higher Technological Institute158

Page 159: Computer programming all chapters

Example

C Programming Higher Technological Institute159

# include <stdio.h>

main ( )

{

int x=123; float y=123.456789;

printf (“Default integer format \n %d \n”, x );

printf (“With precision specifier \n %2.8d \n”, x );

printf (“Default float format \n %f \n”, y );

printf (“With precision specifier \n %10.2f \n”, y );

printf (“With precision specifier \n %-10.2f \n”, y );

return 0;

}

Page 160: Computer programming all chapters

Example

C Programming Higher Technological Institute160

# include <stdio.h>

main ( )

{

int x=123; float y=123.456789;

printf (“Default integer format \n %d \n”, x );

printf (“With precision specifier \n %2.8d \n”, x );

printf (“Default float format \n %f \n”, y );

printf (“With precision specifier \n %10.2f \n”, y );

printf (“With precision specifier \n %-10.2f \n”, y );

return 0;

}

Page 161: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute161

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 162: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute162

Decision6

Page 163: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute163

The if statement

Flowchart of if statement

The if else statement and flowchart

Nested if statements and their flowcharts

The switch statement

The break and continue statement

The goto statements

Decision6

Page 164: Computer programming all chapters

The if statemento The if statement is the most popular conditional

branching statement.

o It can be used to evaluate the conditions as well as tomake the decision whether the block of codecontrolled by the statement is going to be executed.

o Here if condition is logical TRUE, the statements insidethe braces are executed.

o If condition is logical FALSE, then the statements areskipped.

C Programming Higher Technological Institute164

Page 165: Computer programming all chapters

The if statement Flowchart

C Programming Higher Technological Institute165

…..if ( Test Expression) {statement1;statement2;…}….

IF Statement Syntax

Page 166: Computer programming all chapters

The if statement Flowchart

C Programming Higher Technological Institute166

…if ( Test Expression) statement;…

IF Statement Syntax

Example

if ( x>5) printf( “ X > 5 ” );…

Page 167: Computer programming all chapters

Example

C Programming Higher Technological Institute167

# include <stdio.h>

main ( )

{

int i;

printf (“Integers that can be divided by 3 \n ” );

printf (“Enter a positive number : \n ”, x );

scanf(“%d”, &i );

if ( i %3 == 0 )

printf (“The number %d is divisible by 3 \n ”, i );

return 0;

}

Page 168: Computer programming all chapters

Output

C Programming Higher Technological Institute168

Integers that can be divided by 3Enter a positive number : 12The number 12 is divisible by 3

Integers that can be divided by 3Enter a positive number : 7

Page 169: Computer programming all chapters

The if else statement

C Programming Higher Technological Institute169

…if ( Test Expression) {statement1;statement2;…}else {statement_Astatement_B…}…

if else Statement Syntax

Page 170: Computer programming all chapters

The if else statement

C Programming Higher Technological Institute170

…if ( Test Expression) statement1;elsestatementA;…

if else Statement Syntax

if ( X > 5) printf( “ X > 5 ” );elseprintf( “ X < 5 ” );

Example

Page 171: Computer programming all chapters

if else Example

C Programming Higher Technological Institute171

Draw a flow chart to

read two numbers and

compare between both

if they are equal or not.

Page 172: Computer programming all chapters

C Programming Higher Technological Institute172

# include <stdio.h>

main ( )

{

int x,y;

printf (“Please Enter X and Y \n ” );

scanf(“%d %d”, &x , &y );

if ( x == y )

printf (“ X = Y \n ” );

else

printf (“ X != Y \n ” );

return 0; }

if else Example

Page 173: Computer programming all chapters

Nested if Flowchart

C Programming Higher Technological Institute173

if ( Test Expression1) { if ( Test Expression2)

{ statement1;statement2;

} else { statementA;

statementB;}

}else {statement X ;}

Nest IF Statement Syntax

Page 174: Computer programming all chapters

Nested if Flowchart

C Programming Higher Technological Institute174

Draw a flow chart to

determine the minimum

value of three input

numbers.

Page 175: Computer programming all chapters

C Programming Higher Technological Institute175

# include <stdio.h>

main ( )

{

int A,B,C;

printf (“Please Enter A,B and C \n ” );

scanf(“%d %d %d”, &A , &B , &C );

if ( A<B )

{ if ( A<C )

printf (“ A The is the Smallest \n ” );

else

printf (“ C The is the Smallest \n ” );

} else if ( B<C )

printf (“ B The is the Smallest \n ” );

else

printf (“ C The is the Smallest \n ” );

return 0; }

Nested if Example

Page 176: Computer programming all chapters

The switch statement

C Programming Higher Technological Institute176

o The nested if statements will become very complex ifthere are many decisions that need to be made.

o The switch statement, can be used to make unlimiteddecisions or choices based on the value of a conditionalexpression and specified cases.

o The conditional expression is evaluated first, if thereturn value of the conditional expression is equal tothe constant expression expression 1, the statementstatement 1 is executed.

o the value of the conditional expression is not equal toany values of the constant expressions labeled by thecase keyword, the statement (statement-default)following by the default keyword is executed.

Page 177: Computer programming all chapters

The Switch Statement Syntax

C Programming Higher Technological Institute177

switch ( Conditional Expression ) {

case expression1;statment1;case expression2;statment2;…default;statement-default;

}

Page 178: Computer programming all chapters

The Switch Statement Syntax

C Programming Higher Technological Institute178

switch ( Conditional Expression ) {case expression1;statment1;case expression2;statment2;…default;statement-default; }

o You have to use the case keyword to label each case.

o The default keyword is recommended to be used forthe default case.

o Note that no constant expressions are identical in theswitch statement.

Page 179: Computer programming all chapters

The break statemento An important feature of the switch statement is that

the computer continues to execute the statementsfollowing the selected case until the end of the switchstatement.

o You can add a break statement at the end of thestatement list following every case label, if you wantto exit the switch construct after the statementswithin a selected case are executed

C Programming Higher Technological Institute179

Page 180: Computer programming all chapters

The Break Statement Syntax

C Programming Higher Technological Institute180

switch ( Conditional Expression ) {

case expression1;statment1;break ;case expression2;statment2;break;…default;statement-default;

}

Page 181: Computer programming all chapters

Example

Total Score Letter Grade

85 <= T <= 100 A

80 <= T < 85 B+

75 <= T < 80 B

70 <= T < 75 C+

65 <= T < 70 C

55 <= T < 65 D+

50 <= T < 55 D

T < 50 F

C Programming Higher Technological Institute181

Write Program that compute the grade for a student, the

grade is based on Total Score T

( from 0 to 100) The Grading Scale Is Illustrated In The

Following Table:

Page 182: Computer programming all chapters

# include <stdio.h>

main ( )

{ int score;

printf (" Enter Scorre: \n");

scanf ("%d", &score);

switch (score / 10 ) {

case 10:

printf ("your grade is A \n");

break;

case 9:

printf ("your grade is A \n");

break;

case 8:

printf ("your grade is B \n");

break;

case 7:

printf ("your grade is C \n");

break;

C Programming Higher Technological Institute182

case 6:

printf ("your grade is D \n");

break;

case 5:

printf ("your grade is D \n");

break;

case 4:

printf ("your grade is F \n");

break;

case 3:

printf ("your grade is F \n");

break;

case 2:

printf ("your grade is F \n");

break;

case 1:

printf ("your grade is F \n");

break;

case 0:

printf ("your grade is F \n");

break;

default:

printf ( "illegal input \n");

break;

}

printf ( "GOODBYE\n" );

return 0;

}

Example

Page 183: Computer programming all chapters

The Continue statemento There are times when you want to stay in a loop

but skip over some statements within the loop.o To do this, the continue statement causes

execution to jump to the top of the loopimmediately.

o The statement can be used within a while,do…while or for statement to terminate thecurrent iteration of the loop and begin thenext.

C Programming Higher Technological Institute183

Page 184: Computer programming all chapters

The goto statemento Do not use the goto statement unless it’s absolutely

necessary because its usage may make the C programunreliable and hard to debug.

o The Syntax of goto Statement:

C Programming Higher Technological Institute184

…lablename :

statment1;statment2;..

goto lablename ;…

Page 185: Computer programming all chapters

C Programming Higher Technological Institute185

# include <stdio.h>

main ( )

{

int x;

guess_lbl:

printf (“Guess integer number in the range 1 to 10 \n”);

scanf (“%d”, &x );

if ( x == 7)

goto rght_lbl;

printf (“NO, wrong guess, try again \n”);

goto guess_lbl;

rght_lbl:

printf (“WELL DONE your guess = %d is right\n” , x);

return 0;

}

goto Example

Page 186: Computer programming all chapters

C Programming Higher Technological Institute186

# include <stdio.h>

main ( )

{

int x;

guess_lbl:

printf (“Guess integer number in the range 1 to 10 \n”);

scanf (“%d”, &x );

if ( x == 7) goto rght_lbl;

printf (“NO, wrong guess, try again \n”);

goto guess_lbl;

rght_lbl:

printf (“WELL DONE your guess = %d is right\n” , x);

return 0;

}

goto Example

Page 187: Computer programming all chapters

Course Contents

Introduction

C Programming Higher Technological Institute187

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 188: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute188

Iteration 7

Page 189: Computer programming all chapters

Course Contents

C Programming Higher Technological Institute189

The for statement

The Null Statement

The while statement

The infinite while Loop

The do-while Statement

The Nested Loop

Examples

Iteration7

Page 190: Computer programming all chapters

Iterationo Iteration (looping) is used in programming

to perform the same set of statementsover and over until certain specifiedconditions are met.

C Programming Higher Technological Institute190

Repeat the same statement(s) until certain condition

Page 191: Computer programming all chapters

Iteration

C Programming Higher Technological Institute191

Example

Draw a flow chart to generate

a table of squares and cubes

of integers from 1 to 9.

Page 192: Computer programming all chapters

Iteration

C Programming Higher Technological Institute192

Example

Draw a flow chart to generate

a table of squares and cubes

of integers from 1 to 9.

From

1 to 9

Step 1

Page 193: Computer programming all chapters

The for statement

C Programming Higher Technological Institute193

for ( initialization ; Test Expression ; Step) statement;…

for Statement Syntax One Statement

for ( initialization ; Test Expression ; Step) {statement1;statement2;…}

for Statement Syntax Multiple Statements

Page 194: Computer programming all chapters

The for statement

C Programming Higher Technological Institute194

for ( i=1 ; i <= 10 ; i++ )

printf (“ Hello \n ”);

printf (“ Goodbye \n ”);

Will Print Hello 10 Times Then PrintGoodbye

for ( i=1 ; i <= 10 ; i++ )

{

printf (“ Hello \n ”);

printf (“ Goodbye \n ”);

}

Will Print Hello Goodbye10 Times

Page 195: Computer programming all chapters

Example

C Programming Higher Technological Institute195

# include <stdio.h>

main ( )

{

int i=1; int j,k;

for ( i=1 ; i <= 9 ; i++ )

{

j=i*i; k=j*i;

printf (“ i = %d ”, i ) ;

printf (“ j = %d ”, j ) ;

printf (“ k = %d \n”, k ) ;

}

return 0;

}

Page 196: Computer programming all chapters

Null statement

C Programming Higher Technological Institute196

for ( i=1 ; i <= 10 ; i++ ) ;

printf (“ Hello \n ”);

printf (“ Goodbye \n ”);

Page 197: Computer programming all chapters

The while statement

C Programming Higher Technological Institute197

Initializationwhile ( Test Expression ){…Increment Or Decrement ;}

while Statement Syntax

Page 198: Computer programming all chapters

The while statement

C Programming Higher Technological Institute198

int w = 1;

while ( w <= 10 )

{

printf (“ Hello \n ”);

w = w++;

}

printf (“ Goodbye \n ”);

Will Print Hello 10 Times Then PrintGoodbye

Page 199: Computer programming all chapters

While Loop Example

C Programming Higher Technological Institute199

# include <stdio.h>

main ( )

{

int i=1; int j,k;

while (i<=10)

{

j=i*i; k=j*i;

printf (“ i = %d ”, i ) ;

printf (“ j = %d ”, j ) ;

printf (“ k = %d \n”, k ) ;

i=i+1;

}

return 0; }

Page 200: Computer programming all chapters

The Infinite while loop

C Programming Higher Technological Institute200

while(1)

{

Statement1;

Statement2;

}

Always returns 1, the statements inside thestatement block will be executed over andover- that is, the while loop will continueforever.

You can set certain conditions inside the while loopto break the infinite loop as soon as theconditions are met.

Page 201: Computer programming all chapters

The do-while statement

C Programming Higher Technological Institute201

Initializationdo{…Increment Or Decrement ;

} while ( Test Expression ) ;

do-while Statement Syntax

Page 202: Computer programming all chapters

The do-while statement

C Programming Higher Technological Institute202

Initializationdo{…Increment Or Decrement ;

} while ( Test Expression ) ;

do-while Statement Syntax

Only after do-while loop

Don’t forget to write ;

Page 203: Computer programming all chapters

The do-while statement

C Programming Higher Technological Institute203

int w = 1;

do

{

printf (“ Hello \n ”);

w = w++;

} while ( w <= 10 ) ;printf (“ Goodbye \n ”);

Will Print Hello 10 Times Then PrintGoodbye

Page 204: Computer programming all chapters

do-while Loop Example

C Programming Higher Technological Institute204

# include <stdio.h>

main ( )

{

int i=1; int j,k;

do

{

j=i*i; k=j*i;

printf (“ i = %d ”, i ) ;

printf (“ j = %d ”, j ) ;

printf (“ k = %d \n”, k ) ;

i=i+1;

} while (i<=10);

return 0; }

Page 205: Computer programming all chapters

Examples

C Programming Higher Technological Institute205

Example 1

Write a program to calculate the

summation and Average of 20 values

read from a data statement.

Page 206: Computer programming all chapters

Examples

C Programming Higher Technological Institute206

# include <stdio.h>

main ( )

{

int i,x,sum; float av;

sum=0;

for ( i=1 ; i<=20 ; i++ )

{

printf(“Please enter new no \n”);

scanf(“%d”,&x);

sum=sum+x;

}

av=sum/20;

printf(“Sum=%d \n Av=%f\n”, sum , av);

return 0; }

Page 207: Computer programming all chapters

Examples

C Programming Higher Technological Institute207

Example 2

Write a program to calculate the

summation and Average of N numbers

read from a data statement.

Page 208: Computer programming all chapters

Examples

C Programming Higher Technological Institute208

# include <stdio.h>

main ( )

{

int i,n,x,sum; float av;

sum=0;

printf(“How many Numbers ? \n”);

scanf(“%d”,&n);

for ( i=1 ; i<=n ; i++ )

{

printf(“Please enter new no \n”);

scanf(“%d”,&x);

sum=sum+x;

}

av=sum/n;

printf(“Sum=%d \n Av=%f\n”, sum , av);

return 0; }

Page 209: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute209

Example 3 Nested Loop

Write a program and Draw its flowchart to compute all

possible products of X and Y

X and Y varying from 1 through 9

(100 products)

1x0=0 2x0=0 … 9x0=0

1x1=0 2x1=1 … 9x1=9

1x2=0 2x2=2 … 9x2=18

1x3=0 2x3=3 … 9x3=27

. .

. .

1x9=0 2x9=9 … 9x9=81

Page 210: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute210

Example 3 Nested Loop

# include <stdio.h>

int main ( )

{

int x,y;

for ( x=1 ; x <= 9 ; x++)

for ( y=1 ; y <= 9 ; y++)

printf(“%d x %d = %d\n”,x,y,x*y);

return 0;

}

Page 211: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute211

Example 4

Draw a flowchart and write a Program that

Calculate and Display The Factorial of n

𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏

Page 212: Computer programming all chapters

Flowcharts Examples

C Programming Higher Technological Institute212

# include <stdio.h>

int main ( )

{

int n,fact; fact=1;

printf(“Enter your N \n”);

scanf(“%d”,&n);

for( int i=1;i<=n;i++)

fact=fact*i;

printf(“ N! =%d \n”,fact);

return 0;

}

Page 213: Computer programming all chapters

Home Work..!!

Rewrite The previous examples using:

a) while Loop

b) do-while Loop

C Programming Higher Technological Institute213

Page 214: Computer programming all chapters

Course Contents

Introduction

Higher Technological Institute214

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

C Programming

Page 215: Computer programming all chapters

Course Contents

Higher Technological Institute215

Arrays8

C Programming

Page 216: Computer programming all chapters

Examples

Higher Technological Institute216

Example 2

Write a program to calculate the

summation and Average of N numbers

read from a data statement.

C Programming

Page 217: Computer programming all chapters

Examples

Higher Technological Institute217

# include <stdio.h>

main ( )

{

int i,n,x,sum; float av;

sum=0;

printf(“How many Numbers ? \n”);

scanf(“%d”,&n);

for ( i=1 ; i<=n ; i++ )

{

printf(“Please enter new no \n”);

scanf(“%d”,&x);

sum=sum+x;

}

av=sum/n;

printf(“Sum=%d \n Av=%f\n”, sum , av);

return 0; }C Programming

Page 218: Computer programming all chapters

Arrays

Higher Technological Institute218

An array is a collection of variables that are of the samedata type.

Each item in an array is called an element.

All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.

The array takes two forms :

One dimensional array (1-D) ; where the data is arranged horizontally or vertically.

Multi-dimensional array; where the data is arranged in two

dimensional (2-D), three dimensional (3-D),…etc.

C Programming

Page 219: Computer programming all chapters

Arrays

Higher Technological Institute219

The following is the syntax form to declare an array:

The data-type is the type specifier that indicates what data type the declared array will be.

Array-Name is the name of the declared array. When choosing a name for an array, you should follow the same rules of naming a variable.

Array-Size defines how many elements the array can contain. Note that the brackets are required in declaring an array.

data-type Array-Name [Array-Size];

C Programming

Page 220: Computer programming all chapters

Arrays Example

Higher Technological Institute220

Where int specifies the data type of the array whose name is arrat_int.

The size of the array is 8, which means that the array can store 8 elements

int array_int [8];

C Programming

Page 221: Computer programming all chapters

Indexing Arrays

Higher Technological Institute221

You can access each of the elements in the array separately.

The following declaration declares an array called day of integer numbers:

You can access the elements in the array of day one after another: day[0], day[1], …, day[6].

The important thing to remember is that all arrays in C are indexed starting at 0.

The first element in the array of day is day[0].

int day [7];

C Programming

Page 222: Computer programming all chapters

Initializing Arrays

Higher Technological Institute222

You can initialize each element in an array by two way.

For instance if we have the array :

Initialize the first element in the array: like this:

Here the value 24 is assigned to the first element of the array number, number[0].

int number [5];

number[0] = 24;

C Programming

Page 223: Computer programming all chapters

Initializing Arrays

Higher Technological Institute223

The second way to initialize an array is to initializeall elements in the array together.

For instance, the following statement initializes allelements of the array, number:

Here the integers inside the braces are assigned tothe corresponding elements of the array number.

That is, 100 is given to the first element(number[0]), 4 to the second element (number[1]),and so on.

int number [5]= {100, 4, 24, 34,5, 16};

C Programming

Page 224: Computer programming all chapters

Initializing Arrays

Higher Technological Institute224

The second way to initialize an array is to initializeall elements in the array together.

For instance, the following statement initializes allelements of the array, number:

Here the integers inside the braces are assigned tothe corresponding elements of the array number.

That is, 100 is given to the first element(number[0]), 4 to the second element (number[1]),and so on.

int number [5]= {100, 4, 24};

C Programming

Page 225: Computer programming all chapters

Initializing Arrays

Higher Technological Institute225

# include <stdio.h>

main ( )

{

int i; int A[10];

for ( i=0 ; i<10 ; i++ )

{

A[i]=i+1;

printf(“A[%d]=%d \n”,i,A[i]);

}

return 0; }

C Programming

Page 226: Computer programming all chapters

Initializing Arrays

Higher Technological Institute226

# include <stdio.h>

main ( )

{

int i; int A[10];

for ( i=0 ; i<10 ; i++ )

{

A[i]=i+1;

printf(“A[%d]=%d \n”,i,A[i]);

}

return 0; }

C Programming

Page 227: Computer programming all chapters

Example

Higher Technological Institute227

Example

Write a program to calculate the

Summation and Average of 10 numbers read

from a data statement.( Using Array )

C Programming

Page 228: Computer programming all chapters

Example

Higher Technological Institute228

# include <stdio.h>

main ( )

{

int i; int A[10]; int sum=0; float av;

for ( i=0 ; i<10 ; i++ )

{

printf(“Enter the value of Element [%d] \n”,i);

scanf(“%d”,A[i]);

sum=sum+A[i]

}

av=sum/10;

printf(“Sum=%d \n Av=%f\n”, sum , av);return 0; }

C Programming

Page 229: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute229

int A[3][4] ;

Rows

Columns

C Programming

Page 230: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute230

int A[3][4] = { { 1 , 2 , 3 , 4 },

{ 5 , 6 , 7 , 8 },

{ 9 , 8 , 7 , 6 }} ;

Assigning Static Value

A[3][4] = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 8 , 7 , 6 }} ;

OR

C Programming

Page 231: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute231

Example 1

Write a program to calculate and display

the Summation of two Matrices A and B.

A = 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

, B = 𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

C Programming

Page 232: Computer programming all chapters

Example

Higher Technological Institute232

# include <stdio.h> main ( ){

int A[3][3] = { { 1 , 2 , 3 } ,

{ 4 , 5 , 6 } ,

{ 7 , 8, 9 } } ;

int B[3][3] = { { 9 , 8 , 7 } ,

{ 6 , 5 , 4 } ,

{ 3 , 2, 1 } } ;

return 0; }

C Programming

Page 233: Computer programming all chapters

Example

Higher Technological Institute233

# include <stdio.h> main ( ){

int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;

int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;

return 0; }

C Programming

Page 234: Computer programming all chapters

Example

Higher Technological Institute234

# include <stdio.h> main ( ){

int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;

int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;

int S[3][3] ; int i,j;

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

C Programming

Page 235: Computer programming all chapters

Example

Higher Technological Institute235

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

S3,3 = A3,3 + B3,3

= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎

at i = 0 j = 0

C Programming

Page 236: Computer programming all chapters

Example

Higher Technological Institute236

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

S3,3 = A3,3 + B3,3

= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎

at i = 0 j = 1

C Programming

Page 237: Computer programming all chapters

Example

Higher Technological Institute237

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

S3,3 = A3,3 + B3,3

= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎

at i = 1 j = 1

C Programming

Page 238: Computer programming all chapters

Example

Higher Technological Institute238

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

S3,3 = A3,3 + B3,3

= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎

at i = 2 j = 0

C Programming

Page 239: Computer programming all chapters

Example

Higher Technological Institute239

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

S3,3 = A3,3 + B3,3

= 𝟏 𝟐 𝟑𝟒 𝟓 𝟔𝟕 𝟖 𝟗

+𝟗 𝟖 𝟕𝟔 𝟓 𝟒𝟑 𝟐 𝟏

=𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎𝟏𝟎 𝟏𝟎 𝟏𝟎

at i = 2 j = 2

C Programming

Page 240: Computer programming all chapters

Example

Higher Technological Institute240

# include <stdio.h> main ( ){

int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;

int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;

int S[3][3] ; int i,j;

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }

C Programming

Page 241: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute241

Example 2

Write a program to calculate and display

the Summation of any two 3x3 Matrices.

C Programming

Page 242: Computer programming all chapters

Example 2

Higher Technological Institute242

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

.

.

.

.

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }C Programming

Page 243: Computer programming all chapters

Example 2

Higher Technological Institute243

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }C Programming

Page 244: Computer programming all chapters

Example 2

Higher Technological Institute244

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

{ S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

printf(“S[%d][%d] = %d \n ”, i , j , S [ i ][ j ] ) ; }

return 0; }C Programming

Page 245: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute245

Example 3

Write a program to calculate and display

the Summation of any two 3x3 Matrices.

Then from Summation Matrix Find :

a) The Largest and Smallest element.

b) The average

c) Matrix Transpose

C Programming

Page 246: Computer programming all chapters

Example 3

Higher Technological Institute246

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

return 0; }

C Programming

Page 247: Computer programming all chapters

Example 3

Higher Technological Institute247

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/

for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ ) /* Rows*/

for ( j=0 ; j<3 ; j++ ) /* Columns*/

if (L<S[i][j])

L=S[i][j];

return 0; }

C Programming

Page 248: Computer programming all chapters

Example 3 (a)

Higher Technological Institute248

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (L<S[i][j])

L=S[i][j];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (M>S[i][j])

M=S[i][j];

printf(“Largest Element = %d \n ”, L ) ;

printf(“Smallest Element = %d \n ”, M ) ;

return 0; }

C Programming

Page 249: Computer programming all chapters

Example 3 (a) (b)

Higher Technological Institute249

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (L<S[i][j])

L=S[i][j];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (M>S[i][j])

M=S[i][j];

printf(“Largest Element = %d \n ”, L ) ;

printf(“Smallest Element = %d \n ”, M ) ;

int sum = 0 ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

sum=sum + S[i][j];

float average =sum/9 ;

printf(“Average = %d \n ”, average ) ;

return 0; }

C Programming

Page 250: Computer programming all chapters

Example 3 (a) (b) (c)

Higher Technological Institute250

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (L<S[i][j])

L=S[i][j];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (M>S[i][j])

M=S[i][j];

printf(“Largest Element = %d \n ”, L ) ;

printf(“Smallest Element = %d \n ”, M ) ;

int sum = 0 ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

sum=sum + S[i][j];

float average =sum/9 ;

printf(“Average = %d \n ”, average ) ;

int ST[3][3];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

ST[i][j]=S[j][i];

return 0; }

C Programming

Page 251: Computer programming all chapters

Example 3 (a) (b) (c)

Higher Technological Institute251

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (L<S[i][j])

L=S[i][j];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (M>S[i][j])

M=S[i][j];

printf(“Largest Element = %d \n ”, L ) ;

printf(“Smallest Element = %d \n ”, M ) ;

int sum = 0 ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

sum=sum + S[i][j];

float average =sum/9 ;

printf(“Average = %d \n ”, average ) ;

int ST[3][3];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

ST[i][j]=S[j][i];

return 0; }

C Programming

Do Not Forget to

add the display

statements for ST

Page 252: Computer programming all chapters

Example 3 (a) (b) (c)

Higher Technological Institute252

# include <stdio.h> main ( ){ int A[3][3] , B[3][3] , S[3][3]; int i,j ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter A[%d][%d] = \n ”, i , j ) ;

scanf(“%d”,A[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf(“Please Enter B[%d][%d] = \n ”, i , j ) ;

scanf(“%d”, B[i][j]); }

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

S [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;

int L=S[0][0], M=S[0][0];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (L<S[i][j])

L=S[i][j];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

if (M>S[i][j])

M=S[i][j];

printf(“Largest Element = %d \n ”, L ) ;

printf(“Smallest Element = %d \n ”, M ) ;

int sum = 0 ;

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

sum=sum + S[i][j];

float average =sum/9 ;

printf(“Average = %d \n ”, average ) ;

int ST[3][3];

for ( i=0 ; i<3 ; i++ )

for ( j=0 ; j<3 ; j++ )

{ printf (“\n”); ST[i][j]=S[j][i];

printf (“%d”, ST[i][j]); }

return 0; }

C Programming

Page 253: Computer programming all chapters

Matrix Multiplication

Higher Technological Institute253

Example 4

Write a program to calculate and display

the Multiplication of any two 3x3 Matrices.

C Programming

Page 254: Computer programming all chapters

Two Dimensional Arrays

Higher Technological Institute254

Remember

C Programming

=

New Matrix

Dimensions

Page 255: Computer programming all chapters

Example 4

Higher Technological Institute255

# include <stdio.h> main ( ){ int A[4][3]={ 2 , 3 , 4 ,

1 , 2 , 1 ,2 , 1 , 0 ,1 , 0 , 2 };

int B[3][4]={ 10 , 0 , 20 , 10 ,0 , 20 , 0 , 20,

20 , 10 , 10 , 0 };int M[4][4];int ra = 4 , ca = 3 , rb = 3, cb = 4 ;int i , j , k ;for (i=0;i<ra;i++)for (j=0;j<cb;j++){M[i][j]=0;for(k=0;k<ca;k++)M[i][j]= M[i][j]+ A[i][k]*B[k][j];}

printf("\n The M is \n");

for(i=0;i<ra;i++)

{

printf("\n");

for (j=0;j<cb;j++)

printf("%4d",M[i][j]);

}

return 0 ; }

C Programming

Page 256: Computer programming all chapters

C Programming Higher Technological Institute256

Home Work ..!!

Write a program to calculate thedeterment of 3x3 Matrix.

Page 257: Computer programming all chapters

Course Contents

Introduction

Higher Technological Institute257

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

C Programming

Page 258: Computer programming all chapters

Course Contents

Higher Technological Institute258

C Functions9

C Programming

Page 259: Computer programming all chapters

Functions

Higher Technological Institute259

Why Function

o To make large programs Manageable,

Programmers modularize them into

subprograms.

o These subprograms are called

Functions

C Programming

Page 260: Computer programming all chapters

Functions

Higher Technological Institute260C Programming

C Standard FunctionsOR

Built in Function

Page 261: Computer programming all chapters

C Standard Functions

Higher Technological Institute261

The C language has a standard library which is acollection of predefined functions and other programelements which are accessed through header files.

One of the most important header file <math.h>contains useful mathematical functions.

C Programming

Note

Every mathematical function return doubledata type

Page 262: Computer programming all chapters

Some Mathematical in <math.h>

Higher Technological Institute262C Programming

Function Descriptioncos(x) Cosine of x in Radian

exp(x) Exponential of x (base e)

log(x) Natural Logarithm of x (base e)

log10(x) Common Logarithm of x (base e)

pow(x,p) x to the power p = xP

sin(x) Sine of x in Radian

sqrt(x) Square root of x = 𝒙

tan(x) Tangent of x in Radian

Page 263: Computer programming all chapters

Example 1

Higher Technological Institute263C Programming

Write a program to calculate the following:

a) cos (x)b) e x

c) x 3/2

d) 𝒙Test your program with

x = 8, 16, 32

Page 264: Computer programming all chapters

Example 1

Higher Technological Institute264C Programming

# include <stdio.h>

# include <math.h>

main ( )

{

int x=8; double a,b,c,d;

do

{

a = cos(x*180/(22/7));

b=exp(x);

c=pow(x, 3/2);

d=sqrt(x);

printf(“At x = %d \n A = %f \n B= %f \n C= %f \n D=%f \n ”,x,a,b,c,d);x=x*2;

} while ( x<=32 );

return 0;

}

Page 265: Computer programming all chapters

Example 1

Higher Technological Institute265C Programming

# include <stdio.h>

# include <math.h>

main ( )

{

int x=8; double a,b,c,d;

do

{

a = cos(x*180/(22/7));

b=exp(x);

c=pow(x, 3/2);

d=sqrt(x);

printf(“At x = %d \n A = %f \n B= %f \n C= %f \n D=%f \n ”,x,a,b,c,d);x=x*2;

} while ( x<=32 );

return 0;

}

Page 266: Computer programming all chapters

Example 1

Higher Technological Institute266C Programming

# include <stdio.h>

# include <math.h>

main ( )

{

int x=8; double b ;

b=exp(x);

return 0;

}

x

b

8

Function 1

Function 2

exponential Function

math

main

Page 267: Computer programming all chapters

Example 1

Higher Technological Institute267C Programming

# include <stdio.h>

# include <math.h>

main ( )

{

int x=8; double b ;

b=exp(x);

return 0;

}

x

b

8

Function 1

Function 2

exponential Function

math

main

8

Page 268: Computer programming all chapters

Example 1

Higher Technological Institute268C Programming

# include <stdio.h>

# include <math.h>

main ( )

{

int x=8; double b ;

b=exp(x);

return 0;

}

x

b

8

Function 1

Function 2

exponential Function

math

main

8

8886110.520508

Page 269: Computer programming all chapters

Some Header Files in C Library

Higher Technological Institute269C Programming

Header File

Description

<ctype.h> Defines function to test characters

<float.h> Defines constant to float

<limits.h>Defines the integer limits on your local computer

<math.h> Defines mathematical function

<stdio.h> Defines functions for standard input and output

<stdlib.h> Defines utility functions

<string.h> Defines functions for processing string

<time.h> Defines time and date functions

Page 270: Computer programming all chapters

User Defined Function

Higher Technological Institute270C Programming

You Have to Know

o Function Type

o Function Name

o Arguments to the function

Typevoid

returnable

Name

Func_1

n1

Arguments

with

without

Page 271: Computer programming all chapters

Build Your Function

Higher Technological Institute271

Func_Type Func_name( type1 Arg1, type2 Arg2 )

C Programming

{

statment1 ;

statment2 ;

.

.

.

return variable;

}

Function’s

Body

Page 272: Computer programming all chapters

Where I can put my Function ??

Higher Technological Institute272

# include <stdio.h>

func_type Func_1( Argument1, Argument2 )

{

statment1 ;

statment2 ;

return ;

}

main ( )

{

Func1 Call ;

return 0;

}

C Programming

Main Function

Function’s Body

Page 273: Computer programming all chapters

Where I can put my Function ??

Higher Technological Institute273

# include <stdio.h>

func_type Func_1( typeArgument1, typeArgument2 )main ( )

{

Func1 Call ;

return 0;

}

func_type Func_1( Argument1, Argument2 )

{

statment1 ;

statment2 ;

return ;

}

C Programming

Function’s Body

Main Function

;Function DeclarationOR

Page 274: Computer programming all chapters

Where I can put my Function ??

Higher Technological Institute274

# include <stdio.h>

func_type Func_1( typeArgument1, typeArgument2 )main ( )

{

Func1 Call ;

return 0;

}

func_type Func_1( Argument1, Argument2 )

{

statment1 ;

statment2 ;

return ;

}

C Programming

Function’s Body

Main Function

;Function Declaration

Please Do not

forget ; after

Declaration Statement

Page 275: Computer programming all chapters

Simple Example

Higher Technological Institute275

# include <stdio.h>

int sum ( int x, int y)

{

z = x + y;

return z ;

}

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

C Programming

Page 276: Computer programming all chapters

Simple Example

Higher Technological Institute276

# include <stdio.h>

int sum ( int x, int y)

{

z = x + y;

return z ;

}

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

C Programming

Page 277: Computer programming all chapters

Simple Example

Higher Technological Institute277

# include <stdio.h>

int sum ( int x, int y)

{

z = x + y;

return z ;

}

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

C Programming

Page 278: Computer programming all chapters

Simple Example

Higher Technological Institute278

# include <stdio.h>

int sum ( int x, int y)

{

z = x + y;

return z ;

}

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

C Programming

• Why we use x, y,z in Function’s Body

While use a,b,c in main Function …?

• Effect of use the same variable name.

Page 279: Computer programming all chapters

Simple Example by Method 2

Higher Technological Institute279

# include <stdio.h>

int sum ( int, int ) ;

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

int sum ( int x, int y)

{

z = x + y;

return z ; }

C Programming

Page 280: Computer programming all chapters

Simple Example by Method 2

Higher Technological Institute280

# include <stdio.h>

int sum ( int, int ) ;

main ( )

{

int a, b, c;

a = 3;

b = 5;

c = sum( a , b );

printf("C=%d \n", c);

return 0;

}

int sum ( int x, int y)

{

z = x + y;

return z ; }

C Programming

Again Do not

forget ; after

Declaration Statement

Page 281: Computer programming all chapters

Why Functions

Higher Technological Institute281

ExampleWrite a program to calculate the

factorial of any integer number. Then use

your program to calculate the value of Y.

𝒀 =𝒏! − 𝒌!

𝒏 − 𝒌 !

C Programming

Page 282: Computer programming all chapters

Example

Higher Technological Institute282

# include <stdio.h>

main ( )

{

int n ;

printf(“Please Enter The Value of n \n");

scanf(“%d”,&n);int factorial =1;

for ( int i=n ; i>=1 ; i-- )

factorial = factorial * i ;

printf(“Factorial =%d \n", factorial);

return 0;

}

C Programming

Page 283: Computer programming all chapters

Example

Higher Technological Institute283

# include <stdio.h>

int fact ( int ) ;

main ( )

{

int n, k, y;

printf(“Please Enter The Value of n and k \n");

scanf(“%d %d”,&n,&k);y = ( fact(n) * fact(k) ) / fact( n – k ) ;

printf(“Y=%d \n", y);

return 0;

}

int fact ( int fc )

{

int factorial =1;

for ( int i=fc ; i>=1 ; i-- )

factorial = factorial * i ;

return factorial ;

}

C Programming

Page 284: Computer programming all chapters

LOGO

Eng. Ibrahim Elewah

Higher Technological Institute 10th of Ramadan City6th of October Branch

Electrical and Computer Engineering Department

284

HTI Student Book

Main Reference “C For Dummies”

by Dan Gookin 2nd Edition