Professional Documents
Culture Documents
Answers
Answers
h>
int main() {
int n;
printf("Input the number of elements to store in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d number of elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
return 0;
}
2. Program to find the sum of all elements of the array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
}
return 0;
}
3. Program to copy the elements of one array into another array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int firstArray[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &firstArray[i]);
}
int secondArray[n];
for (int i = 0; i < n; i++) {
secondArray[i] = firstArray[i];
}
return 0;
}
4. Program to count the total number of duplicate elements in an array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("Total number of duplicate elements found in the array is: %d\n", count);
return 0;
}
5. Program to print all unique elements in an array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Print all unique elements of an array:\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
return 0;
}
#include <stdio.h>
int main() {
int n;
printf("Print all unique elements of an array:\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
return 0;
}
7. Program to merge two arrays of the same size sorted in descending
order:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the first array: ");
scanf("%d", &n);
int firstArray[n];
printf("Input %d elements in the first array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &firstArray[i]);
}
int secondArray[n];
printf("Input %d elements in the second array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &secondArray[i]);
}
int mergedArray[2 * n];
int index = 0;
return 0;
}
8. Program to count the frequency of each element of an array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0; i < n; i++) {
printf("element - %d : ", i);
scanf("%d", &array[i]);
}
int freq[n];
int visited = -1;
return 0;
}
9. Program to find the maximum and minimum elements in an array:
c
Copy
#include <stdio.h>
int main() {
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
int array[n];
printf("Input %d elements in the array:\n", n);
for (int i = 0;Certainly! Here are the simplified versions of the programs you
mentioned:
```c
#include <stdio.h>
int main() {
int size;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &size);
int array[size];
printf("Input %d elements in the array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d: ", i);
scanf("%d", &array[i]);
}
printUniqueElements(array, size);
return 0;
}
7. Program to merge two arrays of the same size sorted in descending
order:
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the number of elements to be stored in the arrays: ");
scanf("%d", &size);
int firstArray[size];
printf("Input %d elements in the first array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d: ", i);
scanf("%d", &firstArray[i]);
}
int secondArray[size];
printf("Input %d elements in the second array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d: ", i);
scanf("%d", &secondArray[i]);
}
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
return 0;
}
Program 11: Sort Elements of an Array in Ascending Order
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
return 0;
}
Program 12: Sort Elements of an Array in Descending Order
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
return 0;
}
Program 13: Insert Values in a Sorted Array
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Insert New value in the sorted array:\n");
printf("-----------------------------------------\n");
printf("Input the size of array: ");
scanf("%d", &size);
int value;
printf("Input the value to be inserted: ");
scanf("%d", &value);
int i = size - 1;
while (i >= 0 && arr[i] > value) {
arr[i+1] = arr[i];
i--;
}
arr[i+1] = value;
printf("\n--------------------------------\n");
printf("Process exited after 39.33 seconds with return value 10\n");
return 0;
}
Program 14: Insert Values in an Unsorted Array
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
arr[position-1] = value;
return 0;
}
Program 15: Delete an Element at a Desired Position from an Array
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array in ascending order:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
int position;
printf("Input the position where to delete: ");
scanf("%d", &position);
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array:\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
return 0;
}
Program 17: Find the Second Smallest Element in an Array
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of array: ");
scanf("%d", &size);
int arr[size];
printf("Input %d elements in the array (value must be <9999):\n", size);
for (int i = 0; i < size; i++) {
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
return 0;
}
Program 18: Print a 3x3 Matrix
c
Copy
#include <stdio.h>
int main() {
int matrix[3][3];
return 0;
}
Program 19: Add Two Matrices of the Same Size
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &size);
return 0;
}
Program 20: Subtract Two Matrices
c
Copy
#include <stdio.h>
int main() {
int size;
printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &size);
return 0;
}
#define MAX_SIZE 10
void multiplyMatrices(int mat1[][MAX_SIZE], int mat2[][MAX_SIZE], int result[]
[MAX_SIZE], int rows1, int cols1, int cols2) {
int i, j, k;
int main() {
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE]
[MAX_SIZE];
int rows1, cols1, rows2, cols2;
if (cols1 != rows2) {
printf("Matrix multiplication not possible. Invalid dimensions.\n");
return 0;
}
return 0;
}
Program 22: Transpose of a Matrix
c
Copy
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int mat[MAX_SIZE][MAX_SIZE], transposed[MAX_SIZE][MAX_SIZE];
int rows, cols;
return 0;
}
Program 23: Sum of Right Diagonal Elements of a Matrix
c
Copy
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int mat[MAX_SIZE][MAX_SIZE];
int size, i, j;
return 0;
}
Program 24: Sum of Left Diagonal Elements of a Matrix
c
Copy
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int mat[MAX_SIZE][MAX_SIZE];
int size, i, j;
Copy
printf("Addition of the left diagonal elements is: %d\n", sum);
return 0;
}
ini
Copy
**Program 25: Sum of Rows and Columns of a Matrix**
```c
#include <stdio.h>
#define MAX_SIZE 10
void calculateRowColumnSum(int mat[][MAX_SIZE], int rows, int cols, int rowSum[], int
colSum[]) {
int i, j;
int main() {
int mat[MAX_SIZE][MAX_SIZE];
int rowSum[MAX_SIZE] = {0};
int colSum[MAX_SIZE] = {0};
int rows, cols;
#define MAX_SIZE 10
int main() {
int size, matrix[MAX_SIZE][MAX_SIZE];
printUpperTriangularMatrix(matrix, size);
return 0;
}
Task 28: Calculate Determinant of a 3x3 Matrix
c
Copy
#include <stdio.h>
return determinant;
}
int main() {
int matrix[3][3];
return 0;
}
Task 29: Check if Matrix is Sparse
c
Copy
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int rows, cols, matrix[MAX_SIZE][MAX_SIZE];
return 0;
}
Task 30: Check if Two Matrices are Equal
c
Copy
#include <stdio.h>
#define MAX_SIZE 10
return 1;
}
int main() {
int rows, cols, matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];
return 0;
}