Thread: small program stuck at segmentation fault (mean, median... using arrays)

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

    small program stuck at segmentation fault (mean, median... using arrays)

    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;
        int t2, t3, t4;
        double t1;
        printf("test0");
    
    
    
    
        
    
    
    
    
    
    
        FILE *inFile;
    
    
        inFile=fopen("marks.txt", "r");
        if (inFile==NULL)
        {
            printf("File open error!\n");
            return 0;
        }
        printf("test1");
        
        int marks[n];
        n=0;
        printf("test2");
        while((fscanf(inFile, "%d", &marks[n]))==1) n++;
        printf("%d\n", n);
        
    
    
    
    
        t1=mean(marks, n);
        t2=variance(marks, n);
        t3=stdDev(marks, n);
        t4=mode(marks, n);
    
    
        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;
        ave=mean(marks, n);
        for (i=0; i<n; i++) varsum+=(marks[i]-ave)*(marks[i]-ave);
        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;}
    I can't find where the error occurs. The function names represent what the function does (finds mean etc.). I tried some error testing printf, but even those do not print.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    int marks[n];
    n=0;
    printf("test2");
    while((fscanf(inFile, "%d", &marks[n]))==1) n++;
    printf("%d\n", n);

    None of this makes any sense.
    n is uninitialised at the time you create your array - who knows what that is going to give you.

    What you DON'T have is an array that magically expands with every increment of n in that loop.

    Use two while loops
    - one to simply count how many integers would be read
    - allocate the space
    - fseek() back to the start of the file, and clearerr() to reset end of file status
    - read at most n integers from the file.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yep - exactly what Salem said. Count the numbers, then allocate the space, then read them.


    Is the compiler warning you about this line?

    Code:
    int marks[n];
    I'd expect most compilers to complain about this: something like "using uninitialised variable", or "variable may not be initalised". It's worth taking notice of compiler warnings!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    sorry, i started on learning array yesterday. my lecture notes are vague as hell . working on it starting now

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thanks to you, the segmentation error is gone, but I think I do not know the right way to pass array from one function to another:
    how to pass array from one function into another: variance to mean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CS 1 Program segmentation fault but can't see the problem
    By DaNxTh3xMaNx in forum C Programming
    Replies: 1
    Last Post: 09-29-2010, 08:23 PM
  2. client-server program: segmentation fault
    By ankurcse in forum Linux Programming
    Replies: 6
    Last Post: 07-17-2006, 07:54 AM
  3. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM
  4. Segmentation fault in beginning or program
    By tameeyore in forum C Programming
    Replies: 1
    Last Post: 02-26-2005, 08:16 PM
  5. Segmentation fault !, program works:S
    By jspri in forum C Programming
    Replies: 8
    Last Post: 09-28-2004, 05:25 PM

Tags for this Thread