Solving 50 Array Based Programming Questions with Answers for Grade 11, and 12 | Computer | C Programming
Rohan Koirala - Fri Mar 22 2024
0
The difficulty level is in ascending order.
Write a program to find the sum of all elements in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5}; int sum = 0; for (int i = 0; i 5; i++) { sum += arr[i]; } printf("The sum of all elements in the array is %d\n", sum); return 0; }
Write a program to find the average of numbers in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); float sum = 0; for (int i = 0; i n; i++) { sum += arr[i]; } float average = sum / n; printf("The average of the numbers in the array is: %f", average); return 0; }
Write a program to find the product of numbers in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int product = 1; for (int i = 0; i n; i++) { product *= arr[i]; } printf("Product of numbers in the array: %d", product); return 0; }
Write a program to find the largest number in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int max = arr[0]; for (int i = 1; i n; i++) { if (arr[i] max) { max = arr[i]; } } printf("The largest number in the array is %d\n", max); return 0; }
Write a program to find the smallest number in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5}; int min = arr[0]; for (int i = 1; i 5; i++) { if (arr[i] min) { min = arr[i]; } } printf("The smallest number in the array is %d\n", min); return 0; }
Write a program to sort numbers in a 1D array in ascending order.
#include stdio.h int main() { int n, i, j, temp; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter the elements: "); for (i = 0; i n; i++) { scanf("%d", &arr[i]); } for (i = 0; i n - 1; i++) { for (j = 0; j n - i - 1; j++) { if (arr[j] arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("Sorted array: "); for (i = 0; i n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
Write a program to sort numbers in a 1D array in descending order.
#include stdio.h #include stdlib.h void sort_descending(int *arr, int size) { int i, j, temp; for (i = 0; i size - 1; i++) { for (j = i + 1; j size; j++) { if (arr[i] arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } int main() { int arr[] = {10, 5, 20, 15, 25, 30, 7, 12}; int size = sizeof(arr) / sizeof(arr[0]); printf("Unsorted array: "); for (int i = 0; i size; i++) { printf("%d ", arr[i]); } sort_descending(arr, size); printf("\nSorted array in descending order: "); for (int i = 0; i size; i++) { printf("%d ", arr[i]); } return 0; }
Write a program to reverse numbers in a 1D array.
#include stdio.h #include stdlib.h int main() { int n, i, j, temp; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i n; i++) { scanf("%d", &arr[i]); } printf("The original array is: "); for (i = 0; i n; i++) { printf("%d ", arr[i]); } for (i = 0, j = n - 1; i j; i++, j--) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } printf("\nThe reversed array is: "); for (i = 0; i n; i++) { printf("%d ", arr[i]); } return 0; }
Write a program to remove duplicates from a 1D array.
#include stdio.h #include stdlib.h int main() { int *arr, n, i, j, k; printf("Enter the size of the array: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); printf("Enter the elements of the array: "); 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("The array after removing duplicates: "); for (i = 0; i n; i++) { printf("%d ", arr[i]); } return 0; }
Write a program to find the frequency of a given element in a 1D array.
#include stdio.h int main() { int arr[] = {1, 2, 3, 4, 5, 1, 2, 3}; int n = sizeof(arr) / sizeof(arr[0]); int x = 3; int count = 0; for (int i = 0; i n; i++) { if (arr[i] == x) { count++; } } printf("The frequency of %d in the array is %d.\n", x, count); return 0; }
Write a program to check if two 1D arrays are equal.
#include stdio.h #include stdlib.h int main() { int n; printf("Enter the size of the arrays: "); scanf("%d", &n); int arr1[n], arr2[n]; printf("Enter the elements of the first array: "); for (int i = 0; i n; i++) { scanf("%d", &arr1[i]); } printf("Enter the elements of the second array: "); for (int i = 0; i n; i++) { scanf("%d", &arr2[i]); } int flag = 1; for (int i = 0; i n; i++) { if (arr1[i] != arr2[i]) { flag = 0; break; } } if (flag) { printf("The two arrays are equal.\n"); } else { printf("The two arrays are not equal.\n"); } return 0; }
Write a program to find the sum of numbers in a 2D array.
#include stdio.h int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int sum = 0; for (int i = 0; i 3; i++) { for (int j = 0; j 3; j++) { sum += arr[i][j]; } } printf("The sum of the numbers in the 2D array is: %d\n", sum); return 0; }
Write a program to find the average of numbers in a 2D array.
#include stdio.h int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int i, j, sum = 0; int rows = sizeof(arr) / sizeof(arr[0]); int cols = sizeof(arr[0]) / sizeof(arr[0][0]); for (i = 0; i rows; i++) { for (j = 0; j cols; j++) { sum += arr[i][j]; } } float average = (float)sum / (rows * cols); printf("The average of the numbers in the 2D array is: %.2f\n", average); return 0; }
Write a program to find the product of numbers in a 2D array.
#include stdio.h int main() { int rows, cols; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &cols); int arr[rows][cols]; printf("Enter the elements of the array:\n"); for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { scanf("%d", &arr[i][j]); } } int product = 1; for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { product *= arr[i][j]; } } printf("The product of all numbers in the array is: %d\n", product); return 0; }
Write a program to find the largest number in a 2D array.
Here is a C program to find the largest number in a 2D array: #include stdio.h int main() { int rows, cols; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &cols); int arr[rows][cols]; printf("Enter the elements of the array:\n"); for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { scanf("%d", &arr[i][j]); } } int largest = arr[0][0]; for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { if (arr[i][j] largest) { largest = arr[i][j]; } } } printf("The largest number in the array is %d\n", largest); return 0; }
Write a program to find the smallest number in a 2D array.
Here is a C program to find the smallest number in a 2D array: #include stdio.h #include stdlib.h int main() { int rows, cols; printf("Enter the number of rows and columns: "); scanf("%d %d", &rows, &cols); int **arr = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i rows; i++) { arr[i] = (int *)malloc(cols * sizeof(int)); } printf("Enter the elements of the array:\n"); for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { scanf("%d", &arr[i][j]); } } int smallest = arr[0][0]; for (int i = 0; i rows; i++) { for (int j = 0; j cols; j++) { if (arr[i][j] smallest) { smallest = arr[i][j]; } } } printf("The smallest number in the array is: %d\n", smallest); return 0; }
Write a program to multiply two matrices (2D arrays) and display the result.
#include stdio.h #define ROWS 2 #define COLS 3 int main() { int matrix1[ROWS][COLS] = { {1, 2, 3}, {4, 5, 6} }; int matrix2[ROWS][COLS] = { {7, 8, 9}, {10, 11, 12} }; int resultMatrix[ROWS][COLS]; // Multiply the two matrices for (int i = 0; i ROWS; i++) { for (int j = 0; j COLS; j++) { resultMatrix[i][j] = 0; for (int k = 0; k COLS; k++) { resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j]; } } } // Display the result matrix for (int i = 0; i ROWS; i++) { for (int j = 0; j COLS; j++) { printf("%d ", resultMatrix[i][j]); } printf("\n"); } return 0; }
Write a program to find the sum of numbers in a 3D array.
#include stdio.h int main() { int arr[3][4][5]; int sum = 0; // Initialize the array with values for (int i = 0; i 3; i++) { for (int j = 0; j 4; j++) { for (int k = 0; k 5; k++) { arr[i][j][k] = i + j + k; } } } // Calculate the sum of all elements in the 3D array for (int i = 0; i 3; i++) { for (int j = 0; j 4; j++) { for (int k = 0; k 5; k++) { sum += arr[i][j][k]; } } } printf("The sum of all elements in the 3D array is: %d\n", sum); return 0; }
Write a program to find the average of numbers in a 3D array.
#include stdio.h int main() { int arr[3][3][3]; int sum = 0; int count = 0; // Get the input for (int i = 0; i 3; i++) { for (int j = 0; j 3; j++) { for (int k = 0; k 3; k++) { scanf("%d", &arr[i][j][k]); } } } // Calculate the sum and count of numbers for (int i = 0; i 3; i++) { for (int j = 0; j 3; j++) { for (int k = 0; k 3; k++) { sum += arr[i][j][k]; count++; } } } // Calculate the average double average = (double)sum / count; // Print the average printf("The average of the numbers in the 3D array is: %.2f\n", average); return 0; }
Write a program to find the product of numbers in a 3D array.
#include stdio.h int main() { int arr[3][3][3], i, j, k, prod = 1; printf("Enter elements in 3D array:\n"); for (i = 0; i 3; i++) { for (j = 0; j 3; j++) { for (k = 0; k 3; k++) { scanf("%d", &arr[i][j][k]); } } } for (i = 0; i 3; i++) { for (j = 0; j 3; j++) { for (k = 0; k 3; k++) { prod *= arr[i][j][k]; } } } printf("Product of all elements in 3D array: %d\n", prod); return 0; }
Write a program to find the largest number in a 3D array.
#include stdio.h #include stdlib.h int main() { int n1, n2, n3, i, j, k, max; int ***arr; printf("Enter the dimensions of the 3D array (n1 x n2 x n3): "); scanf("%d %d %d", &n1, &n2, &n3); arr = (int ***)malloc(n1 * sizeof(int **)); for (i = 0; i n1; i++) { arr[i] = (int **)malloc(n2 * sizeof(int *)); for (j = 0; j n2; j++) { arr[i][j] = (int *)malloc(n3 * sizeof(int)); } } printf("Enter the elements of the 3D array:\n"); for (i = 0; i n1; i++) { for (j = 0; j n2; j++) { for (k = 0; k n3; k++) { scanf("%d", &arr[i][j][k]); } } } max = arr[0][0][0]; for (i = 0; i n1; i++) { for (j = 0; j n2; j++) { for (k = 0; k n3; k++) { if (arr[i][j][k] max) { max = arr[i][j][k]; } } } } printf("The largest number in the 3D array is: %d\n", max); return 0; }
Write a program to find the smallest number in a 3D array.
#include stdio.h int main() { int arr[3][3][3] = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }, { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} }, { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } }; int smallest = arr[0][0][0]; for (int i = 0; i 3; i++) { for (int j = 0; j 3; j++) { for (int k = 0; k 3; k++) { if (arr[i][j][k] smallest) { smallest = arr[i][j][k]; } } } } printf("The smallest number in the 3D array is %d.\n", smallest); return 0; }
Write a program to multiply two 3D arrays and display the result.
#include stdio.h int main() { int a[3][3][3], b[3][3][3], c[3][3][3]; int i, j, k; printf("Enter elements of first 3D array:\n"); for (i = 0; i 3; i++) { for (j = 0; j 3; j++) { for (k = 0; k 3; k++) { scanf("%d", &a[i][j][k]); } } } printf("Enter elements of second 3D array:\n"); for (i = 0; i 3; i++) { for (j = 0; j 3; j++) { for (k = 0; k 3; k++) { scanf("%d", &b[i][j][k]); } } } printf("Multiplication of two 3D arrays:\n"); for (i = 0; i 3; i++) { for (j = 0; j 3; j++) { for (k = 0; k 3; k++) { c[i][j][k] = a[i][j][k] * b[i][j][k]; printf("%d ", c[i][j][k]); } printf("\n"); } printf("\n"); } return 0; }
Write a program to transpose a matrix (2D array).
#include stdio.h #include stdlib.h int main() { int m, n, i, j; int **mat, **transpose; printf("Enter the number of rows and columns: "); scanf("%d %d", &m, &n); mat = (int **)malloc(m * sizeof(int *)); transpose = (int **)malloc(n * sizeof(int *)); for (i = 0; i m; i++) { mat[i] = (int *)malloc(n * sizeof(int)); } for (j = 0; j n; j++) { transpose[j] = (int *)malloc(m * sizeof(int)); } printf("Enter the elements of the matrix:\n"); for (i = 0; i m; i++) { for (j = 0; j n; j++) { scanf("%d", &mat[i][j]); } } for (i = 0; i m; i++) { for (j = 0; j n; j++) { transpose[j][i] = mat[i][j]; } } printf("Transpose of the matrix:\n"); for (i = 0; i n; i++) { for (j = 0; j m; j++) { printf("%d ", transpose[i][j]); } printf("\n"); } return 0; }
Write a program to find the sum of the diagonal elements of a square matrix (2D array).
#include stdio.h int main() { int n, sum = 0; printf("Enter the size of the square matrix: "); scanf("%d", &n); int matrix[n][n]; printf("Enter the elements of the matrix:\n"); for (int i = 0; i n; i++) { for (int j = 0; j n; j++) { scanf("%d", &matrix[i][j]); } } // Calculate the sum of the diagonal elements for (int i = 0; i n; i++) { sum += matrix[i][i]; } printf("The sum of the diagonal elements is: %d\n", sum); return 0; }