Good evening. I was trying to to have the following program give me the average and round the average. So far I have the following.
When using <math.h>
's round
function it returns a zero. Yes I have tried compiling with -std=99
, -lm
and -fno-builtin
. None of these seem to matter. It still returns zero.
It should be using the ave
given by ComputeAverage
. This is where I get strange output. The Average sometimes shows as 148.28
(which should be correct) when it is run.
However I noticed if I run it to close to the previous run I get the following examples: 2303125649788529278976.00
, -31360458752.00
, -319407486092479668309433442631680.00
, and 2618000384.00
.
Also please be aware I'm still adding to the code but have tried to make sure I remove anything that isn't useful to the current situation. The code is as follows.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define _ISOC99_SOURCE
#define N 8
typedef struct _Matrix {
double element[N][N];
} Matrix;
void PrintMatrix(Matrix a)
{
int i;
int j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
printf("%g ", a.element[i][j]);
}//Inner for
printf("\n");
}//Outer For
}// printMatrix
float ComputeAverage(Matrix a)
{
float sum;
float average;
int i;
int j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
sum += a.element[i][j];
}//inner for
average = sum / 64;
}//for
//a.element[i][j];
printf("Average = %.2f",average);
printf("\n");
// printf ("Testing Sum = %f", sum);
// printf("\n");
}// ComputeAverage
Matrix Q50 = {{16, 11, 10, 16, 24, 40, 51, 61,
12, 12, 14, 19, 26, 58, 60, 55,
14, 13, 16, 24, 40, 57, 69, 56,
14, 17, 22, 29, 51, 87, 80, 62,
18, 22, 37, 56, 68,109,103, 77,
24, 35, 55, 64, 81,104,113, 92,
49, 64, 78, 87,103,121,120,101,
72, 92, 95, 98,112,100,103, 99}
};
int main(int argc, const char * argv[])
{
Matrix M = {{154, 123, 123, 123, 123, 123, 123, 136,
192, 180, 136, 154, 154, 154, 136, 110,
254, 198, 154, 154, 180, 154, 123, 123,
239, 180, 136, 180, 180, 166, 123, 123,
180, 154, 136, 167, 166, 149, 136, 136,
128, 136, 123, 136, 154, 180, 198, 154,
123, 105, 110, 149, 136, 136, 180, 166,
110, 136, 123, 123, 123, 136, 154, 136}};
// need to implement PrintMatrix
PrintMatrix(M);
// need to implement ComputeAverage
float ave = ComputeAverage(M);
// need to implement round
int dc = round(ave);
//printf("Ave = %d\n",dc);
return EXIT_SUCCESS;
}