Hello I have a problem with code. I need to count 2 more options:
- arithmetic average under the main diagonal
- arithmetic average of the second diagonal
I wrote the code below, but I don't know how I can count these 2 options. Any help will be nice to see. Thanks!
double arithmetic(int *arr, int n);
int main() {
int n, sum = 0, **A, *arr, l;
printf("Enter size of matrix: ");
scanf("%d", &n);
A = (int **)malloc(sizeof(int *) * n);
arr = (int *)malloc(sizeof(int *) * n);
if (!A)
printf("Error");
for (int i = 0; i < n; i++) {
A[i] = (int *)malloc(sizeof(int) * n);
if (!A[i])
printf("Error");
}
printf("Give numbers by lines:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Give the line and column to count: ");
scanf("%d", &l);
for (int i = 0; i < n; i++) {
arr[i] = A[l][i];
}
printf("Arithmetic average in line %d, is %.2f\n", l, arithmetic(arr, n));
for (int i = 0; i < n; i++) {
arr[i] = A[i][l];
}
printf("Arithmetic average in column %d, is %.2f\n", l, arithmetic(arr, n));
for (int i = 0; i < n; i++) {
arr[i] = A[i][i];
}
printf("Arithmetic average on main diagonal, is %.2f\n", arithmetic(arr, n));
}
double arithmetic(int *arr, int n) {
double sum = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > 0)
sum = sum + arr[i];
}
double result = sum / n;
return result;
}