Wednesday 28 August 2013

Find The Mean & Standard Deviation of a Group of Numbers.

My girlfriend is a dietitian, she is taking another supplementary course or something. I don't know what the course is, I'm guessing. She is in another city and almost done with the course and only has to submit her papers for approval.
Yesterday she sent me some data she had collected. Her professor had asked her to:
1. Find The Mean & Standard Deviation of some of the data she had collected.
2. Type them out in 2 decimal places. (She sent me this "3.2821 .45588 how can I write these in 2 decimal places?")
3. Put the Plus-Minus sign before them.

She wanted to know how to do those.

As you can see from the fact that number #2 confuses her, she's almost as bad at math as I am. It's only because I was learning C that I knew 2 decimal places means ignore the numbers after 2 digits. so it would be 3.28 and .45 for 3.2821 .45588 respectively.

Number 3 is easy enough, the Plus-Minus sign can be found in the character map on any Windows system (Start>All Programs>Accessories>System Tools>Character Map on Windows 7), also, the character code for that character is 241.
So if she was typing in the character in MS Word, she would have to hold down the Alt key, type in 241 on the Num-Pad (doesn't have to be Num-Pad for Laptops), and let go of the Alt key and find ± (Incidentally, you can use that to type in any of the ASCII characters).

I was a bit confused on #1. I didn't know what Mean & Standard Deviation meant. She led me to a page:
http://www.mathsisfun.com/data/standard-deviation-formulas.html

She also told me she had a lot of data to find the Mean and SD for. So I wrote a little program that will solve it for her if she inputs the numbers.

As usual, just copy paste into Code::Blocks and press F9 to run the program.

Sample:


Code:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>

int get_key(void);

main()
{
    int loop,key,x,limit;
    float input[50],TotalInput1=0,TotalInput2=0,mean1,mean2,diff1[10],diff2[10],variance,median;
    char in[50][50];

    system("mode 158,77");

    printf("Enter your values (upto 50). Press Enter after each. Press F1 to calculate. Esc to exit\n");

    for(loop=0;loop<50;loop++)
    {
        x=0;
        while(x<10)
        {
            key=get_key();
            if((key<58 && key>47) || key==46 && x<10)
            {
                printf("%c",key);
                in[loop][x]=key;
                x++;
            }
            else if(key==13 && x>0)
            {
                in[loop][x]='\0';
                printf("\n");
                break;
            }
            else if(key==315) //f1
            {
                in[loop][x]='\0';
                system("cls");
                for(x=0;x<loop;x++)
                {
                    input[x]=atof(in[x]);
                    printf("%f\n",input[x]);
                }
                limit=loop;
                for(loop=0;loop<limit;loop++)
                {
                    TotalInput1=TotalInput1+input[loop];
                }
                mean1=TotalInput1/limit;
                for(loop=0;loop<limit;loop++)
                {
                    diff1[loop]=(input[loop]-mean1)*(input[loop]-mean1);
                }
                for(loop=0;loop<limit;loop++)
                {
                    TotalInput2=TotalInput2+diff1[loop];
                }
                mean2=TotalInput2/limit;
                for(loop=0;loop<limit;loop++)
                {
                    diff2[loop]=(diff1[loop]-mean2)*(diff1[loop]-mean2);
                }
                variance=(float)1/limit*TotalInput2;

                /*now find the sq root of variance. That is the mean.*/
                median=sqrt(variance);
                printf("You entered %i digits.\nTheir Mean(%c) =%f\nStandard Deviation(%c)=%f\n",limit,230,mean1,229,median);
                getch();
                system("cls");
                printf("Enter your values (upto 50). Press Enter after each. Press F1 to calculate. Esc to exit\n");
                break;
            }
            else if(key==8 && x>0)
            {
                printf("\b \b");
                x--;
            }
            else if(key==27)
            {
                system("mode 80,25");
                return 0;
            }
        }
    }
}

int get_key(void)
{
    int c = getch();
    switch (c)
    {
      case 0:   return getch()+256;
      case 224: return getch()+512;
    }
    return c;
}


4 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. https://ajay3303.blogspot.com/2013/05/program-to-find-standard-deviation-mean.html?showComment=1552994688622#c6575749606299358827

    ReplyDelete