Thread: Help with program that calculates confidence interval

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Help with program that calculates confidence interval

    Hi!!
    I need a little help with my program.
    I have done a program that calculates mean, variance and standard devisation now I need to add the calculation for confidence interval.

    The confidence interval is calculated using this equation:
    mean +/- z*(standard deviation/number of samples)

    I have added this to my code:
    Function prototype: double conf_int(double n[ ], double stv, double z);
    and
    double z;
    printf ("Enter the z value");
    scanf ("%d", &z);

    This is wher I get stuck.
    I am nto sure on how to do write the code for calculationg confidence interval.
    The output for example should look like this: 73.125 +/- 69.536

    This is my code
    Code:
    #include <stdio.h>
    #include <math.h>
    #define N 12
    
    /* Function prototypes */
    double mean(double n[ ]);
    double variance(double n[ ], double m);
    double std_dev(double v);
    double conf_int(double n[ ], double stv, double z);
    
    void main(void)
    {
    
    	double mu, v, stv;
    	double num[N] = {87.8, 45.7, 56.7, 78.6, 49.7, 91.2, 87.6, 63.6, 59.8, 98.3, 77.2, 81.3};
    	double z;
    	printf ("Enter the z value");
    	scanf ("%d", &z);
    
    /*	int ctr;
    
    	for (ctr=0; ctr<N; ctr++)
          { 
    		printf ("Please enter the next number . . . ");
           	scanf ("%f", &num[ctr]);
           }
    	printf ("fdls %f", num[1]); */
    
    	mu = mean (num);
    	v = variance (num, mu);
    	stv = std_dev(v);
    	printf ("Mean = %.3f, Variance = %.3f\n", mu, v);
    	printf ("Standard Deviation = %.3f\n", stv);
    	printf ("Confidence Interval = %.3f\n", conf_int);
    
    }
    
    double mean(double n[N])
    {
    	double sum=0.0, retVal;
    	int i;
    
    	for (i=0; i<N; i++)
    	{
    		sum += n[i]; 	
    	}
    	
    	retVal = sum/N;
    	return retVal;
    }
    
    double variance(double n[N], double m)
    {
    	double ctr=0.0, retVal;
    	int i;
    
    	for (i=0; i<N; i++)
    	{
    		ctr += pow ((n[i] - m), 2);
    	}
    
    	retVal = ctr/(N-1);
    	return retVal;
    }
    
    double std_dev(double v)
    {
    	double retVal;
    
    	retVal = sqrt(v);
    	return retVal;
    }
    
    double conf_int(double n[ ], double stv, double z);
    {
    ?????????????????????????????
    THANK YOU

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. The formula is mean +/- z*(stdev/sqrt(number of samples)).

    2. Your function takes the list of numbers, the standard deviation, and z. So you'll need to calculate the mean and the number of samples. The first is easy; the second is impossible (due to the fact that C does not pass the size of the array into a function for you). However, you seem to know ahead of time that N=12, so you can run with that.

    3. Once you do that, you have to calculate the mean (you have a function to do that) and z*(stdev/sqrt(12)), which involves, well, typing exactly that.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    double z;
    scanf ("&#37;d", &z);
    do not lie to scanf... read scanf format specifiers - double should be scanned with %lf
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Program that calculates the value of PI
    By noodles in forum C Programming
    Replies: 7
    Last Post: 09-06-2006, 05:20 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM