Triton SS & College
Seti Opi Marga Koteshwor-32, Kathmandu
Lab Report Of Computer Science on C
Programming
Lab Report No: 05
Submitted By:
Submitted to:
Name: Rohit Mahato Computer Science
Department
Roll:32
Triton SS and College
Class11
Sec:503(PHY)
Lab-05: [1D and 2D Array]
Introduction to 1D and 2D Arrays in C
In the C programming language, arrays are used to store multiple values
of the same data type in a single variable. They are particularly useful
when dealing with large datasets and can be thought of as collections of
variables. Arrays in C can be one-dimensional (1D) or two-dimensional
(2D).
Objectives:
1. Understand the Concept of Arrays: Learn what arrays are and
how they work in C.
2. Declare and Initialize Arrays: Learn how to declare and initialize
both 1D and 2D arrays.
3. Access and Manipulate Array Elements: Understand how to
access and modify array elements.
4. Implement Array Operations: Implement various operations such
as traversal, insertion, and deletion on arrays.
Theory:
1D Arrays:
Definition: A one-dimensional array is a list of variables of the
same type stored in contiguous memory locations.
Declaration: A 1D array is declared as follows:
dataType arrayName[arraySize];
For example:
int numbers[10];
This declares an array named numbers that can hold 10 integers.
Initialization: You can initialize an array when you declare it:
int numbers[5] = {1, 2, 3, 4, 5};
Accessing Elements: Array elements are accessed using the array
index. The index of the first element is 0:
int firstNumber = numbers[0];
2D Arrays:
Definition: A two-dimensional array is an array of arrays. It can be
visualized as a matrix with rows and columns.
Declaration: A 2D array is declared as follows
dataType arrayName[rows][columns];
For example:
int matrix[3][4];
This declares a 2D array named matrix with 3 rows and 4 columns.
Initialization: You can initialize a 2D array when you declare it:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Accessing Elements: Elements in a 2D array are accessed using
two indices, one for the row and one for the column:
int element = matrix[1][2]; // Accesses the element at second
row, third
1.Write a program to input 10 integers into an array and display them.
#include <stdio.h>
int main() {
int arr[10];
int i;
// Input 10 integers
printf("Enter 10 integers:\n");
for(i = 0; i < 10; i++) {
while (scanf("%d", &arr[i]) != 1) {
printf("Invalid input. Please enter an integer:\n");
while (getchar() != '\n'); // clear the invalid input
// Display the integers
printf("The integers you entered are:\n");
for(i = 0; i < 10; i++) {
printf("%d ", arr[i]);
return 0;
}
2.Write a program to find the sum of all elements in an array of size n.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
for(i = 0; i < n; i++) {
sum += arr[i];
printf("The sum of all elements in the array is: %d\n", sum);
return 0;
}
3.Write a program to find the largest element in an array of size n.
#include <stdio.h>
int main() {
int n, i, max;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
max = arr[0]; // Assume the first element is the largest initially
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
printf("The largest element in the array is: %d\n", max);
return 0;
}
4.Write a program to find the smallest element in an array of size n.
#include <stdio.h>
int main() {
int n, i, min;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
min = arr[0]; // Assume the first element is the smallest initially
for(i = 1; i < n; i++) {
if(arr[i] < min) {
min = arr[i];
printf("The smallest element in the array is: %d\n", min);
return 0;
}
5.Write a program to count the number of even and odd numbers in an
array.
#include <stdio.h>
int main() { int n, i, even_count = 0, odd_count = 0;printf("Enter the
size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n; i++) {
if(arr[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
printf("The number of even numbers in the array is: %d\n",
even_count);
printf("The number of odd numbers in the array is: %d\n",
odd_count);
return 0 ;
}
6.Write a program to reverse an array of size n and display the reversed
array.
#include <stdio.h>
int main() {
int n, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("The reversed array is:\n");
for(i = n-1; i >= 0; i--) {
printf("%d ", arr[i]);
return 0;
}
7.Write a program to copy the elements of one array into another array.
#include <stdio.h>
int main() {
int n, i;
printf("Enter the size of the arrays: ");
scanf("%d", &n);
int source[n], destination[n];
// Input elements into the source array
printf("Enter %d integers for the source array:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &source[i]);
// Copy elements from source array to destination array
for(i = 0; i < n; i++) {
destination[i] = source[i];
// Display the elements of the destination array
printf("The elements of the destination array are:\n");
for(i = 0; i < n; i++) {
printf("%d ", destination[i]);
return 0;
}
8.Write a program to delete an element from a specific position in an
array.
#include <stdio.h>
int main() {
int n, i, pos;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]); }
printf("Enter the position of the element to delete (0-based index): ");
scanf("%d", &pos);
if(pos < 0 || pos >= n) {
printf("Invalid position!\n");
} else {
for(i = pos; i < n-1; i++) {
arr[i] = arr[i+1];
n--; // Decrease the size of the array
printf("The array after deletion is:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
9.Write a program to sort an array of integers in ascending order.
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i – 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1; }
arr[j + 1] = key; } }
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); }
printf("\n"); }
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]); }
printf("Unsorted array: \n");
printArray(arr, n);
insertionSort(arr, n);
printf("Sorted array in ascending order: \n");
printArray(arr, n);
return 0;
10.Write a program to sort an array of integers in descending order.
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Bubble sort algorithm to sort the array in descending order
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] < arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
printf("The sorted array in descending order is:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
}
11. Write a program to find the second largest element in an array.
#include <stdio.h>
int main() {
int n, i, target, index = -1;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Enter the element to find: ");
scanf("%d", &target);
for(i = 0; i < n; i++) {
if(arr[i] == target) {
index = i;
break;
if(index != -1) {
printf("The element %d is found at index %d.\n", target, index);
} else {
printf("The element %d is not found in the array.\n", target);
return 0;
12. Write a program to find the second largest element in an array.
#include <stdio.h>
int main() {
int n, i, largest, second_largest;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]); }
if(n < 2) {
printf("Array should have at least two elements\n");
return 1; }
largest = second_largest = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > largest) {
second_largest = largest;
largest = arr[i];
} else if(arr[i] > second_largest && arr[i] != largest) {
second_largest = arr[i];
} }
printf("The second largest element is: %d\n", second_largest);
return 0;
13. Write a program to remove duplicate elements from an array.
#include <stdio.h>
int main() {
int n, i, j, k;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
for(k = j; k < n - 1; k++) {
arr[k] = arr[k + 1];
}
n--;
j--;
printf("Array after removing duplicates:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
14. Write a program to input and display a 2D array of size m x n.
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
printf("The 2D array is:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("%d ", arr[i][j]);
printf("\n");
return 0;
}
15. Write a program to find the sum of all elements in a 2D array of size m
x n.
#include <stdio.h>
int main() {
int m, n, i, j, sum = 0;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
sum += arr[i][j];
printf("The sum of all elements in the array is: %d\n", sum);
return 0;
}
16. Write a program to find the largest element in a 2D array of size m x
n.
#include <stdio.h>
int main() {
int m, n, i, j, max;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
max = arr[0][0];
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
if(arr[i][j] > max) {
max = arr[i][j];
printf("The largest element in the array is: %d\n", max);
return 0;
17. Write a program to find the smallest element in a 2D array of size m x
n.
#include <stdio.h>
int main() {
int m, n, i, j, min;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
min = arr[0][0];
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
if(arr[i][j] < min) {
min = arr[i][j];
printf("The smallest element in the array is: %d\n", min);
return 0;
}
18. Write a program to count the number of even and odd numbers in a
2D array.
#include <stdio.h>
int main() { int m, n, i, j, even_count = 0, odd_count = 0;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
if(arr[i][j] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
}
printf("The number of even numbers in the array is: %d\n",
even_count);
printf("The number of odd numbers in the array is: %d\n",
odd_count);
return 0;
}
19. Write a program to find the sum of elements in each row of a 2D
array.
#include <stdio.h>
int main() {
int m, n, i, j, sum;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
for(i = 0; i < m; i++) {
sum = 0;
for(j = 0; j < n; j++) {
sum += arr[i][j];
printf("The sum of elements in row %d is: %d\n", i, sum);
return 0;
}
20. Write a program to find the sum of elements in each column of a 2D
array.
#include <stdio.h>
int main() {
int m, n, i, j, sum;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
for(j = 0; j < n; j++) {
sum = 0;
for(i = 0; i < m; i++) {
sum += arr[i][j];
printf("The sum of elements in column %d is: %d\n", j, sum);
return 0;
21. Write a program to find the transpose of a 2D array (swap rows and
columns).
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int arr[m][n], transpose[n][m];
printf("Enter elements for the %d x %d array:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
transpose[j][i] = arr[i][j];
printf("The transpose of the array is:\n");
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
printf("%d ", transpose[i][j]);
printf("\n");
return 0;
}
22. Write a program to add two matrices.
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int matrix1[m][n], matrix2[m][n], sum[m][n];
printf("Enter elements for the first %d x %d matrix:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix1[i][j]); }
printf("Enter elements for the second %d x %d matrix:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix2[i][j]);
} for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j]; } }
printf("The sum of the two matrices is:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("%d ", sum[i][j]); }
printf("\n"); }
return 0;
23. Write a program to subtract two matrices.
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
int matrix1[m][n], matrix2[m][n], difference[m][n];
printf("Enter elements for the first %d x %d matrix:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix1[i][j]);
} }
printf("Enter elements for the second %d x %d matrix:\n", m, n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix2[i][j]); } }
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
difference[i][j] = matrix1[i][j] - matrix2[i][j];
} }
printf("The difference of the two matrices is:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
printf("%d ", difference[i][j]); }
printf("\n"); }
return 0;
}
24. Write a program to add multiply two matrices.
#include <stdio.h>
int main() {
int m1, n1, m2, n2, i, j, k;
printf("Enter the number of rows and columns of the first matrix (m1
n1): ");
scanf("%d %d", &m1, &n1);
printf("Enter the number of rows and columns of the second matrix (m2
n2): ");
scanf("%d %d", &m2, &n2);
if(n1 != m2) {
printf("Matrix multiplication is not possible. Number of columns of the
first matrix must be equal to the number of rows of the second matrix.\
n");
return 1;
int matrix1[m1][n1], matrix2[m2][n2], product[m1][n2];
printf("Enter elements for the first %d x %d matrix:\n", m1, n1);
for(i = 0; i < m1; i++) {
for(j = 0; j < n1; j++) {
scanf("%d", &matrix1[i][j]);
printf("Enter elements for the second %d x %d matrix:\n", m2, n2);
for(i = 0; i < m2; i++) {
for(j = 0; j < n2; j++) {
scanf("%d", &matrix2[i][j]);
// Initialize product matrix to 0
for(i = 0; i < m1; i++) {
for(j = 0; j < n2; j++) {
product[i][j] = 0;
// Multiply the matrices
for(i = 0; i < m1; i++) {
for(j = 0; j < n2; j++) {
for(k = 0; k < n1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
} }
printf("The product of the two matrices is:\n");
for(i = 0; i < m1; i++) {
for(j = 0; j < n2; j++) {
printf("%d ", product[i][j]); }
printf("\n"); }
return 0;
Conclusion
In summary, arrays are a vital data structure in C programming that allow
us to store and manage collections of data efficiently. We covered:
1. One-Dimensional Arrays (1D):
a. Suitable for storing a list of variables of the same type.
b. Useful for applications like lists, queues, and stacks.
2. Two-Dimensional Arrays (2D):
a. Ideal for representing matrices or tables.
b. Useful for applications like spreadsheets, image processing,
and game boards.
Understanding how to declare, initialize, access, and manipulate array
elements is essential for efficient data handling and algorithm
implementation in C programming. Arrays help organize data in a
structured manner and are foundational for more complex data
structures.