Académique Documents
Professionnel Documents
Culture Documents
Muhammad Suffian
HOW TO STORE THE ITEMS?
Take 100 numbers as input and perform
following functions..
Sum
Average
Sort in Ascending order
char symbol[10];
int arr[ ] = { 12, 15, 275, 100, 20, 57, 93, 102};
ARRAY…
A Collection of items of same type.
Contiguous memory
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
Pass by value
int arr [10];
int main()
{
int arr[5];
int sum,j;
printf("Enter 5 numbers for calculating sum:\n");
//Function definition
int main()
{
int arr[5];
int sum,j;
printf("Enter 5 numbers for calculating sum:\n");
//Function definition
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];
int main()
{
int i,j,a[3][3];
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