C program strange output and wrong output
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
add a comment |
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
1
Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextrafor gcc and clang. You might need to compile with-Otoo to see the most important one).
– Shawn
Nov 10 at 5:21
1
Why do you havedouble elementandfloat ComputeAverage? There are 32-bits difference in the width. Further -- youreturnnothing fromComputeAverage, so you may as well make itvoid.
– David C. Rankin
Nov 10 at 5:21
1
Also you should get a warning about uninitialized local variablefloat sum;, change it tofloat sum = 0;, and the functionComputeAverageshould returnaverage.
– Barmak Shemirani
Nov 10 at 5:27
add a comment |
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
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
c matrix rounding math.h
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 -Wextrafor gcc and clang. You might need to compile with-Otoo to see the most important one).
– Shawn
Nov 10 at 5:21
1
Why do you havedouble elementandfloat ComputeAverage? There are 32-bits difference in the width. Further -- youreturnnothing fromComputeAverage, so you may as well make itvoid.
– David C. Rankin
Nov 10 at 5:21
1
Also you should get a warning about uninitialized local variablefloat sum;, change it tofloat sum = 0;, and the functionComputeAverageshould returnaverage.
– Barmak Shemirani
Nov 10 at 5:27
add a comment |
1
Your compiler isn't giving you warnings with that code? If not, turn them on (-Wall -Wextrafor gcc and clang. You might need to compile with-Otoo to see the most important one).
– Shawn
Nov 10 at 5:21
1
Why do you havedouble elementandfloat ComputeAverage? There are 32-bits difference in the width. Further -- youreturnnothing fromComputeAverage, so you may as well make itvoid.
– David C. Rankin
Nov 10 at 5:21
1
Also you should get a warning about uninitialized local variablefloat sum;, change it tofloat sum = 0;, and the functionComputeAverageshould returnaverage.
– 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Your compiler isn't giving you warnings with that code? If not, turn them on (
-Wall -Wextrafor gcc and clang. You might need to compile with-Otoo to see the most important one).– Shawn
Nov 10 at 5:21
1
Why do you have
double elementandfloat ComputeAverage? There are 32-bits difference in the width. Further -- youreturnnothing fromComputeAverage, so you may as well make itvoid.– David C. Rankin
Nov 10 at 5:21
1
Also you should get a warning about uninitialized local variable
float sum;, change it tofloat sum = 0;, and the functionComputeAverageshould returnaverage.– Barmak Shemirani
Nov 10 at 5:27