I'm a newbie to C programming, and wonder if anyone can tell me if the following code I've written for a text problem is correct in style, or can it be improved to look "professional".
Problem is:"Make 3 sets of 5 doubles stored as array.
Compute average of each row using VLA."
Particular question regards my style of passing in additional argument to function prototype so that row#
can be passed to function.
Code:
#include <stdio.h>
#define ROWS 3
#define COLS 5
void avg_row(int ,double ar[*],int ); //fn prototype
int main(void)
{
int i= ROWS,j=COLS;
double arr[i][j];
for(i=0;i<ROWS;i++) //initialize array
{
for(j=0;j<COLS;j++)
{
puts("Enter a double value");
while(scanf("%lf", &arr[i][j])!=1 && getchar()!='\n')
{
printf("Retry: Enter a double value!\n");
}
}
}
for(i=0;i<ROWS;i++) //1-D array function calls
{
avg_row(COLS,arr[i],i);
}
return 0;
}
}
void avg_row(int n,double ar[n],int m) //fn definition
{
double linetotal=0.0;
for(int i=0;i < n; i++) //sum array rows
{
linetotal += ar[i];
}
printf("Average for row %d is %.2f\n",m+1,linetotal/n);
}
Thanks for any help