Vous êtes sur la page 1sur 20

ARRAYS

Muhammad Suffian
HOW TO STORE THE ITEMS?
 Take 100 numbers as input and perform
following functions..
 Sum
 Average
 Sort in Ascending order

 Take 100 student IDs as input…

 Think more examples….


ARRAY IS THE SOLUTION….
 int roll[10];
0 1 2 3 4 5 6 7 8 9
10 5 23 75 87 92 3 45 11 53

 roll[i] where i = 1…100 ( as “ i ” in loops)


 “ i ” is the index or subscript…..
 roll[0] = 10
 roll[9] = 53
 roll [10] = ERROR!!! --- out of bound

 Items are placed in contiguous memory…

 char symbol[10];

 int arr[ ] = { 12, 15, 275, 100, 20, 57, 93, 102};
ARRAY…
 A Collection of items of same type.

 Contiguous memory

 Memory fixed at compile time (Static)


 Can not be extended at run time [Must know the
number of items before compilation/execution]
ARRAY…
 int num[5];

 num[0] = 2;
 num[1] = 8;

 num[2] = 7;

 num[3] = 6;

 num[4] = 0;
EXAMPLE
#include<stdio.h>

int main()
{
int num[10];
int i,j;
for(i=0; i<=9; i++)
{
num[i] = i*2;
}
for(j=0; j<=9; j++)
{
printf("number at index %d is: %d \n", j, num[j]);
}
return 0;
}
OPERATIONS ON ARRAYS

 Insertion

 Deletion

 Search

 Sort
PASSING ARRAY TO FUNCTION
 Arrays stores a collection of items
 Provides flexibility to pass a collection to a function

 Two ways to pass array to a function


 Pass by reference
int arr [10];

int sum ( int *arr);

 Pass by value
int arr [10];

int sum ( int arr[10]);

//int sum (int arr [], int size);


PASSING ARRAY TO FUNCTION-I
#include<stdio.h>

int add(int arr[5]);// Function declaration

int main()
{
int arr[5];
int sum,j;
printf("Enter 5 numbers for calculating sum:\n");

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


{
scanf("%d", &arr[j]); // Reading numbers
//printf("%d", arr[j]);
}
sum = add(arr); // funtion call
printf("\nsum is: %d \n", sum);
return 0;
} // FUNCTION DEFINITION ON NEXT SLIDE….
PASSING ARRAY TO FUNCTION – I

//Function definition

int add (int arr[5])


{
int i, sum = 0;
for(i=0; i<5; i++)
{
sum = sum + arr[i];
}
return (sum);
}
PASSING ARRAY TO FUNCTION-II
#include<stdio.h>

int add(int arr[ ], int size);// Function declaration

int main()
{
int arr[5];
int sum,j;
printf("Enter 5 numbers for calculating sum:\n");

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


{
scanf("%d", &arr[j]); // Reading numbers
//printf("%d", arr[j]);
}
sum = add(arr, 5); // funtion call
printf("\nsum is: %d \n", sum);
return 0;
} // FUNCTION DEFINITION ON NEXT SLIDE….
PASSING ARRAY TO FUNCTION – II

//Function definition

int add (int arr[ ], int size)


{
int i, sum = 0;
for(i=0; i<size; i++)
{
sum = sum + arr[i];
}
return (sum);
}
HOW TO STORE THE ITEMS???
 Input GPAs of 30 students in all 5 courses.
 Perform the following tasks:
 Calculate the CGPA of each student
 Identify the highest GPA in each course

 Etc…

 Solution
 For Student wise storage
 Use 30 arrays, each of size 5
 For course wise storage
 Use 5 arrays, each of size 30
SOLUTION: TWO DIMENSIONAL ARRAY
 float arr[5][5];

cs10 cs20 cs30 cs40 cs50


CS102 3.0 3.33 1.33 1.33 2.33

CS101 3.33 3.0 3.33 2.33 1.33

MT101 2.33 1.33 3.0 3.33 1.33

HU102 1.33 2.33 2.67 3.0 3.33

HU104 2.67 2.67 2.33 2.67 3.0


SOLUTION: TWO DIMENSIONAL ARRAY
 float arr[5][5];

Col 0 Col 1 Col 2 Col 3 Col 4

Row 0 3.0 3.33 1.33 1.33 2.33

Row 1 3.33 3.0 3.33 2.33 1.33

Row 2 2.33 1.33 3.0 3.33 1.33

Row 3 1.33 2.33 2.67 3.0 3.33

Row 4 2.67 2.67 2.33 2.67 3.0


2D ARRAYS: MEMORY REPRESENTATION
 int arr[3][3];
 3 x 3 array
2D ARRAYS: MEMORY REPRESENTATION
 int arr[3][3];
 3 x 3 array
2D ARRAYS: MEMORY REPRESENTATION
 int arr[3][3];
 3 x 3 array
Element Memory Location
a[0][0] 4000
a[0][1] 4002
a[0][2] 4004
a[1][0] 4006
a[1][1] 4008
a[1][2] 4010
a[2][0] 4012
a[2][1] 4014
a[2][2] 4016
2D ARRAY EXAMPLE
#include<stdio.h>

int main()
{
int i,j,a[3][3];

/* i = For Counting Rows


j = For Counting Columns */

/* Accept 9 elements from user */

for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{
printf("\nEnter the a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
/* Print 9 elements */

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

return 0;
}
LIMITATIONS OF ARRAYS
 Static..
 Memory allocated at Compile time
 Can not be changed at Run time

 All data stored in an array MUST of same data


type.
 Insertion is Inefficient
 Requires shifting of values downwards (right)
 Deletion is Inefficient
 Requires shifting of values upwards (left)
 Size must be known before use..
 If more space required… we are short at space
 If less space utilized…. Memory wasted

Vous aimerez peut-être aussi