The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculating variance in perl programming cdfd123 Shell Programming and Scripting 4 01-14-2008 10:30 PM
calculate from three files koti_rama Shell Programming and Scripting 5 01-08-2008 06:27 AM
How To Calculate krishna_sicsr Shell Programming and Scripting 1 03-28-2007 07:24 AM
How to calculate with awk whatisthis Shell Programming and Scripting 4 11-09-2005 09:39 AM
bc calculate problem Nicol UNIX for Dummies Questions & Answers 2 09-13-2004 07:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-08-2008
Registered User
 

Join Date: Jul 2007
Posts: 88
Post C language to calculate mean,variance

Here I want to calculate mean,variance and sum from a file
1.1*2*4*22*211*22*12*22*22*11
2.2*2*22*12*22*11*11*122*33*22
3.9*7*22*88*87*98*67*66*56*66*11
As this is a large file and i am trying to write in c

where formulae of

MEAN = 1/N (X1...+..Xn)
Variance = square root of 1/N-1 (X1+.,,Xn - mean)2

So simply question say in this example we have one file where first part want to calculate mean and variance
again second part calculate mean and variance ..respectively and save the result

#include <math.h>
void moment(float data[], int n, float *ave, float *var)

{
void nrerror(char error_text[]);
int j;
float ep=0.0,s,p;
if (n <= 1) nrerror("n must be at least 2 in moment");

s=0.0;
for (j=1;j<=n;j++) s += data[j];
*ave=s/n;

what will be the code for variance?
Can rewrite the program with mentioning of open file and closing of file
Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 01-08-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Code:
#include <math.h>
void moment(float data[], int n, float *ave, float *var)

{
    void nrerror(char error_text[]);
    int j;
    float ep=0.0,s,p;
    if (n <= 1) nrerror("n must be at least 2 in moment");
                                              
    s=0.0;
    for (j=1;j<=n;j++) s += data[j];
      *ave=s/n;
    variance = sqrt(pow((s - avg), 2)/(n - 1));
Reply With Quote
  #3  
Old 01-08-2008
Registered User
 

Join Date: Dec 2007
Posts: 260
Is this a homework assignment?? I keep reading posts with really "interesting" program requirements that seem to only have a theoretical benefit.
Reply With Quote
  #4  
Old 01-08-2008
Registered User
 

Join Date: Jul 2007
Posts: 88
Quote:
Originally Posted by frank_rizzo View Post
Is this a homework assignment?? I keep reading posts with really "interesting" program requirements that seem to only have a theoretical benefit.
ask the source code for variance that i have got..
home work assignment only if u don't attempt on a problem and writing questions only....
sorry if bother
Reply With Quote
  #5  
Old 01-08-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Question

Quote:
Here I want to calculate mean,variance and sum from a file
1.1*2*4*22*211*22*12*22*22*11
2.2*2*22*12*22*11*11*122*33*22
3.9*7*22*88*87*98*67*66*56*66*11
As this is a large file and i am trying to write in c
Are you trying to find the mean & variance of all the data points in the file or for every line of the file?

Reply With Quote
  #6  
Old 01-08-2008
Registered User
 

Join Date: Jul 2007
Posts: 88
Quote:
Originally Posted by shamrock View Post
Are you trying to find the mean & variance of all the data points in the file or for every line of the file?


Dear Shamrock,
Here trying to derive mean and variance for every line of the file Hereby each line is just say as separate entity.
That is In one file Input is
1.1*2*4*22*211*22*12*22*22*11 then derive its mean and variance
2.2*2*22*12*22*11*11*122*33*22 same derivations......
continue............etc.etc....
Reply With Quote
  #7  
Old 01-08-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Question

Quote:
Originally Posted by cdfd123 View Post
Dear Shamrock,
Here trying to derive mean and variance for every line of the file Hereby each line is just say as separate entity.
That is In one file Input is
1.1*2*4*22*211*22*12*22*22*11 then derive its mean and variance
2.2*2*22*12*22*11*11*122*33*22 same derivations......
continue............etc.etc....
So let me see if I understand your post. You need the mean & variance for every line of input and every one of those input lines has ten data values separated by the asterisk character and the first data value is a floating point number like 1.1 2.2 yada yada...correct me if I am wrong.
The code you have posted does not seem to be able to compute the mean and variance of every line of input.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0