Solved

Finding the Standard deviation from a structure of numbers

Posted on 2003-11-25
11
354 Views
Last Modified: 2010-04-15
Im trying to find the standard deviation from a structure of numbers.
the array is in the format:

        s[x].sid=            301111;
        s[x].mark=      50;
        s[x].grade=      'A';

were x is incremented for every user.


heres the code for the standard deviation, its in the form of a function initalised using:

float stdDev(struct student s[], int n);

and called using:

stdDev(staff,ARR_SIZE);

heres the function... it just crashes! theres 5 entrys, which should be passed to the function in n
float stdDev(struct student s[], int n)
{
  int x;
  float  mean, sigma;


  mean = s[x].sid / n;
  for (x=0; x<5; x++) n += (mean - s[x].mark)*(mean-s[x].mark);
  sigma = sqrt(n/5);
  printf("Std Deviation is %f",sigma);


  return sigma;
0
Comment
Question by:chinswain
  • 5
  • 2
  • 2
11 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9819557
You're trying to access your struct in:

   mean = s[x].sid / n;

but x has not been initialized!

Try to change:

   int x;

to:

   int x = 0;
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9819564
Also, the for() loop, change:

   for (x=0; x<5; x++)

to:

   for (x=0; x<n; x++)

as 'n' is the size of the array!
0
 

Author Comment

by:chinswain
ID: 9820212
Its returning: -1.#IND00 any ideas on what could be wrong?
if you send your email to madlan@yahoo.com i will reply with the whole code,
if that would be any easier? or is there a better way to work out the Standard Deviation!

float stdDev(struct student s[], int n);




*/function called with:*/
stdDev(staff,ARR_SIZE);

*/Function:*/

float stdDev(struct student s[], int n)
{
  int x = 0;
  float  mean, sigma;


  mean = s[x].sid / n;
  for (x=0; x<n; x++) n += (mean - s[x].mark)*(mean-s[x].mark);
  sigma = sqrt(n/5);
  printf("Std Deviation is %f",sigma);
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9821055
Ok, I had a closer look at your aglorithm, and it is not correct, which of course is why you're getting the wrong result.

Change to the following:

float mean_mark(struct student s[], int n)
{
      int i;
      int acc = 0;
      for( i=0; i<n; i++ )
      {
            acc += s[i].mark;
      }
      return (float)acc / (float)n;
}

float stdDev(struct student s[], int n)
{
      int x;
      float  mean, sigma, sum = 0.0f;
      
      mean = mean_mark( s, n );
      for (x=0; x<n; x++)
      {
            sum += pow(mean - s[x].mark, 2.0);
      }
      sigma = sqrtf(sum/(n -1));
      std::cout << "Std Deviation is %f" << sigma;
      
      return sigma;
}

And, btw, you voilated your agreement by posting your email address!

Good luck!
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:chinswain
ID: 9839878
I belive this is a C++ alternative, is it possible to modify this for C? i think pow is the only command thats C++, apart from that, its perfect
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 250 total points
ID: 9840774
The code above is pure C. pow is included in the standard math.h

So the code is working for you now?
0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9853044
>>> std::cout << "Std Deviation is %f" << sigma;
>>>The code above is pure C
???!!!

replace that with a printf() and replace
 pow (x, 2.0)
with
 (x * x)


0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9864021
The code that does the work is pure C. The std::cout was a lapse.

0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9880593
I figured but couldn't resist the urge to be pedantic.  :)
E.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now