1) Display Array
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
187
249
2) Find index of element
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int t = 4;
int r, c;
int found = 0;
for (r = 0; r < 2; r++) {
for (c = 0; c < 3; c++) {
if (arr[r][c] == t) {
found = 1;
break;
}
}
if (found) {
break;
}
}
if (found) {
printf("Element %d found at index [%d][%d]\n", t, r, c);
} else {
printf("Element not found in the array.\n");
}
return 0;
}
Output:
Element 4 found at index [1][1]
3) Search no:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int t = 4;
int found = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == t) {
found = 1;
break;
}
}
if (found) {
break;
}
}
if (found) {
printf("Number %d found in the array.", t);
} else {
printf("Number %d not found in the array.", t);
}
return 0;
}
Output:
Number 4 found in the array.
4) Descending order
#include <stdio.h>
int main() {
int rows = 2;
int cols = 3;
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int temp;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < rows; k++) {
for (int l = 0; l < cols; l++) {
if (arr[i][j] > arr[k][l]) {
temp = arr[i][j];
arr[i][j] = arr[k][l];
arr[k][l] = temp;
}
}
}
}
}
printf("Array in descending order:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
Array in descending order:
987
421
5) Insert element
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int row = 1, col = 1, element = 6;
arr[row][col] = element;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
187
269
6) Delete element
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
arr[0][1] = 0;
arr[1][2] = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output
107
240
7) Largest no:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int max = arr[0][0];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
}
printf("The largest number in the 2D array is: %d", max);
return 0;
}
Output:
The largest number in the 2D array is: 9
8) Sum of Array:
#include <stdio.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int sum = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
}
}
printf("Sum of the 2D array: %d\n", sum);
return 0;
}
Output:
Sum of the 2D array: 31
9) Median, Mode, Mean
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int total_elements = 2 * 3;
int sum = 0;
int mode = 0;
int max_count = 0;
// Calculate sum
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
}
}
// Calculate mean
float mean = (float)sum / total_elements;
// Calculate median
int temp[total_elements];
int k = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
temp[k++] = arr[i][j];
}
}
// Sort the array
for (int i = 0; i < total_elements - 1; i++) {
for (int j = 0; j < total_elements - i - 1; j++) {
if (temp[j] > temp[j + 1]) {
int swap = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = swap;
}
}
}
float median;
if (total_elements % 2 == 0) {
median = (temp[total_elements / 2 - 1] + temp[total_elements / 2]) / 2.0;
} else {
median = temp[total_elements / 2];
}
// Calculate mode
for (int i = 0; i < total_elements; i++) {
int count = 0;
for (int j = 0; j < total_elements; j++) {
if (temp[j] == temp[i]) {
count++;
}
}
if (count > max_count) {
max_count = count;
mode = temp[i];
}
}
printf("Mean: %.2f\n", mean);
printf("Median: %.2f\n", median);
printf("Mode: %d\n", mode);
return 0;
}
Output:
Mean: 5.17
Median: 5.50
Mode: 1
10) Standard Deviation and Variance
#include <stdio.h>
#include <math.h>
int main() {
int arr[2][3] = {{1, 8, 7}, {2, 4, 9}};
int sum = 0, sum_sq = 0;
double mean, variance, std_deviation;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
sum_sq += arr[i][j] * arr[i][j];
}
}
mean = (double)sum / 6;
variance = ((double)sum_sq / 6) - (mean * mean);
std_deviation = sqrt(variance);
printf("Mean: %.2f\n", mean);
printf("Variance: %.2f\n", variance);
printf("Standard Deviation: %.2f\n", std_deviation);
return 0;
}
Output:
Mean: 5.17
Variance: 9.14
Standard Deviation: 3.02
11) Fibonacci Series:
#include <stdio.h>
int main() {
int fib[2][3] = {{1, 8, 7}, {2, 4, 9}};
for (int i = 2; i < 2; i++) {
for (int j = 3; j < 6; j++) {
fib[i][j] = fib[i - 1][j] + fib[i - 2][j];
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", fib[i][j]);
}
printf("\n");
}
return 0;
}
Output:
187
249