C program strange output and wrong output










0














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 = %dn",dc);

return EXIT_SUCCESS;










share|improve this question



















  • 1




    Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
    – Shawn
    Nov 10 at 5:21







  • 1




    Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
    – David C. Rankin
    Nov 10 at 5:21







  • 1




    Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
    – Barmak Shemirani
    Nov 10 at 5:27















0














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 = %dn",dc);

return EXIT_SUCCESS;










share|improve this question



















  • 1




    Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
    – Shawn
    Nov 10 at 5:21







  • 1




    Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
    – David C. Rankin
    Nov 10 at 5:21







  • 1




    Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
    – Barmak Shemirani
    Nov 10 at 5:27













0












0








0







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 = %dn",dc);

return EXIT_SUCCESS;










share|improve this question















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 = %dn",dc);

return EXIT_SUCCESS;







c matrix rounding math.h






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 5:55









David C. Rankin

40.2k32647




40.2k32647










asked Nov 10 at 5:12









Tyreese Davis

74




74







  • 1




    Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
    – Shawn
    Nov 10 at 5:21







  • 1




    Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
    – David C. Rankin
    Nov 10 at 5:21







  • 1




    Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
    – Barmak Shemirani
    Nov 10 at 5:27












  • 1




    Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
    – Shawn
    Nov 10 at 5:21







  • 1




    Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
    – David C. Rankin
    Nov 10 at 5:21







  • 1




    Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
    – Barmak Shemirani
    Nov 10 at 5:27







1




1




Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
– Shawn
Nov 10 at 5:21





Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextra for gcc and clang. You might need to compile with -O too to see the most important one).
– Shawn
Nov 10 at 5:21





1




1




Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
– David C. Rankin
Nov 10 at 5:21





Why do you have double element and float ComputeAverage? There are 32-bits difference in the width. Further -- you return nothing from ComputeAverage, so you may as well make it void.
– David C. Rankin
Nov 10 at 5:21





1




1




Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
– Barmak Shemirani
Nov 10 at 5:27




Also you should get a warning about uninitialized local variable float sum;, change it to float sum = 0;, and the function ComputeAverage should return average.
– Barmak Shemirani
Nov 10 at 5:27












1 Answer
1






active

oldest

votes


















2














You invoke Undefined Behavior by using the return of float ComputeAverage(Matrix a) (which returns no value) in float ave = ComputeAverage(M);



You invoke Undefined Behavior within the above Undefined Behavior by using sum uninitialized in sum += a.element[i][j];



You invoke Undefined Behavior within the both above Undefined Behavior by passing a float to round(ave) where round expects a double.



Solution -- Make ComputeAverage(Matrix a) type double and return average; (and clean up the warnings regarding Missing Braces in your Matrix initializers), e.g.



#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 (" %3g", a.element[i][j]);
putchar ('n');



double ComputeAverage(Matrix a)

double sum = 0;
double average;
int i;
int j;

for (i = 0; i < N; i++)
for (j=0; j<N; j++)
sum += a.element[i][j];
average = sum / 64.0;


return average;



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 (void)

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 ;

PrintMatrix (M);
double ave = ComputeAverage(M);
printf ("ave: %gn", ave);
int dc = round(ave);
printf ("dc : %dn", dc);

return EXIT_SUCCESS;



Example Use/Output



$ ./bin/computeavg
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
ave: 148.281
dc : 148


Enable Compiler Warnings



Always compile with warnings enabled, and do not accept code until it compiles cleanly without warning. To enable warnings add -Wall -Wextra to your gcc or clang compile string. (add -pedantic for several additional warnings). For clang, instead you can use -Weverything (but that includes numerous extraneous warnings). For both gcc/clang, recommend:



 -Wall -Wextra -pedantic -Wshadow


For VS (cl.exe on windoze), add /W3 (or use /Wall but you will get quite a few extraneous non-code related warnings).



Read and understand each warning. They will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.



Let me know if you have further questions.






share|improve this answer




















  • Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
    – Tyreese Davis
    Nov 10 at 18:17










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236168%2fc-program-strange-output-and-wrong-output%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You invoke Undefined Behavior by using the return of float ComputeAverage(Matrix a) (which returns no value) in float ave = ComputeAverage(M);



You invoke Undefined Behavior within the above Undefined Behavior by using sum uninitialized in sum += a.element[i][j];



You invoke Undefined Behavior within the both above Undefined Behavior by passing a float to round(ave) where round expects a double.



Solution -- Make ComputeAverage(Matrix a) type double and return average; (and clean up the warnings regarding Missing Braces in your Matrix initializers), e.g.



#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 (" %3g", a.element[i][j]);
putchar ('n');



double ComputeAverage(Matrix a)

double sum = 0;
double average;
int i;
int j;

for (i = 0; i < N; i++)
for (j=0; j<N; j++)
sum += a.element[i][j];
average = sum / 64.0;


return average;



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 (void)

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 ;

PrintMatrix (M);
double ave = ComputeAverage(M);
printf ("ave: %gn", ave);
int dc = round(ave);
printf ("dc : %dn", dc);

return EXIT_SUCCESS;



Example Use/Output



$ ./bin/computeavg
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
ave: 148.281
dc : 148


Enable Compiler Warnings



Always compile with warnings enabled, and do not accept code until it compiles cleanly without warning. To enable warnings add -Wall -Wextra to your gcc or clang compile string. (add -pedantic for several additional warnings). For clang, instead you can use -Weverything (but that includes numerous extraneous warnings). For both gcc/clang, recommend:



 -Wall -Wextra -pedantic -Wshadow


For VS (cl.exe on windoze), add /W3 (or use /Wall but you will get quite a few extraneous non-code related warnings).



Read and understand each warning. They will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.



Let me know if you have further questions.






share|improve this answer




















  • Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
    – Tyreese Davis
    Nov 10 at 18:17















2














You invoke Undefined Behavior by using the return of float ComputeAverage(Matrix a) (which returns no value) in float ave = ComputeAverage(M);



You invoke Undefined Behavior within the above Undefined Behavior by using sum uninitialized in sum += a.element[i][j];



You invoke Undefined Behavior within the both above Undefined Behavior by passing a float to round(ave) where round expects a double.



Solution -- Make ComputeAverage(Matrix a) type double and return average; (and clean up the warnings regarding Missing Braces in your Matrix initializers), e.g.



#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 (" %3g", a.element[i][j]);
putchar ('n');



double ComputeAverage(Matrix a)

double sum = 0;
double average;
int i;
int j;

for (i = 0; i < N; i++)
for (j=0; j<N; j++)
sum += a.element[i][j];
average = sum / 64.0;


return average;



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 (void)

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 ;

PrintMatrix (M);
double ave = ComputeAverage(M);
printf ("ave: %gn", ave);
int dc = round(ave);
printf ("dc : %dn", dc);

return EXIT_SUCCESS;



Example Use/Output



$ ./bin/computeavg
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
ave: 148.281
dc : 148


Enable Compiler Warnings



Always compile with warnings enabled, and do not accept code until it compiles cleanly without warning. To enable warnings add -Wall -Wextra to your gcc or clang compile string. (add -pedantic for several additional warnings). For clang, instead you can use -Weverything (but that includes numerous extraneous warnings). For both gcc/clang, recommend:



 -Wall -Wextra -pedantic -Wshadow


For VS (cl.exe on windoze), add /W3 (or use /Wall but you will get quite a few extraneous non-code related warnings).



Read and understand each warning. They will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.



Let me know if you have further questions.






share|improve this answer




















  • Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
    – Tyreese Davis
    Nov 10 at 18:17













2












2








2






You invoke Undefined Behavior by using the return of float ComputeAverage(Matrix a) (which returns no value) in float ave = ComputeAverage(M);



You invoke Undefined Behavior within the above Undefined Behavior by using sum uninitialized in sum += a.element[i][j];



You invoke Undefined Behavior within the both above Undefined Behavior by passing a float to round(ave) where round expects a double.



Solution -- Make ComputeAverage(Matrix a) type double and return average; (and clean up the warnings regarding Missing Braces in your Matrix initializers), e.g.



#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 (" %3g", a.element[i][j]);
putchar ('n');



double ComputeAverage(Matrix a)

double sum = 0;
double average;
int i;
int j;

for (i = 0; i < N; i++)
for (j=0; j<N; j++)
sum += a.element[i][j];
average = sum / 64.0;


return average;



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 (void)

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 ;

PrintMatrix (M);
double ave = ComputeAverage(M);
printf ("ave: %gn", ave);
int dc = round(ave);
printf ("dc : %dn", dc);

return EXIT_SUCCESS;



Example Use/Output



$ ./bin/computeavg
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
ave: 148.281
dc : 148


Enable Compiler Warnings



Always compile with warnings enabled, and do not accept code until it compiles cleanly without warning. To enable warnings add -Wall -Wextra to your gcc or clang compile string. (add -pedantic for several additional warnings). For clang, instead you can use -Weverything (but that includes numerous extraneous warnings). For both gcc/clang, recommend:



 -Wall -Wextra -pedantic -Wshadow


For VS (cl.exe on windoze), add /W3 (or use /Wall but you will get quite a few extraneous non-code related warnings).



Read and understand each warning. They will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.



Let me know if you have further questions.






share|improve this answer












You invoke Undefined Behavior by using the return of float ComputeAverage(Matrix a) (which returns no value) in float ave = ComputeAverage(M);



You invoke Undefined Behavior within the above Undefined Behavior by using sum uninitialized in sum += a.element[i][j];



You invoke Undefined Behavior within the both above Undefined Behavior by passing a float to round(ave) where round expects a double.



Solution -- Make ComputeAverage(Matrix a) type double and return average; (and clean up the warnings regarding Missing Braces in your Matrix initializers), e.g.



#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 (" %3g", a.element[i][j]);
putchar ('n');



double ComputeAverage(Matrix a)

double sum = 0;
double average;
int i;
int j;

for (i = 0; i < N; i++)
for (j=0; j<N; j++)
sum += a.element[i][j];
average = sum / 64.0;


return average;



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 (void)

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 ;

PrintMatrix (M);
double ave = ComputeAverage(M);
printf ("ave: %gn", ave);
int dc = round(ave);
printf ("dc : %dn", dc);

return EXIT_SUCCESS;



Example Use/Output



$ ./bin/computeavg
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
ave: 148.281
dc : 148


Enable Compiler Warnings



Always compile with warnings enabled, and do not accept code until it compiles cleanly without warning. To enable warnings add -Wall -Wextra to your gcc or clang compile string. (add -pedantic for several additional warnings). For clang, instead you can use -Weverything (but that includes numerous extraneous warnings). For both gcc/clang, recommend:



 -Wall -Wextra -pedantic -Wshadow


For VS (cl.exe on windoze), add /W3 (or use /Wall but you will get quite a few extraneous non-code related warnings).



Read and understand each warning. They will identify any problems, and the exact line on which they occur. You can learn a lot by listening to what your compiler is telling you.



Let me know if you have further questions.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 5:43









David C. Rankin

40.2k32647




40.2k32647











  • Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
    – Tyreese Davis
    Nov 10 at 18:17
















  • Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
    – Tyreese Davis
    Nov 10 at 18:17















Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
– Tyreese Davis
Nov 10 at 18:17




Good morning, thank you for your response. I had originally had sum initialized to 0.0, and I had missed the return statement altogether :(. My question is do you have a link to the official documentation for the C libraries? The only documentation I found about the built in round function in C said it takes double/floats/longs so I wouldn't have considered changing the float values to doubles. Is there anything specific you suggest I read or listen to while studying to help with the learning? Or more practice than anything?
– Tyreese Davis
Nov 10 at 18:17

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236168%2fc-program-strange-output-and-wrong-output%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages