I am trying to use pointers in functions and arrays and when I call the report function in main, I keep getting an error Expression must have a pointer to object type. I have tried everything. Nothing seem to be working. Can anyone please let me know what I am doing wrong?
Please note: without the report
function, if I call the other functions separately in main
it works. It's not working only with the report
function.
#include <stdio.h>
#include <conio.h>
void print(int *list, int row_count, int column_count);
void rowaverage(int *list, int row_count, int column_count);
void allaverage(int *list, int row_count, int column_count);
void largest(int *list, int row_count, int column_count);
void report(int *list, int row_count, int column_count);
int main()
{
int i = 1, row, column;
int list[3][5];
printf("Enter 3 sets of 5 integers::\n");
for (row = 0; row < 3; row++)
{
printf("Elements in the %d set are ::\n", row);
for (column = 0; column < 5; column++)
{
printf("Element No. %d is ", i++);
scanf("%d", &list[row][column]);
}
printf("\n");
i = 1;
}
printf("The elements in array are:\n");
report(&list[0][0], row, column);
getch();
return 0;
}
void print(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
printf("%8d", *(list + row * column_count + column));
}
printf("\n");
}
}
void rowaverage(int *list, int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++)
{
float sum = 0, count = 0;
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}
void allaverage(int *list, int row_count, int column_count)
{
int column, row;
float sum = 0, count = 0;
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
sum += *(list + row * column_count + column);
count++;
}
}
printf("Average of all elements in array is %.2f\n", (sum / count));
}
void largest(int *list, int row_count, int column_count)
{
int column = 0, row = 0;
int largest = *(list + row * column_count + column);
for (row = 0; row < row_count; row++)
{
for (column = 0; column < column_count; column++)
{
if (largest < *(list + row * column_count + column))
{
largest = *(list + row * column_count + column);
}
}
}
printf("The largest number in the array is %d\n", largest);
}
void report(int *list, int row_count, int column_count)
{
int row = 0, column = 0;
print(list[0][0], row, column);
printf("\n");
rowaverage(list[0][0], row, column);
printf("\n");
allaverage(list[0][0], row, column);
printf("\n");
largest(list[0][0], row, column);
}
In report function remove that line and see below report function:
int row = 0, column = 0;
In functions, use list as
int list[][5]
Call list as
list
not as
list[0][0]
Here is the complete code:
#include <stdio.h>
#include <stdlib.h>
void print(int list[][5], int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++)
printf("%8d", list[row][column]);
printf("\n");
}
}
void rowaverage(int list[][5], int row_count, int column_count)
{
int column, row;
for (row = 0; row < row_count; row++) {
float sum = 0, count = 0;
for (column = 0; column < column_count; column++) {
sum += list[row][column];
count++;
}
printf("Average of row %d is %.2f\n", row, (sum / count));
}
}
void allaverage(int list[][5], int row_count, int column_count)
{
int column, row;
float sum = 0, count = 0;
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++) {
sum += list[row][column];
count++;
}
}
printf("Average of all elements in array is %.2f\n", (sum / count));
}
void largest(int list[][5], int row_count, int column_count)
{
int column = 0, row = 0;
int largest = list[0][0];
for (row = 0; row < row_count; row++) {
for (column = 0; column < column_count; column++) {
if (largest < list[row][column]) {
largest = list[row][column];
}
}
}
printf("The largest number in the array is %d\n", largest);
}
void report(int list[][5], int row_count, int column_count)
{
print(list, row_count, column_count);
printf("\n");
rowaverage(list, row_count, column_count);
printf("\n");
allaverage(list, row_count, column_count);
printf("\n");
largest(list, row_count, column_count);
}
int main()
{
int i = 1, row, column;
int list[3][5];
printf("Enter 3 sets of 5 integers::\n");
for (row = 0; row < 3; row++) {
printf("Elements in the %d set are ::\n", row);
for (column = 0; column < 5; column++) {
printf("Element No. %d is ", i++);
scanf("%d", &list[row][column]);
}
printf("\n");
i = 1;
}
printf("The elements in array are:\n");
report(list, row, column);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With