Array loading with doubles in C
Posted
by
user2892120
on Stack Overflow
See other posts from Stack Overflow
or by user2892120
Published on 2013-10-17T21:36:29Z
Indexed on
2013/10/17
21:53 UTC
Read the original article
Hit count: 157
I am trying to load a 3x8 array of doubles but my code keeps outputting 0.00 for all of the values. The code should be outputting the array (same as the input) under the Read#1 Read#2 Read#3 lines, with the average under average.
Here is my code:
#include <stdio.h>
double getAvg(double num1, double num2, double num3);
int main()
{
int numJ,month,day,year,i,j;
double arr[3][8];
scanf("%d %d %d %d",&numJ,&month,&day,&year);
for (i = 0; i < 8; i++)
{
scanf("%f %f %f",&arr[i][0], &arr[i][1], &arr[i][2]);
}
printf("\nJob %d Date: %d/%d/%d",numJ,month,day,year);
printf("\n\nLocation Read#1 Read#2 Read#3 Average");
for (j = 0; j < 8; j++)
{
printf("\n %d %.2f %.2f %.2f %.2f",j+1,arr[j][0],arr[j] [1],arr[j][2],getAvg(arr[j][0],arr[j][1],arr[j][2]));
}
return 0;
}
double getAvg(double num1, double num2, double num3)
{
double avg = (num1 + num2 + num3) / 3;
return avg;
}
Input example:
157932 09 01 2013
0.00 0.00 0.00
0.36 0.27 0.23
0.18 0.16 0.26
0.27 0.00 0.34
0.24 0.00 0.31
0.16 0.33 0.36
0.29 0.36 0.00
0.21 0.36 0.00
© Stack Overflow or respective owner