Thread: how to pass array from one function into another: variance to mean

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    how to pass array from one function into another: variance to mean

    function name implies what it does: i.e. finds mean variance etc.
    how to pass array from one function into another: variance to mean-untitled-png
    I think the mean is ok. The resultant mean in the main function is 6.something, ask you can see from the image.

    But the mean is not right in the variance function, as you can see from a test print. What is wrong with the mean there?

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    double mean(int marks[], int n);
    double variance(int marks[], int n);
    double stdDev(int marks[], int n);
    double mode(int marks[], int n);
    void sort(int marks[], int n);
    
    
    int main(){
        
        int n,i;
        double t1, t2, t3, t4;
        
    
    
    
    
        
    
    
    
    
    
    
        FILE *inFile;
    
    
        inFile=fopen("marks.txt", "r");
        if (inFile==NULL)
        {
            printf("File open error!\n");
            return 0;
        }
        
        n=0;
        int count[10000];
        
        
        
        while((fscanf(inFile, "%d", &count[n]))==1) n++;
        int marks[n];
        for(i=0; i<n; i++) marks[i]=count[i];
    
    
        printf("%d is the number of readings\n", n);
        /*printf("testing marks array %d %d %d %d %d", marks[0], marks[1], marks[2], marks[812], marks[813]);*/
    
    
    
    
        t1=mean(marks, n);
    /*printf("testing marks array %d %d %d %d %d", marks[0], marks[1], marks[2], marks[812], marks[813]);*/
    
    
    
    
        t2=variance(marks, n);
    /*printf("testing marks array %d %d %d %d %d", marks[0], marks[1], marks[2], marks[812], marks[813]);*/
    
    
    
    
        t3=stdDev(marks, n);
    /*printf("testing marks array %d %d %d %d %d", marks[0], marks[1], marks[2], marks[812], marks[813]);*/
    
    
    
    
        t4=mode(marks, n);
    /*printf("testing marks array %d %d %d %d %d", marks[0], marks[1], marks[2], marks[812], marks[813]);*/
    
    
    
    
    
    
        printf("%lf %d %lf %d, mean, variance, stdDev, mode", t1, t2, t3, t4);
        fclose(inFile);
        return 0;
    }
    
    
    
    
    double mean(int marks[], int n){
        int i;
        int sum=0;
        for(i=0; i<n; i++) sum+=marks[i];
        return (1.0*sum/n);
    }
    
    
    double variance(int marks[], int n){
        int i;
        double ave, varsum;
        varsum=0;
        printf("%d %d %d marks test\n", marks[0], marks[1], marks[813]);
        
        ave=mean(marks, n);
        printf("%d ave test\n", ave);
        for (i=0; i<n; i++) varsum+=(marks[i]-ave)*(marks[i]-ave);
        printf("%d varsum, %d variance test \n", varsum, varsum/n);
        return (varsum/n);
    }
    double stdDev(int marks[], int n){
        int i;
        double var;
        var=variance(marks, n);
        return (sqrt(var));
    }
    
    
    double mode(int marks[], int n){
        int i, count=0;
        double mid;
        mid=n/2.0;
        sort(marks, n);
        for(i=0; i<n; i++){
            count=+marks[i];
            if (count==mid) return ((marks[i]+marks[i+1])/2.0); 
            if (count>mid) return(marks[i]);}
            return 0;
    
    
    
    
        } 
    
    
        void sort(int marks[], int n){
            int sorter[n];
            sorter[0]=marks[0];
            int i, temp;
            for(i=1; i<n; i++){
                if (marks[i]>=sorter[i]) sorter[i+1]=marks[i];
                else { 
                    temp=sorter[i];
                    sorter[i]=marks[i];
                    sorter[i+1]=temp;
                }
            }
    
    
                for (i=0; i<n; i++) marks[i]=sorter[i];
                return;}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So how many threads do you plan to start for the same problem?

    (Please tell me now so I can ignore them... )

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    it's no longer segmentation error, so i cannot continue with the same thread

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nanobot View Post
    it's no longer segmentation error, so i cannot continue with the same thread
    It's the same program....
    Sprouting 324,675,123 threads over it isn't going to make you any friends.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you could do
    gcc -Wall prog.c

    and see that all the format flags to your final printf are wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    thanks a lot, changed the variable types several times so I messed up . Going to fix mode function now

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Code:
    void sort(int marks[], int n){        int sorter[n]={0};
            sorter[0]=marks[0];
            int i, temp;
            for(i=1; i<n; i++){
                if (marks[i]>=sorter[i]) sorter[i+1]=marks[i];
                else { 
                    temp=sorter[i];
                    sorter[i]=marks[i];
                    sorter[i+1]=temp;
                }
            }
    
    
                for (i=0; i<n; i++) marks[i]=sorter[i];
                return;}
    After I intialized the sorter array in the function to zero, this error appears: variable sized objects may not be intialized
    excess elements in array intializer
    near intialization for 'sorter'

    this is the correct way to intialize all elements of array to 0, right?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No.

    Just do this:
    Code:
    void sort(int marks[], int n)
      {     
            int i, j, temp;
            for(i=0; i<n; i++)
              for (j = i; j< n; j++)
                if (marks[i] < marks[j])
                  {
                    temp=marks[i];
                    marks[i]=marks[j];
                    marks[j]=temp;
                  }
      }
    If you're going to work off a copy of the array...
    sort the copy then copy it back to the original...
    but you can just as easily do it right in the original array.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    thanks a lot for the tip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Can't pass array to function
    By yougene in forum C Programming
    Replies: 9
    Last Post: 08-19-2007, 04:09 PM
  3. how can i pass this array to my function?
    By kosodo in forum C Programming
    Replies: 4
    Last Post: 04-14-2006, 09:40 PM
  4. Replies: 3
    Last Post: 04-02-2002, 01:39 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM

Tags for this Thread