0% found this document useful (0 votes)
3 views9 pages

Lab 17

The document contains five C programs that perform various operations on 2-D arrays. These include calculating the sum of all elements, the sum of main diagonal elements, finding the largest and smallest elements, summing only prime values, and performing matrix addition. Each program includes functions to encapsulate the logic for the respective operations.

Uploaded by

rashushankar2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab 17

The document contains five C programs that perform various operations on 2-D arrays. These include calculating the sum of all elements, the sum of main diagonal elements, finding the largest and smallest elements, summing only prime values, and performing matrix addition. Each program includes functions to encapsulate the logic for the respective operations.

Uploaded by

rashushankar2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1)Write a C program to determine the sum of elements of a 2-D array using a function ELESUM.

#include <stdio.h>

int ELESUM(int arr[][3], int rows, int cols) {


int sum = 0;

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


for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}

return sum;
}

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][3];

printf("Enter the elements of the 2-D array:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

int sum = ELESUM(arr, rows, cols);

printf("The sum of the elements in the 2-D array is: %d\n", sum);

return 0;
}
2)Write a C program to determine the sum of main diagonal elements of a 2-D array of size 3x3
using a function SUMDIAGONAL.
#include <stdio.h>

int SUMDIAGONAL(int arr[3][3]) {


int sum = 0;

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


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

return sum;
}

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

printf("Enter the elements of the 3x3 array:\n");


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &arr[i][j]);
}
}

int diagonalSum = SUMDIAGONAL(arr);


printf("The sum of the main diagonal elements is: %d\n", diagonalSum);

return 0;
}

3)Write a C program to determine the largest and smallest element of a 2-D


array. Use functions LARGEST and SMALLEST for the given purpose.
#include <stdio.h>

int LARGEST(int arr[][3], int rows, int cols) {


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

return largest;
}

int SMALLEST(int arr[][3], int rows, int cols) {


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

return smallest;
}

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][3];

printf("Enter the elements of the 2-D array:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

int largest = LARGEST(arr, rows, cols);


int smallest = SMALLEST(arr, rows, cols);

printf("The largest element in the 2-D array is: %d\n", largest);


printf("The smallest element in the 2-D array is: %d\n", smallest);

return 0;
}
4)Write a C program to find the sum of only PRIME values in a 2-D array using a function
PRIMESUM.
#include <stdio.h>

int isPrime(int num) {


if (num <= 1) return 0;
if (num <= 3) return 1;

for (int i = 2; i * i <= num; i++) {


if (num % i == 0) {
return 0;
}
}

return 1;
}

int PRIMESUM(int arr[][3], int rows, int cols) {


int sum = 0;

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


for (int j = 0; j < cols; j++) {
if (isPrime(arr[i][j])) {
sum += arr[i][j];
}
}
}

return sum;
}

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][3];

printf("Enter the elements of the 2-D array:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

int primeSum = PRIMESUM(arr, rows, cols);

printf("The sum of prime values in the 2-D array is: %d\n", primeSum);

return 0;
}
5)Write a C program to perform addition of two matrices and display the result using 3 rd matrix.
#include <stdio.h>

int main() {
int rows, cols;

// Input the number of rows and columns for the matrices


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

// Declare the two matrices and the result matrix


int matrix1[rows][cols];
int matrix2[rows][cols];
int result[rows][cols];

// Input elements for the first matrix


printf("Enter elements for the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input elements for the second matrix


printf("Enter elements for the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Perform matrix addition and store the result in the third matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result matrix


printf("Resultant matrix after addition:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

You might also like