Cześć!
Zadanie polega na wprowadzeniu 25 wartości to tablicy dwuwymiarowej 5x5, a następnie program oblicza i wyświetla min. max. oraz średnią dla każdego wiersza i kolumny. Program działa, wszystko liczy poprawnie, ale test maszynowy wyrzuca błąd i nie wiem jak go poprawić:
main.c:30:22: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
int const *ptr = ptrA;
Poniżej kompletny kod programu:
#include<stdio.h>
#include<stdlib.h>
int row_statistics(const int *ptr, int width, int height, int row_id, int* max, int* min, float *avg);
int column_statistics(const int *ptr, int width, int height, int column_id, int* max, int* min, float *avg);
int main()
{
int i,j;
int times=25;
int max;
int *p_max = &max;
int min;
int *p_min = &min;
float avg;
float *p_avg = &avg;
int column_id, row_id;
int tab[5][5];
/*int tab[5][5] = {
{55, 73, 30, 82, 15},
{91, 90, 92, 11, 31},
{63, 56, 45, 97, 25},
{6 , 55, 50, 22, 99},
{28, 39, 60, 2, 58}
};*/
int (*ptrA)[5];
ptrA = tab;
int const *ptr = ptrA;
printf("Podaj liczby:\n");
for (i=0;i<5;i++)
for(j=0;j<5;j++)
{
scanf("%d", *(ptrA+i)+j);
}
/*for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ", *(*(ptr+i)+j));
}
printf("\n");
}*/
for(column_id=0;column_id<5;column_id++)
{
column_statistics(ptr, 5, 5, column_id, p_max, p_min, p_avg);
printf("%d %d %.2f", min, max, avg);
printf("\n");
}
for(row_id=0; row_id<times; row_id=row_id+5)
{
row_statistics(ptr, 5, 5, row_id, p_max, p_min, p_avg);
printf("%d %d %.2f", min, max, avg);
printf("\n");
}
return 0;
}
int row_statistics(const int *ptr, int width, int height, int row_id, int* max, int* min, float *avg)
{
if (ptr != NULL || max != NULL || min!= NULL || avg!=NULL)
{
int i=0;
height = height+0;
*(min) = *(ptr+row_id);
*(max)= *(ptr+row_id);
int suma = *(ptr+row_id);
for(i=1; i<width; i++)
{
if (*(min)> *(ptr+row_id+i))
{
*(min)= *(ptr+row_id+i);
}
if(*(max) < *(ptr+row_id+i))
{
*(max)=*(ptr+row_id+i);
}
suma = suma + *(ptr+row_id+i);
}
*(avg) = ((float) suma) / 5.0;
return 1;
}
else
{
return 0;
}
}
int column_statistics(const int *ptr, int width, int height, int column_id, int* max, int* min, float *avg)
{
if (ptr != NULL || max != NULL || min!= NULL || avg!=NULL)
{
int i=0;
width = width + 0;
*(min) = *(ptr+column_id);
*(max)= *(ptr+column_id);
int suma = *(ptr+column_id);
for(i=5;i<(height*5); i=i+5)
{
if (*(min)> *(ptr+column_id+i))
{
*(min)= *(ptr+column_id+i);
}
if(*(max) < *(ptr+column_id+i))
{
*(max)= *(ptr+column_id+i);
}
suma = suma + *(ptr+column_id+i);
}
*(avg)= ((float) suma) / 5.0;
return 1;
}
else
{
return 0;
}
}