43
To presentation

Array 2 hina

  • Upload
    heena94

  • View
    58

  • Download
    0

Embed Size (px)

Citation preview

To presentation

.BCA II SEM.

SUBMITTED TO:

CHETAN SIR

SUBMITTED BY :

HINA KHAN

Prakash KhaireThe Mandvi Education Society Institute of Computer Science

A

R

R

A

Y

S

CONTENTS

Introduction Need for on array Types of array• 1D• 2D• MD Difference between 1D & 2D Array Array Applications Advantages & disadvantages

What is an Array ?

• It is derived data type.

• The nature of array is static (fix).

• Array is a collection of similar or

homogeneous data type.

• Array is also known subscript variable

or index variable.

• The lower bond of an array is zero 0.

• The upper bond of an array is n-1.for ex.-int a[9] is an array that stores 9 integers

• Array index start with zero(0).

• Array end with n-1.

0 1 2 3 4 5 6 7 8

100 102 104 106 108 110 112 114 116

index

elements

Memory address

'

Need for an Array

NEED FOR AN ARRAY

• To store large number of variables of same type under a single variable.

• E.g.To store Marks of 50 students.Record of sales of 100 salesman.

Types of Array

A list of items can be given one variable name using only one subscript and such a variable is called a one-dimensional Array.

•Syntax

data_type ArrayName[size];

data_type : is a valid data type like int, float,char

Arrayname : is a valid identifier

size : maximum number of elements that can

be stored in array

1. Declaration of on ArrayTo declare an array to syntax is-Data_ type variablename [size];

o The type of data which you want to store like – int , float , char.

o Means the name of an Array.

o The number of Array type variable.

Ex.- int a [3];• Type of array variable is “integer”. • Its variable name is a.• 3 is the size of the array.

2. Initialization

• An elements of an array must be initialized, otherwise they may contain “garbage” value.

• An array can be initialized at either of the following stageso At compile timeo At run time

• At the time of declaration and initialization at the same time.

• For ex.- int a[5]={1,2,3,4,5}

An array can be explicitly in initialization at run time.

• To initialize multiple array variable for loop is used.for ex.-

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

scanf (“%d”,&a[i]);}

3.Assessing

• To print the element of an array is known as assessing.

• To assessing the element of an array for loop is used frequently.

for ex.-for (i=0; i<5 ;i++){

printf (“%d”,a[i]);} OUTPUT:

01234

Examples:-1) Enter a one-d array

#include<stdio.h>int main(){int a[10],i;printf(“enter the array=“);for(i=0;i<10;i++){

scanf(“%d”,&a[i]);}printf(“the entered array is=“);for(i=0;i<10;i++){

printf(“%d”,a[i]);}return(0);}

output:

0123456789

• C allows us to define such tables of items by using two-dimensional arrays.

•The two dimensional array are declared as follows-

type array_name[row_size][column_size];

For ex.- int a [3][2];

Data typeArray name Row=3

Column=2

1. Declaration 2D ARRAY• To declare an array to syntax is.• The syntax of 2D array is

datatype arrayname[row] [column];

2.Initializing 2D ARRAY• Like 1D array ,2D array initialized.• Tow for loop is used .

ex.-for (i=0 ;i<3; i++)

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

{scanf(“%d”,&a[i][j]);

}}

3.Assessing 2D ARRAY

• For loop is used.

Ex.-for (i=0; i<3; i++)

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

printf(“\t%d”,a[i][j]);}

print(“\n”);}

• To print the element of an array is known as assessing.

/* addition of two matrix/*

#include<stdio.h>int main(){int i,j;int a[3][3],b[3][3],c[3][3];

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

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

printf(“enter the matrix a=”);scanf(“%d”,&a[i][j]);

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

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

printf(“enter the matrix b=”);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 addition of two matrix=“);{

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

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

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

printf(“/n”);}

}return(0);}

output:

A= B=

C=

9 8 7

6 5 4

3 2 1

1 2 3

4 5 6

7 8 9

10 10 10

10 10 10

10 10 10

/*subtraction of two matrix/*

#include<stdio.h>int main(){int i,j;int a[3][3],b[3][3],c[3][3];

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

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

printf(“enter the matrix a=”);scanf(“%d”,&a[i][j]);

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

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

printf(“enter the matrix b=”);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 substract of two matrix=”);{

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

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

printf(“%d”,c[i][j]);}printf(“/n”);

}}

return(0);}

output:

A= B=

C=

2 3 4

5 6 7

8 9 10

1 2 3

4 5 6

7 8 9

1 1 1

1 1 1

1 1 1

/*multiplication of two matrix/*

#include<stdio.h>int main(){int i,j,k;int a[3][3],b[3][3],c[3][3];

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

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

printf(“enter the matrix a=”);scanf(“%d”,&a[i][j]);

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

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

printf(“enter the matrix b=”);scanf(“%d”,&b[i][j]);

}}

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

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

c[i][j]=0;for(k=0;k<3;k++){c[i][j]=c[i][j]+a[i][k]*b[k][j];}

}}

printf(“\n enter the matix a is =”);for(i=0;i<3;i++){

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

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

}

printf(“\n enter the matrix b is =”);for(i=0;i<3;i++){

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

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

}

printf(“\n the multiplication is =”);for(i=0;i<3;i++){

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

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

}return(0);}

output:

A=

B=

AB=

1 2 3

4 5 6

7 8 9

9 8 7

6 5 4

3 2 1

46 28 10

118 73 28

190 118 46

Not in courseNot in course

• C allows arrays of three or more dimensions. The exact limit is determined by the compiler

•Syntax-

Datatype aray_name[s1][s2][s3]………….[Sm];

• For ex-

int MyArray[2][3][4];

DIFFERENCE1D

1. 1D Array contains single row and multiple columns.

2. 1D Array is a simple collection of elements.

3. Ex:-

2d1. 2D Array contains

multiple row and multiple columns.

2. 2D Array is a collection of 1D Array .

3. Ex:-

1 2 3 4 1 2 3 4

A 1 2 3

B 4 5 6

C 7 8 9

Array Applications

Array Applications

• Array can be used for sorting elements.• Array can perform matrix operation.• Array can be used to sum all the

elements.• Array can be stores elements of same

data type.

• Given a list of test scores, determine the maximum and minimum scores.

• Read in a list of student names and rearrange them in alphabetical order (sorting).

• Given the height measurements of students in a class, output the names of those students who are taller than average.

Advantages

&

Disadvantages

Advantages It is used to represent multiple data of same type by assigning only

single name.

It can be used to implement other data structures like –linked lists , stacks ,queue etc….

2D array are used to represent matrices.

Disadvantages We must know array is static structure It means that array is fixed size. The

memory which is allocated to array can not be increased or reduced.

Since array is of fixed size , if we allocate more memory than requirement then thememory space will be wasted . And if we allocate less memory than requirement than it will create problem.

The elements of array are stored in consecutive memory location. So insertions and deletion are very difficult and time consuming.

Programming in ANSI(E balagurusamy)

Thank you Chetan Sir for giving me this topic.

I would like to thank my friends who helped me Ekta, Sakina, Kunal