# Thread: Loop and standard deviation problem

1. ## Loop and standard deviation problem

I get an undefined error when compiling this. Everything else works when I take out the standard deviation, but I get the error when I put it back in.

I also need help with the user input. My professor wants it done in a certain way and I just can't figure out how it's done. This is just an example of the output.

Enter Number:
1.0 5.5 2.9 -1

After that, the program is suppose to count how many integers there are and calculate the rest of the stuff. -1 is there to tell the program to not count the numbers beyond it.

One more thing. I can't find out why the loop isn't working in at the end. When I answer "y" it just prints back the summary instead of going back to the beginning asking "Enter number." I did it similar to her notes and it still doesn't work.

Thanks.

Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{

double a;
int n = 0;
double max = 0;
double min = 9999;
double sum = 0;
double vsum;
double mean = 0;
double vari = 0;
double devi = 0;

do {

while(a != -1)
{
printf("Enter numbers: \n");
scanf("%lg",&a);

if (a != -1)
{
n++;

if (a >= max)
max = a;
if (a <= min)
min = a;

sum += a;
vsum += a * a;
}
}

mean = sum / n;
vari = (vsum / n) - (mean * mean);
devi = sqrt(vari);

printf("You entered %i numbers \n", n);
printf("Maximum value entered: %g \n", max);
printf("Minimum value entered: %g \n", min);
printf("Sum of all the values read: %g \n", sum);
printf("Mean of all values: %g \n", mean);
printf("Variance of all values: %g \n", vari);
printf("Standard Deviation of all values: %g \n", devi);

printf("\nWould you like to factor another (Y/N)? ");

return 0;
}```

2. Variables vsum and a (a is also a very poor name for a variable) are uninitialized and used, which means you are NOT going to get defined behaviour.

3. when you firstly check a != -1 your a is not initialized - so result is unpredictable...

a is double - so it is not a good idea to check it for the strict eqality

vsum is not initialized

scanf formats like &#37;lf leave the whitespaces in the input stream while %c - reads them, guess what will be a result?

4. Originally Posted by vart
I was asking about the problem, not the format/indentations.

Thanks for tips anyways.

5. No, but good indentation is an important thing and sometimes people do pick about it if you don't indent well.

6. Originally Posted by Kyeong
I was asking about the problem, not the format/indentations.

Thanks for tips anyways.
Yes, but having correct indentation helps you and us to understand the code and see more clearly which bits of code belong to which loops/conditionals. Us understanding the code will help us help you. Of course, if you are not interested in getting the best possible help, please ignore any of this.

--
Mats

7. I do know I need good indentations, my professor wants it that way. I intended to do that right before turning it in. It's just the way I have been doing things. Also, I was in a rush at the time of posting this and just copy/pasted what I had then, but I guess I'll just state that next time.

Also, he could have just said that I needed to do correct indentations rather than just saying it plain out sucks.

8. I do know I need good indentations, my professor wants it that way. I intended to do that right before turning it in. It's just the way I have been doing things. Also, I was in a rush at the time of posting this and just copy/pasted what I had then, but I guess I'll just state that next time.
More haste less speed.

9. Originally Posted by Kyeong
I do know I need good indentations, my professor wants it that way. I intended to do that right before turning it in. It's just the way I have been doing things.
This is not how it works.
You indent right away, and keep good indentation. Otherwise you're pretty much cheating or negating the advantage of the indentation.
It's not something you do because your professor should be able to read the code, but to keep it organized, so that YOU (as well as others) can read and work with it.

If you don't know HOW or is too lazy to do it yourself, then there are IDEs that automatically indent for you. Two examples are Code::Blocks and Visual Studio.

Popular pages Recent additions