I'm having some issues with C.
I was tasked with this assignment:
Create a dynamically allocated matrix (max 5x5), let the user fill in the values, then send it to a function along with an unallocated array (1D) which will be used to store all values of the matrix that are lower than the average of each row. The 1D array should be defined in the main function and allocated inside the function that finds the desired elements.
This was going fine, I made my matrix, I sent it to the aforementioned function, but I could not change the values of the newly allocated 1D array.
#include<stdio.h>
#include<stdlib.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average) count++;
}
}
*p = (int*)malloc(count * sizeof(int));
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++) {
if (matrix[i][j] < average) *p[counter++] = matrix[i][j];
}
}
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
return 0;
}
For the input: 3 3 then 1 2 3 4 5 6 7 8 9 It outputs literally nothing.
**p and *p[0]
instead of
*p[counter++]
it outputs this: 7 -2467754 -2467754
What am I doing wrong? I thought the allocation was correct and that I could access *(p + n) but it doesn't do anything...