Professional Documents
Culture Documents
Arrays
An array is collection of elements of similar data type.
Why Arrays????
Arrays are used when there is a need to use many variables of
the same type. Say you want to store marks scored by 60 students of
our class in C programming subject, so how many variables you need
to declare to hold 60 values? 60 variables? Will it be feasible? So use
an array for the same task instead. The programmer has to just specify
the data type of elements and the number of elements that are
required as the size of the array.
60 variable’s declaration???
int s1,s2,s3,……..s60;
Or
int s[60] ;
Which is easy???
int a[5]; int a[5][5]; int a[5][5][5];
One Dimensional Array (1D Array)
Syntax for Declaring 1D Array
Data_Type Array_Name[Size];
Example for Declaring 1D Array
a
Name of
the a[0] a[2] a[3] a[4]
a[1]
array
Subscript or index, used to access the specific value from an array, a[3]
gives 40
Initializing 1D Array
1. Compile Time Initialization
1 2 3 4 5 1 2 3 0 0
a[0] a[1] a[2] a[3] a[4] a[0] a[1] a[2] a[3] a[4]
1 2 3 4 5 0 0 0 0 0
a[0] a[1] a[2] a[3] a[4] a[0] a[1] a[2] a[3] a[4]
Write a C Program to scan and print n array elements.
Write a C Program to scan n elements in an array and find the sum
and average of all the elements of the array & print the same
Write a C program to read marks scored by each student in C
programming subject in first internal exam, and calculate the average.
Write a C program to find the largest element in an array of n elements
Write a C program to find the largest and smallest element in an array of
n elements
Write a C Program to copy the elements of one array to another array.
a 1 2 3 4 5 b[0] = a[0]
a 1 2 3 4 5 c[0] = a[0]+b[0]
c[1] = a[1]+b[1]
a[0] a[1] a[2] a[3] a[4]
c[2] = a[2]+b[2]
c[3] = a[3]+b[3]
b 6 7 8 9 10 c[4] = a[4]+b[4]
In general,
b[0] b[1] b[2] b[3] b[4]
c[i] = a[i] + b[i]
c 7 9 11 13 15 where i=0 to n-1