You are on page 1of 25

#include <stdio.

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]);
}

printf("The values stored in the array are:\n");


for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}

printf("\nThe values stored in the array in reverse are:\n");


for (int i = n - 1; i >= 0; i--) {
printf("%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];
}

printf("Sum of all elements stored in the array is: %d\n", sum);

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];
}

printf("The elements stored in the first array are:\n");


for (int i = 0; i < n; i++) {
printf("%d ", firstArray[i]);
}

printf("\nThe elements copied into the second array are:\n");


for (int i = 0; i < n; i++) {
printf("%d ", secondArray[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]);
}

printf("The unique elements found in the array are:\n");


for (int i = 0; i < n; i++) {
int isUnique = 1;
for (int j = 0; j < n; j++) {
if (i != j && array[i] ==array[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%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]);
}

printf("The unique elements found in the array are:\n");


for (int i = 0; i < n; i++) {
int isUnique = 1;
for (int j = 0; j < n; j++) {
if (i != j && array[i] == array[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%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;

for (int i = 0; i < n; i++) {


mergedArray[index] = firstArray[i];
index++;
}

for (int i = 0; i < n; i++) {


mergedArray[index] = secondArray[i];
index++;
}

// Bubble sort in descending order


for (int i = 0; i < 2 * n - 1; i++) {
for (int j = 0; j < 2 * n - i - 1; j++) {
if (mergedArray[j] < mergedArray[j + 1]) {
int temp = mergedArray[j];
mergedArray[j] = mergedArray[j + 1];
mergedArray[j + 1] = temp;
}
}
}

printf("The merged array in descending order is:\n");


for (int i = 0; i < 2 * n; i++) {
printf("%d ", mergedArray[i]);
}

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;

for (int i = 0; i < n; i++) {


int count = 1;
for (int j = i + 1; j < n; j++) {
if (array[i] == array[j]) {
count++;
freq[j] = visited;
}
}
if (freq[i] != visited) {
freq[i] = count;
}
}

printf("The frequency of all elements of an array:\n");


for (int i = 0; i < n; i++) {
if (freq[i] != visited) {
printf("%d occurs %d times\n", array[i], freq[i]);
}
}

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:

6. Program to print all unique elements in an array:

```c
#include <stdio.h>

void printUniqueElements(int array[], int size) {


printf("The unique elements found in the array are:\n");
for (int i = 0; i < size; i++) {
int isUnique = 1;
for (int j = 0; j < size; j++) {
if (i != j && array[i] == array[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%d ", array[i]);
}
}
printf("\n");
}

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>

void mergeArrays(int firstArray[], int secondArray[], int size) {


int mergedArray[2 * size];
int index = 0;

for (int i = 0; i < size; i++) {


mergedArray[index] = firstArray[i];
index++;
}

for (int i = 0; i < size; i++) {


mergedArray[index] = secondArray[i];
index++;
}

// Sort merged array in descending order using bubble sort


for (int i = 0; i < 2 * size - 1; i++) {
for (int j = 0; j < 2 * size - i - 1; j++) {
if (mergedArray[j] < mergedArray[j + 1]) {
int temp = mergedArray[j];
mergedArray[j] = mergedArray[j + 1];
mergedArray[j + 1] = temp;
}
}
}

printf("The merged array in descending order is:\n");


for (int i = 0; i < 2 * size; i++) {
printf("%d ", mergedArray[i]);
}
printf("\n");
}

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]);
}

mergeArrays(firstArray, secondArray, size);

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]);
}

int even[size], odd[size];


int evenCount = 0, oddCount = 0;

for (int i = 0; i < size; i++) {


if (arr[i] % 2 == 0) {
even[evenCount] = arr[i];
evenCount++;
} else {
odd[oddCount] = arr[i];
oddCount++;
}
}

printf("The Even elements are:\n");


for (int i = 0; i < evenCount; i++) {
printf("%d ", even[i]);
}

printf("\nThe Odd elements are:\n");


for (int i = 0; i < oddCount; i++) {
printf("%d ", odd[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]);
}

// Sort the array in ascending order


for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("Elements of array in sorted ascending order:\n");


for (int i = 0; i < size; i++) {
printf("%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]);
}

// Sort the array in descending order


for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-i-1; j++) {
if (arr[j] < arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("Elements of the array in sorted descending order:\n");


for (int i = 0; i < size; i++) {
printf("%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 arr[size + 1];


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 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("The existing array list is:\n");


for (int i = 0; i```
; i < size + 1; i++) {
printf("%d ", arr[i]);
}

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);

int arr[size + 1];


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 value, position;


printf("Input the value to be inserted: ");
scanf("%d", &value);

printf("Input the position, where the value to be inserted: ");


scanf("%d", &position);

for (int i = size; i >= position; i--) {


arr[i] = arr[i-1];
}

arr[position-1] = value;

printf("The current list of the array:\n");


for (int i = 0; i < size + 1; i++) {
printf("%d ", arr[i]);
}

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);

for (int i = position - 1; i < size - 1; i++) {


arr[i] = arr[i+1];
}

printf("The new list is:");


for (int i = 0; i < size - 1; i++) {
printf(" %d", arr[i]);
}

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]);
}

int largest = arr[0];


int secondLargest = arr[0];

for (int i = 0; i < size; i++) {


if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] < largest) {
secondLargest = arr[i];
}
}

printf("The Second largest element in the array is: %d\n", secondLargest);

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]);
}

int smallest = 9999;


int secondSmallest = 9999;

for (int i = 0; i < size; i++) {


if (arr[i] < smallest) {
secondSmallest = smallest;
smallest = arr[i];
} else if (arr[i] < secondSmallest && arr[i] > smallest) {
secondSmallest = arr[i];
}
}

printf("The Second smallest element in the array is: %d\n", secondSmallest);

return 0;
}
Program 18: Print a 3x3 Matrix
c
Copy
#include <stdio.h>

int main() {
int matrix[3][3];

printf("Input elements in the matrix:\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix[i][j]);
}
}

printf("The matrix is:\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

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);

int matrix1[size][size], matrix2[size][size], sum[size][size];

printf("Input elements in the first matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

printf("Input elements in the second matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
// Add two matrices
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

printf("The First matrix is:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix1[i][j]);
}
printf("\n");
}

printf("The Second matrix is:\n");


```c
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix2[i][j]);
}
printf("\n");
}

printf("The Addition of two matrices is:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

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);

int matrix1[size][size], matrix2[size][size], difference[size][size];

printf("Input elements in the first matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

printf("Input elements in the second matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix2[i][j]);
}
}

// Subtract two matrices


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
difference[i][j] = matrix1[i][j] - matrix2[i][j];
}
}

printf("The First matrix is:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix1[i][j]);
}
printf("\n");
}

printf("The Second matrix is:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix2[i][j]);
}
printf("\n");
}

printf("The Subtraction of two matrices is:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", difference[i][j]);
}
printf("\n");
}

return 0;
}

Program 21: Multiplication of Two Square Matrices


c
Copy
#include <stdio.h>

#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;

for (i = 0; i < rows1; i++) {


for (j = 0; j < cols2; j++) {
result[i][j] = 0;
for (k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

void printMatrix(int mat[][MAX_SIZE], int rows, int cols) {


int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE]
[MAX_SIZE];
int rows1, cols1, rows2, cols2;

printf("Input the rows and columns of first matrix: ");


scanf("%d %d", &rows1, &cols1);
printf("Input the rows and columns of second matrix: ");
scanf("%d %d", &rows2, &cols2);

if (cols1 != rows2) {
printf("Matrix multiplication not possible. Invalid dimensions.\n");
return 0;
}

printf("Input elements in the first matrix:\n");


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}

printf("Input elements in the second matrix:\n");


for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
}
multiplyMatrices(mat1, mat2, result, rows1, cols1, cols2);

printf("The First matrix is:\n");


printMatrix(mat1, rows1, cols1);

printf("The Second matrix is:\n");


printMatrix(mat2, rows2, cols2);

printf("The multiplication of two matrices is:\n");


printMatrix(result, rows1, cols2);

return 0;
}
Program 22: Transpose of a Matrix
c
Copy
#include <stdio.h>

#define MAX_SIZE 10

void transposeMatrix(int mat[][MAX_SIZE], int transposed[][MAX_SIZE], int rows, int


cols) {
int i, j;

for (i = 0; i < rows; i++) {


for (j = 0; j < cols; j++) {
transposed[j][i] = mat[i][j];
}
}
}

void printMatrix(int mat[][MAX_SIZE], int rows, int cols) {


int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat[MAX_SIZE][MAX_SIZE], transposed[MAX_SIZE][MAX_SIZE];
int rows, cols;

printf("Input the rows and columns of the matrix: ");


scanf("%d %d", &rows, &cols);

printf("Input elements in the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}

printf("The matrix is:\n");


printMatrix(mat, rows, cols);

transposeMatrix(mat, transposed, rows, cols);

printf("The transpose of a matrix is:\n");


printMatrix(transposed, cols, rows);

return 0;
}
Program 23: Sum of Right Diagonal Elements of a Matrix
c
Copy
#include <stdio.h>

#define MAX_SIZE 10

int calculateRightDiagonalSum(int mat[][MAX_SIZE],int size) {


int i, sum = 0;
for (i = 0; i < size; i++) {
sum += mat[i][i];
}
return sum;
}

void printMatrix(int mat[][MAX_SIZE], int size) {


int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat[MAX_SIZE][MAX_SIZE];
int size, i, j;

printf("Input the size of the square matrix: ");


scanf("%d", &size);

printf("Input elements in the matrix:\n");


for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}
printf("The matrix is:\n");
printMatrix(mat, size);

int sum = calculateRightDiagonalSum(mat, size);


printf("Addition of the right diagonal elements is: %d\n", sum);

return 0;
}
Program 24: Sum of Left Diagonal Elements of a Matrix
c
Copy
#include <stdio.h>

#define MAX_SIZE 10

int calculateLeftDiagonalSum(int mat[][MAX_SIZE], int size) {


int i, sum = 0;
for (i = 0; i < size; i++) {
sum += mat[i][size - 1 - i];
}
return sum;
}

void printMatrix(int mat[][MAX_SIZE], int size) {


int i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat[MAX_SIZE][MAX_SIZE];
int size, i, j;

printf("Input the size of the square matrix: ");


scanf("%d", &size);

printf("Input elements in the matrix:\n");


for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}

printf("The matrix is:\n");


printMatrix(mat, size);
int sum = calculateLeftDiagonalSum(mat, sizeHere is the continuation of the
previous response:

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;

for (i = 0; i < rows; i++) {


rowSum[i] = 0;
for (j = 0; j < cols; j++) {
rowSum[i] += mat[i][j];
colSum[j] += mat[i][j];
}
}
}

void printMatrix(int mat[][MAX_SIZE], int rows, int cols) {


int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat[MAX_SIZE][MAX_SIZE];
int rowSum[MAX_SIZE] = {0};
int colSum[MAX_SIZE] = {0};
int rows, cols;

printf("Input the size of the square matrix: ");


scanf("%d", &rows);
cols = rows;

printf("Input elements in the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &mat[i][j]);
}
}

printf("The First matrix is:\n");


printMatrix(mat, rows, cols);

calculateRowColumnSum(mat, rows, cols, rowSum, colSum);

printf("The sum of rows and columns of the matrix is:\n");


for (int i = 0; i < rows; i

Task 27: Print Upper Triangular Matrix


c
Copy
#include <stdio.h>

#define MAX_SIZE 10

void printUpperTriangularMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {


printf("The matrix is:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

printf("\nSetting zero in upper triangular matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (j < i) {
matrix[i][j] = 0;
}
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int size, matrix[MAX_SIZE][MAX_SIZE];

printf("Input the size of the square matrix: ");


scanf("%d", &size);

printf("Input elements in the first matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix[i][j]);
}
}

printUpperTriangularMatrix(matrix, size);

return 0;
}
Task 28: Calculate Determinant of a 3x3 Matrix
c
Copy
#include <stdio.h>

int calculateDeterminant(int matrix[3][3]) {


int determinant =
matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);

return determinant;
}

int main() {
int matrix[3][3];

printf("Input elements in the first matrix:\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix[i][j]);
}
}

int determinant = calculateDeterminant(matrix);


printf("\nThe Determinant of the matrix is: %d\n", determinant);

return 0;
}
Task 29: Check if Matrix is Sparse
c
Copy
#include <stdio.h>

#define MAX_SIZE 10

int isSparseMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols) {


int zerosCount = 0;

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
zerosCount++;
}
}
}

if (zerosCount > (rows * cols) / 2) {


return 1;
} else {
return 0;
}
}

int main() {
int rows, cols, matrix[MAX_SIZE][MAX_SIZE];

printf("Input the number of rows of the matrix: ");


scanf("%d", &rows);
printf("Input the number of columns of the matrix: ");
scanf("%d", &cols);

printf("Input elements in the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix[i][j]);
}
}

if (isSparseMatrix(matrix, rows, cols)) {


printf("\nThe given matrix is a sparse matrix.\n");
} else {
printf("\nThe given matrix is not a sparse matrix.\n");
}

return 0;
}
Task 30: Check if Two Matrices are Equal
c
Copy
#include <stdio.h>

#define MAX_SIZE 10

int areMatricesEqual(int matrix1[MAX_SIZE][MAX_SIZE], int matrix2[MAX_SIZE]


[MAX_SIZE], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++){
if (matrix1[i][j] != matrix2[i][j]) {
return 0;
}
}
}

return 1;
}
int main() {
int rows, cols, matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];

printf("Input the number of rows of the matrices: ");


scanf("%d", &rows);
printf("Input the number of columns of the matrices: ");
scanf("%d", &cols);

printf("Input elements in the first matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

printf("Input elements in the second matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &matrix2[i][j]);
}
}

if (areMatricesEqual(matrix1, matrix2, rows, cols)) {


printf("\nThe given matrices are equal.\n");
} else {
printf("\nThe given matrices are not equal.\n");
}

return 0;
}

You might also like