0% found this document useful (0 votes)
11 views

quiz 3 code

Uploaded by

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

quiz 3 code

Uploaded by

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

1.

write a c program with no comments that allows to input number of matrix and the
size of matrix NxM where n is the number of rows
and m is the number of column
and solve for the sum of the matrix.
*note size of the matrix should be the same otherwise error in the program.
if size is not equal it wont proceed, error message to display
if size is equal, it will proceed

sample output like this:


Input Number of Matrix:
Size of matrix 1:
Size of matrix 2:
Elements of matrix 1:
Elements of matrix 2:

code
#include <stdio.h>

int main() {
int matrices, rows1, cols1, rows2, cols2, i, j, k;

printf("Input Number of Matrix: ");


scanf("%d", &matrices);

if (matrices != 2) {
printf("Error: Only 2 matrices are supported.\n");
return 1;
}

printf("Size of Matrix 1 (rows & columns): ");


scanf("%d %d", &rows1, &cols1);

printf("Size of Matrix 2 (rows & columns): ");


scanf("%d %d", &rows2, &cols2);

if (rows1 != rows2 || cols1 != cols2) {


printf("Error: Sizes of matrices do not match.\n");
return 1;
}

int matrix1[rows1][cols1], matrix2[rows2][cols2], sum[rows1][cols1];

printf("Elements of matrix 1:\n");


for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}

printf("Elements of matrix 2:\n");


for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

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


for (j = 0; j < cols1; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

printf("Sum of matrices:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}

2. write a c program with no comments that accept a line of text and display the
text inputted as well as the number of occurences
3.
4.
#include <stdio.h>

int main() {
int num_arrays, rows, cols;

printf("Input number of arrays: ");


scanf("%d", &num_arrays);

for (int k = 0; k < num_arrays; k++) {


printf("\nArray %d\n", k + 1);

printf("Input number of rows for Array %d: ", k + 1);


scanf("%d", &rows);
printf("Input number of columns for Array %d: ", k + 1);
scanf("%d", &cols);

int array[rows][cols];

printf("Input elements of Array %d:\n", k + 1);


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

printf("Array %d:\n", k + 1);


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

return 0;
}

You might also like