c programming slide c05

Post on 13-Jan-2015

3.814 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Monday, April 10, 2023

1

PRADEEP DWIVWDI

PREPARED BY-PRADEEP DWIVEDI(persuing B.TECH-IT)from HINDUSTAN COLLEGE OF SCIENCE AND

TECHNOLOGY(MATHURA)MOB-+919027843806E-MAIL-pradeep.it74@gmail.com

C-PROGRAMMING SLIDE-5

Monday, April 10, 2023

2

PRADEEP DWIVWDI

ARRAY

TOPIC

Monday, April 10, 2023PRADEEP DWIVWDI

3

ARRAY

We use fundamental data type, namely char, int, float, double. Variable of these types can store only one value at a time.

In many application we need to store more than one value of data in a single variable that time we use array.

ARRAY:- is a fixed size sequenced collection of same data type.

In other words, an ARRAY is a special variable that can hold the place for more than one values of same data type at adjacent places.

Monday, April 10, 2023PRADEEP DWIVWDI

4

EXAMPLE OF ARRAY

List of name. List of number. List of students mark. etc.

Monday, April 10, 2023PRADEEP DWIVWDI

5

DECLARATION AND MEMORY REPRESENTATION OF AN ARRAY

int a[10];

0 1 2 3 4 5 6 7 8 9

Memoryrepresentatio

n

Index /subscript

ed number

Monday, April 10, 2023PRADEEP DWIVWDI

6

TYPES OF ARRAY

We have following types of array-1. One dimensional array.2. Two dimensional array.3. Multidimensional array.

Monday, April 10, 2023PRADEEP DWIVWDI

7

ONE DIMENSIONAL ARRAY

A list of item can given one variable name using only one subscript and such a variable called single subscripted variable or a one dimensional array.

For, eg- if we want to represent a set of five numbers says (10,20,30,40,50), by an array variable num, then we declare the variable num as follows-

int num[5];

Monday, April 10, 2023PRADEEP DWIVWDI

8

ONE DIMENSIONAL ARRAY

The computer reserves five storage in memory-

num[0]num[1]num[2]num[3]num[4]

The value to the array element can be assigned as follows-

num[0]=10;num[1]=20;num[2]=30;num[3]=40;num[4]=50;

Monday, April 10, 2023PRADEEP DWIVWDI

9

DECLARATION OF ONE DIMENSIONAL ARRAY

Declaration of an array must specifies three things-1. Type2. Name3. Size General form of an array declaration is- eg, float height[10]; int num[10]; char name[10];

type variable_name[size];

Monday, April 10, 2023PRADEEP DWIVWDI

10

DECLARATION OF ONE DIMENSIONAL ARRAY

character array represents maximum number of characters that the string can hold.

for eg, char name[10]; declare the name array(string) variable

that can hold a maximum of 10 characters.

suppose we want to read the following string constant into the string variable name.

“WELL DONE”

Monday, April 10, 2023PRADEEP DWIVWDI

11

DECLARATION OF ONE DIMENSIONAL ARRAY

each character of the string treated as an element of array name and stored in the memory as follows-

NOTE:- when the compiler sees the string, it

terminate with an additional null character thus, the element name[10] holds the

null character ‘\0’. when declaring character array, we must allow one extra element space for null

terminator

‘W’

‘E’

‘L’

‘L’

‘ ‘

‘D’

‘O’

‘N’

‘E’

‘\0’

Monday, April 10, 2023PRADEEP DWIVWDI

12

INITIALIZATION OF ONE DIMENSIONAL ARRAY

an array can be initialized at either of the following stages-

1. at compile time.2. at run time.

Monday, April 10, 2023PRADEEP DWIVWDI

13

COMPILE TIME INITIALIZATION

the general form of initialization of an array is-

the value in the list are separated by commas- eg; int num[3]={1,1,1}; if the number of values in the list is less than

the number of elements, then only that many element will be initialized , the remaining element will be set to zero automatically.

eg; float total[5]={0.0,1.4,-4.7};

type array_name[size]={list of values};

Monday, April 10, 2023PRADEEP DWIVWDI

14

COMPILE TIME INITIALIZATION size may be omitted , in such case compiler

allocates enough free space for all initialized elemets.

for eg, int num[]={1,2,3,4,5}; character array may be initialized in similar

manner.char name[]={‘J’,’O’,’H’,’N’,’/0’};

alternatively, we can assign the string literal directly

eg, char name[]=“JOHN”;

Monday, April 10, 2023PRADEEP DWIVWDI

15

NOTE

At the time of declaration size must be specified of an array.

int a[]; If we don’t mention the size of an array

at the declaration that time we must initialized it-

int a[]={10,20,30,40}; int a[4]={10,20,30,40, 50,60,70}; only

take first four values.

incorrect

Monday, April 10, 2023PRADEEP DWIVWDI

16

NOTE

In character array null character placed after the string char[8];

Suppose we want to store-PRADEEP

char a[20];P R A D E E P /0

P R A D E E P \0

Monday, April 10, 2023PRADEEP DWIVWDI

17

RUN TIME INITIALIZATION

an array can be explicitly initialized at run time . this approach is usually applied for initializing large

arrays. for eg;

for(i=0;i<100;i++){

if(i<50)sum[i]=0.0;

elsesum[i]=1.0;}

Monday, April 10, 2023

18

PRADEEP DWIVWDI

prog23

//Demo for array#include<stdio.h>#include<conio.h>void main(){int num[5],i;clrscr();printf("Enter five

numbers:");for(i=0;i<5;i++){

scanf("%d",&num[i]);}printf("\nThe element of

array:-\n");for(i=0;i<5;i++){printf("%d\n",num[i]);}getch();}

Monday, April 10, 2023

19

PRADEEP DWIVWDI

prog24

//w.a.p. to find out smallest element in the array.

#include<stdio.h>#include<conio.h>void main(){int num[5],small,i;clrscr();printf("Enter any five

numbers:\n");

for(i=0;i<5;i++){scanf("%d",&num[i]);}small=num[0];for(i=0;i<5;i++){if(small>num[i])small=num[i];}printf("the smallest number is:

%d",small);getch();}

Monday, April 10, 2023

20

PRADEEP DWIVWDI

prog25

//w.a.p. to arrange the element in ascending order.(selection sort)

#include<stdio.h>#include<conio.h>void main(){int num[5],i,j,temp;clrscr();printf("Enter five numbers:\n");for(i=0;i<5;i++){scanf("%d",&num[i]);}for(i=0;i<5;i++){for(j=i+1;j<5;j++)

{if(num[i]>num[j]){temp=num[i];num[i]=num[j];num[j]=temp;}}}for(i=0;i<5;i++){printf("%d\t",num[i]);}getch();}

Monday, April 10, 2023PRADEEP DWIVWDI

21

TWO DIMENSIONAL ARRAY

If we want to arrange the element in a row and column format of an array that time we use two dimensional array.

In that first dimensional tells about the number of rows and second dimensional tells about the number of columns.

For eg- int a[3][2];

rows columns

Monday, April 10, 2023PRADEEP DWIVWDI

22

REPRESENTATION OF TWO DIMENSIONAL ARRAY

If we want to represent an array for eg-int a[2][3];

col 0 col 1 col 2 00 01 02

Row 0

Row 110 11 12

Monday, April 10, 2023

23

PRADEEP DWIVWDI

prog26

//write a program to print a matrix#include<stdio.h>#include<conio.h>void main(){int a[3][3],i,j;clrscr();printf("Enter the array elements:");for(i=0;i<3;i++){for(j=0;j<3;j++){scanf("%d",&a[i][j]);}}

printf("The elements of array are:\n");

for(i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",a[i][j]);}printf("\n");}getch();}

Monday, April 10, 2023PRADEEP DWIVWDI

24

Memory representation of prog 26

0 1 2 0

1 2

3(00)

4(01)

5(02)

4(10)

6(11)

6(12)

7(20)

8(21)

9(22)

Monday, April 10, 2023

25

PRADEEP DWIVWDI

prog27

//write a program for matrix addition#include<stdio.h>#include<conio.h>void main(){int a[3][3],b[3][3],c[3][3],i,j;clrscr();printf("Enter the first matrix:\n");for(i=0;i<3;i++){for(j=0;j<3;j++){scanf("%d",&a[i][j]);}}printf("Enter the second matrix:\n");for(i=0;i<3;i++){for(j=0;j<3;j++)

{scanf("%d",&b[i][j]);}}

for(i=0;i<3;i++){for(j=0;j<3;j++){c[i][j]=a[i][j]+b[i][j];}}printf("\n The addition of two matrix:\n");for(i=0;i<3;i++){for(j=0;j<3;j++){printf("%d\t",c[i][j]);}printf("\n");}getch();}

Monday, April 10, 2023PRADEEP DWIVWDI

26

Explaination

matric a matrix b matrix c

+ =3 4 5

4 6 6

7 8 9

1 2 3

4 6 2

3 4 5

4 6 8

8 12 8

10 12 14

Monday, April 10, 2023PRADEEP DWIVWDI27

THANKS

top related