Thread: Loop and standard deviation problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    14

    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;
    char answer;
    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)? ");
    	scanf(" %c",&answer);
    	} while (answer!='N');
    
    	return 0;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?

    your indentation sucks...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    14
    Quote Originally Posted by vart View Post
    your indentation sucks...
    I was asking about the problem, not the format/indentations.

    Thanks for tips anyways.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, but good indentation is an important thing and sometimes people do pick about it if you don't indent well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kyeong View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    14
    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.
    Last edited by Kyeong; 05-23-2008 at 10:04 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kyeong View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  2. Getting standard input
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 04-04-2004, 08:51 PM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. do loop repeats one time too many
    By sunburnbyRA in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 05:26 PM
  5. loops and I/O file.
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 09:41 AM