Function average and stdDev const int tab[ ]. Average problems
I have to use:
float average(const int tab, int size);
float stdDev(const int tab, int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab, int size);
float stdDev(const int tab, int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2fn", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab, int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab, int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
c function
add a comment |
I have to use:
float average(const int tab, int size);
float stdDev(const int tab, int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab, int size);
float stdDev(const int tab, int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2fn", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab, int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab, int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
c function
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are usingx
uninitialized.
– user10605163
Jan 2 at 2:35
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50
add a comment |
I have to use:
float average(const int tab, int size);
float stdDev(const int tab, int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab, int size);
float stdDev(const int tab, int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2fn", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab, int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab, int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
c function
I have to use:
float average(const int tab, int size);
float stdDev(const int tab, int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab, int size);
float stdDev(const int tab, int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2fn", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab, int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab, int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
c function
c function
asked Jan 2 at 2:21


HatariiMisakaHatariiMisaka
11
11
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are usingx
uninitialized.
– user10605163
Jan 2 at 2:35
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50
add a comment |
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are usingx
uninitialized.
– user10605163
Jan 2 at 2:35
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are using
x
uninitialized.– user10605163
Jan 2 at 2:35
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are using
x
uninitialized.– user10605163
Jan 2 at 2:35
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50
add a comment |
1 Answer
1
active
oldest
votes
You define the variable x
in average
here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x
a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
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%2f54000525%2ffunction-average-and-stddev-const-int-tab-average-problems%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 define the variable x
in average
here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x
a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
add a comment |
You define the variable x
in average
here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x
a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
add a comment |
You define the variable x
in average
here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x
a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
You define the variable x
in average
here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x
a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
edited Jan 2 at 2:56
answered Jan 2 at 2:50
user10605163user10605163
2,868624
2,868624
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
add a comment |
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
It worked. Thanks for help :D
– HatariiMisaka
Jan 2 at 2:53
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.
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%2f54000525%2ffunction-average-and-stddev-const-int-tab-average-problems%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
Your code compiles just fine, what is the error you want to solve? It will not run fine though, because you are using
x
uninitialized.– user10605163
Jan 2 at 2:35
I use in school special program to check my work and it sometimes do error. np enter 7 0 ave 116671774720.00⏎ std 0.00 I dont know why
– HatariiMisaka
Jan 2 at 2:45
I think a problem is that i have to add const int tab[101] but this doesnt work good
– HatariiMisaka
Jan 2 at 2:50
No, it is completely unrelated, see my answer below.
– user10605163
Jan 2 at 2:50