Array
Array
1/20
ARR AY S
An array is a collection of elements of the same type that are
referenced by a common name.
int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;
…
…
return 0;
}
By using an array, we just declare like this,
int studMark[1000];
This will reserve 1000 contiguous memory locations for
storing the students’ marks.
Graphically, this can be depicted as in the
following figure.
This absolutely has simplified our declaration of the
variables.
⚫ array_element_data_type array_name[array_
size];
For example:
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[2]);
Output: 3 5
A R R AY E X A M P L E
Take 10 integer input from user and store then in an
array and find the sum of all numbers stored in array.
#include<stdio.h>
int main(){
int i,sum=0,arr[10];
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
for(i=0;i<10;i
++)
sum+=arr[i];
printf(“Sum of
input integers is
%d\n”,sum);
Summarize the response of a survey.
Input: Response of the survey, can be in the range
between 0 and 10. Assume the population size to
be 40.
Output: Frequency of each response.
#include<stdio.h>
#define S I Z E 40
#define A N S 11
int main(void) {
int response[SIZE];
int freq[ANS] = {0};
int i;
for(i=0; i< SIZE; i++){
scanf(“%d”,&response[i]);
++freq[response[i]];
}
for(i=0;i<ANS;i++)
printf("Frequency of %d is %d\n",i,freq[i]);
}
ASSIGNMENT
Read from user ages of all students in class and
save them in an array which can store floating
point and find average, minimum and maximum
age.
A six faced die is rolled 600 times. Find the
frequency of the occurrence of each face?
int rand(void): returns a pseudo-random
number in the range of 0 to R A N D _ M A X .
R A N D _ M A X : is a constant whose default value
may vary between implementations but it is
granted to be at least 32767.
Issue: If we generate a sequence of random
number with rand() function, it will create the
same sequence again and again every time
program runs.
The srand() function sets the starting point for
producing a series of pseudo-random integers. If
srand() is not called, the rand() seed is set as if
srand(1) were called at program start.
The pseudo-random number generator should
only be seeded once, before any calls to rand(),
and the start of the program.
Standard practice is to use the result of a call
to srand(time(0)) as the seed. However,
time()
returns a time_t value which vary everytime
and hence the pseudo-random number vary for
every program call.
#include<stdio.h>
int main(){
int i,j,arr[7]={0};
srand(time(0));
for (i=0;i<600;i++){
j=rand()%6;
j=j+1;
arr[j]++;
}
for(i=1;i<=6;i++)
printf("%d came
out for %d
times\
n",i,arr[i]);
[sourav@gaya]$ ./a.out
1 came out for 113 times
2 came out for 114
times 3 came out for
102 times 4 came out
for 86 times 5 came out
for 99 times 6 came out
for 86 times
ASSIGNMENT
int main(){
int i,j,x,arr[10];
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<9;i++){
x=arr[i];
for(j=i+1;j<10;j++)
{
if(x==arr[j])
printf("%
d
number
appeared
more
than
[sourav@gaya]$ ./a.out
Enter 10 integer number
1
2
3
4
5
6
2
4
8
9
2 number appeared more
than once
4 number appeared more
T W O D I M E N S I O N A L / 2 D A R R AY S
For examples,
⚫ int xInteger[3][4];
⚫ float matrixNum[20][25];
Marks
Students 10 23 31 11
20 43 21 21
12 22 30 13
30 31 26 41
13 03 41 15
int main(void)
{
int i, j;
double total;
int
marks[ROW]
[COL]= { 10,
23, 31, 11,
20, 43, 21,
21,12,
22, 30, 13, 30, 31,
26, 41,13, 03, 41,
15 };
for(i = 0; i <
ROW ; i++)
{
total =
I N I T I A L I Z AT I O N O F 2D A R R AY
int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second
dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason
mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
For array storing string
void main()
{
static int array[10][10];
int i, j, m, n;
printf("Enter the order
of the matrix \n ");
scanf("%d %d", &m,
&n);
scanf("%d", &a[i][j]);
}
scanf("%d",&b[i][j]);
}
⚫ // Initializing all elements of result matrix to 0
⚫ for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
result[i][j] = 0;
}
⚫ result[i][j]+=a[i][k]*b[k][j];
}
⚫ // Displaying the result
⚫ printf("\nOutput Matrix:\n");
⚫ for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
printf("%d ", result[i][j]);
}
⚫ return 0;
}
3 D : A 3D A R R AY I S A N A R R AY O F 2D
A R R AYS .
#include<stdio.h>
int main(){
int
i,j,k,x[][2][3]={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<3;k++){
printf("x[%d,%d,%d]=%d\n",i,j,k,x[i][j][k]);
}
return 0;
}
Insertion Sort
while some elements unsorted:
Using linear search, find the location in the sorted portion
where the 1st element of the unsorted portion should be
inserted
Move all the elements after the insertion location up one
position to make space for the new element
45
38 45
60 60
66 66
45 79 47 13 74 36 21 94 22 57 16 29 81
7 20 10 30 40 50 60 80 100
[0] [1] [2] [4] [5] [6] [7]
[3] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <=
data[pivot]
2. While++too_big_index
data[too_small_index] > data[pivot]
--too_small_index
3. If too_big_index < too_small_index
swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap data[too_small_index] and data[pivot_index]
pivot_index = 0 2 4 10 12 13 50 57 63 100
[0] [1] [2] [3] [4] [5] [6] [7]
[8]
<= data[pivot]
> data[pivot]