Regular Batch
Batch-08 (C Programming)
Array & Its Implementation
An array is a collection of variables of the same data type stored in contiguous memory
locations and accessed using a single name with an index.
Declaration of Array
data_type array_name[size];
int marks[5];
float salary[10];
char name[20];
Example:
int a[5] = {10, 20, 30, 40, 50};
Index Value
a[0] 10
a[1] 20
a[2] 30
a[3] 40
a[4] 50
Input and Output in array
--------Input-----------
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
---------Output-------------
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
Two-Dimensional Array(2-D
D Array or Matrix):
2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of
rows and columns.
Complexity IT Job Care Page 1
Regular Batch-08 (C Programming)
Input and Output in 2D array
#include <stdio.h>
int main() {
int a[3][3];
printf("Enter matrix elements:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
printf("Matrix is:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
printf("\n");
return 0;
Three-Dimensional Array(3-D Array):
A 3-D Multidimensional array contains three dimensions, so it can be considered an array of
two-dimensional arrays.
Dimensions = layers × rows × columns
Complexity IT Job Care Page 2
Regular Batch-08 (C Programming)
Input and Output in 3D array
#include <stdio.h>
int main() {
int a[2][3][4];
printf("Enter elements:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 4; k++) {
scanf("%d", &a[i][j][k]);
printf("3D Array Output:\n");
for(int i = 0; i < 2; i++) {
printf("Layer %d:\n", i);
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 4; k++) {
printf("%d ", a[i][j][k]);
printf("\n");
return 0;
Complexity IT Job Care Page 3
Regular Batch-08 (C Programming)
Some C Programming From Array
Count even and odd numbers in #include <stdio.h>
array int main() {
int a[100], n;
int even = 0, odd = 0;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if(a[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even numbers = %d\n", even);
printf("Odd numbers = %d\n", odd);
return 0;
}
Sort array in ascending order #include <stdio.h>
int main() {
int a[100], n, temp;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1; j++) {
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted array (Ascending):\n");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
Complexity IT Job Care Page 4
Regular Batch-08 (C Programming)
Linear search in array #include <stdio.h>
int main() {
int a[100], n, key, found = 0;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
for(int i = 0; i < n; i++) {
if(a[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if(found == 0) {
printf("Element not found\n");
}
return 0;
}
Binary search in array #include <stdio.h>
int main() {
int a[100], n, key;
int low, high, mid, found = 0;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter sorted elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
Complexity IT Job Care Page 5
Regular Batch-08 (C Programming)
if(a[mid] == key) {
printf("Element found at index %d\n", mid);
found = 1;
break;
}
else if(key < a[mid]) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
if(found == 0) {
printf("Element not found\n");
}
return 0;
}
Find prime numbers in array #include <stdio.h>
int main() {
int a[100], n;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Prime numbers in array:\n");
for(int i = 0; i < n; i++) {
int num = a[i];
int isPrime = 1;
if(num <= 1)
isPrime = 0;
for(int j = 2; j <= num/2; j++) {
if(num % j == 0) {
isPrime = 0;
Complexity IT Job Care Page 6
Regular Batch-08 (C Programming)
break;
}
}
if(isPrime == 1) {
printf("%d ", num);
}
}
return 0;
}
Find largest difference in array #include <stdio.h>
int main() {
int a[100], n;
int max, min, difference;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
max = min = a[0];
for(int i = 1; i < n; i++) {
if(a[i] > max)
max = a[i];
if(a[i] < min)
min = a[i];
}
difference = max - min;
printf("Largest Difference = %d\n", difference);
return 0;
}
Remove duplicate elements #include <stdio.h>
int main() {
int a[100], n;
Complexity IT Job Care Page 7
Regular Batch-08 (C Programming)
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i] == a[j]) {
for(int k = j; k < n - 1; k++) {
a[k] = a[k + 1];
}
n--;
j--;
}
}
}
printf("Array after removing duplicates:\n");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
Addition of two matrices #include <stdio.h>
int main() {
int a[10][10], b[10][10], sum[10][10];
int row, col, i, j;
printf("Enter number of rows and columns: ");
scanf("%d %d", &row, &col);
printf("Enter elements of first matrix:\n");
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
scanf("%d", &a[i][j]);
}
Complexity IT Job Care Page 8
Regular Batch-08 (C Programming)
printf("Enter elements of second matrix:\n");
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of matrices:\n");
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Multiplication of two matrices #include <stdio.h>
int main() {
int a[10][10], b[10][10], mul[10][10];
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication is not possible");
return 0;
}
printf("Enter elements of first matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
Complexity IT Job Care Page 9
Regular Batch-08 (C Programming)
}
}
printf("Enter elements of second matrix:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
mul[i][j] = 0;
for(k = 0; k < c1; k++) {
mul[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Multiplication of matrices:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", mul[i][j]);
}
printf("\n");
}
return 0;
}
Find sum of upper triangle #include <stdio.h>
elements
int main() {
int a[10][10], row, col, i, j, sum = 0;
printf("Enter rows and columns: ");
scanf("%d %d", &row, &col);
printf("Enter matrix elements:\n");
for(i = 0; i < row; i++) {
for(j = 0; j < col; j++) {
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < row; i++) {
Complexity IT Job Care Page 10
Regular Batch-08 (C Programming)
for(j = 0; j < col; j++) {
if(i <= j) {
sum += a[i][j];
}
}
}
printf("Sum of upper triangle elements = %d", sum);
return 0;
}
Find pair with given sum #include <stdio.h>
int main() {
int a[100], n, sum, i, j;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter target sum: ");
scanf("%d", &sum);
printf("Pairs are:\n");
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(a[i] + a[j] == sum) {
printf("(%d, %d)\n", a[i], a[j]);
}
}
}
return 0;
}
Complexity IT Job Care Page 11
Regular Batch-08 (C Programming)
MCQ On Array
1. What is an array in C?
A. A collection of different data types
B. A collection of same data types
C. A function
D. A pointer
Answer: B
2. Array index in C starts from:
A. 1
B. -1
C. 0
D. 2
Answer: C
3. Which of the following correctly declares an array?
A. int arr[10];
B. array int[10];
C. int arr;[10]
D. arr int[10];
Answer: A
4. Size of int array int a[5] is (assuming 4 bytes int):
A. 5 bytes
B. 10 bytes
C. 20 bytes
D. 25 bytes
Answer: C
5. Which operation is NOT allowed on arrays in C?
A. Traversal
B. Insertion at end
C. Assignment of whole array using =
D. Accessing elements
Answer: C
6. Array elements are stored in:
Complexity IT Job Care Page 12
Regular Batch-08 (C Programming)
A. Random memory locations
B. Contiguous memory locations
C. Heap only
D. Stack only
Answer: B
7. What is the value of arr[0] called?
A. First element
B. Last element
C. Index
D. Pointer
Answer: A
8. int arr[10]; valid index range is:
A. 1 to 10
B. 0 to 10
C. 0 to 9
D. 1 to 9
Answer: C
9. Which header file is needed for arrays?
A. stdio.h
B. stdlib.h
C. No specific header needed
D. string.h
Answer: C
10. What will happen if we access arr[100] in int arr[10]?
A. Compilation error
B. Runtime error
C. Undefined behavior
D. Correct output
Answer: C
11. Which loop is commonly used for array traversal?
A. if
B. switch
C. for
D. goto
Complexity IT Job Care Page 13
Regular Batch-08 (C Programming)
Answer: C
12. Multi-dimensional arrays are used for:
A. Single values
B. Matrices and tables
C. Strings only
D. Pointers
Answer: B
13. int a[3] = {1,2}; remaining elements are:
A. Garbage
B. 0
C. 1
D. NULL
Answer: B
14. Which is correct for accessing 2D array element?
A. a[i,j]
B. a[i][j]
C. a(i)(j)
D. a{i}{j}
Answer: B
15. Array name represents:
A. First element value
B. Address of first element
C. Size of array
D. Last element
Answer: B
16. int a[5]; sizeof(a) gives:
A. 5
B. 10
C. 20 (if int = 4 bytes)
D. 1
Answer: C
17. Which search works only on sorted array?
Complexity IT Job Care Page 14
Regular Batch-08 (C Programming)
A. Linear search
B. Binary search
C. Sequential search
D. None
Answer: B
18. Time complexity of linear search is:
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C
19. Which of the following is correct initialization?
A. int a(5) = {1,2,3,4,5};
B. int a[5] = {1,2,3,4,5};
C. array a[5] = {1,2,3,4,5};
D. int a = {1,2,3,4,5};
Answer: B
20. Arrays in C are:
A. Dynamic
B. Fixed size
C. Infinite size
D. Resizable automatically
Answer: B
Written on Array
1. Remove Duplicates from Sorted Array
2. Rotate an array to the right or left by k steps. For example, rotating [1, 2, 3, 4] right by
1 step results in [4, 1, 2, 3]
3. Find the highest and lowest values in an unsorted array.
Complexity IT Job Care Page 15