#Matrix Problem in C

2 messages · Page 1 of 1 (latest)

wicked turretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

vital lily
#

#include <stdio.h>

int main() {
int rows, columns;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);
float matrix[rows][columns];
printf("Enter the matrix elements (one by one):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
scanf("%f", &matrix[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
float current = matrix[i][j];
int count = 0;
float sum = 0;
for (int k = 0; k < columns; k++) {
if (matrix[i][k] == current) {
count++;
} else {
sum += matrix[i][k];
}
}
if (count > 1) {
float average = sum / (columns - count);
matrix[i][j] = average;
}
}
}
printf("Matrix elements with duplicates removed:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%.2f ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

Enter the number of rows: 3
Enter the number of columns: 3
Enter the matrix elements (one by one):
2
1
3
5
6
5
7
7
7

4
Matrix elements with duplicates removed:
2.00 1.00 3.00
6.00 5.00 6.00
4.00 7.00 7.00