0

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Foox
  • 33
  • 4
  • 1
    Doesn't the statement `arr = (int*)malloc(sizeof(int*) * n);` seem a little off to you? What are you really allocating memory for? What is the size? And what happens if `malloc` fails? – Some programmer dude May 23 '21 at 15:02
  • And talking about `malloc`, in C [you really shouldn't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude May 23 '21 at 15:04

1 Answers1

2

There are some issues in the code:

  • the allocation size for arr is incorrect: it should be arr = (int *)malloc(sizeof(int) * n); Note however that this intermediary array is not necessary.

  • you should exit the program upon allocation error.

  • why do you only count the positive numbers?

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

int main() {
    double sum;
    int n, sum = 0, l;
    int **A;

    printf("Enter matrix size: ");
    if (scanf("%d", &n) != 1)
        return 1;
    A = calloc(sizeof(*A), n);
    if (!A) {
       fprintf(stderr, "Allocation error\n");
       return 1;
    }
    for (int i = 0; i < n; i++) {
        A[i] = calloc(sizeof(int), n);
        if (!A[i]) {
            fprintf(stderr, "Allocation error\n");
            return 1;
        }
    }
    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);
    sum = 0;
    for (int i = 0; i < n; i++) {
        sum += A[l][i];
    }
    printf("Arithmetic average in line %d, is %.2f\n", l, sum / n);

    sum = 0;
    for (int i = 0; i < n; i++) {
        sum += A[i][l];
    }
    printf("Arithmetic average in column %d, is %.2f\n", l, sum / n);

    sum = 0;
    for (int i = 0; i < n; i++) {
        sum += A[i][i];
    }
    printf("Arithmetic average on main diagonal, is %.2f\n", sum / n);

    sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            sum += A[i][j];
        }
    }
    printf("Arithmetic average under main diagonal, is %.2f\n",
           sum / (n * (n - 1) / 2));

    sum = 0;
    for (int i = 0; i < n; i++) {
        sum += A[i][n - i - 1];
    }
    printf("Arithmetic average on second diagonal, is %.2f\n", sum / n);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks @chqrlie, i see my mistakes now. But i have a problem with count of arithmetic average below the main diagonal in this code. In normal case i would use if(i>j) but i need to stick with my function of my first code and i dont have idea how i can do it... – Foox May 23 '21 at 19:01
  • @Foox: I added the computation for the average under the first diagonal (excluding the diagonal). – chqrlie May 23 '21 at 19:07