Thread: Find me this last syntax error!

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Angry Find me this last syntax error!

    I receive the error right on the last line that contains the final bracket of the program and i have no idea why maybe a new set of eyes can help me
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h> 
    #define ARRAY_SIZE 50
    
    void fill(int a[]);
    void sortArray(int a[]);
    double findMean(int a[]);
    double get_median(int a[]);
    int findMode(int a[]);
    void printResults(double mean, double median, double mode);
    
    int 
    main(void) 
    {
    	
    	//variables
    	double mean, median, mode;
    	int array[ARRAY_SIZE];
    	
    	// fill, sort and print array
    	fill(array);
    	sortArray(array);
    	
    	//define and calculate mean median mode
    	mean = findMean(array);
    	median = get_median(array);
    	mode = findMode(array);
    	
    	//Print Results
    	printResults(mean, median, mode);
    
    	return 0;
    }
    
    									// functions
    									
    //fills array a with random numbers from 0-100.
    void fill(int a[]) {
    	int i;
    	struct timeval tv;
    
    	gettimeofday(&tv, NULL);
    	srand(tv.tv_usec);
    
    	for(i = 0; i < ARRAY_SIZE; i++) {
    		a[i] = rand() % 101;
    	}
    }
    
    
    //sorts array out
    void sortArray(int a[])
    {
     int i,
     temp, 
     j;
     
     for (i=0; i < i-1; ++i)
         {
               if ( a[i] > a[j] )
    			{
    				temp = a[i] ;
    				a[i] = a[j] ;
    				a[j] = temp ;
    			}
    	}
          printf("%d ", a[i]);
    }
    
    //Mean function >:I
    
    double findMean(int a[]) 
    {
        int i;
        double sum = 0.0;
        for(i = 0; i < ARRAY_SIZE; i++) {
            sum += a[i];
        }
        return (sum/ARRAY_SIZE);
    }
    
    //Median Fucntion
    
    double get_median(int a[]) {
    	double median;
    	int x,y;
    
    	if(ARRAY_SIZE % 2 == 1) { //array_size is odd
    		x = a[ARRAY_SIZE / 2];
    		y = a[ARRAY_SIZE / 2 - 1];
    		median = ((double)x + y)/2.0;
    	} else { //array_size is even
    		median = a[ARRAY_SIZE / 2];
    	}
    
    	return median;
    }
    
    //Mode Function
    
    int findMode(int a[])
    {
        int i, j, maxCount, modeValue;
        int tally[ARRAY_SIZE];
        for (i = 0; i < ARRAY_SIZE; i++) {
             tally[a[i]]++;
        }
        maxCount = 0;
        modeValue = 0;
        for (j = 0; j < ARRAY_SIZE; j++) {
            if (tally[j] > maxCount) {
                maxCount = tally[j];
                modeValue = j;
            }
        }
        return modeValue;
    }
    
    //Print out results
    
    void printResults(double mean, double median, double mode) 
    {
        printf("Mean: %d\tMedian: %d\tMode: %i", mean, median, mode);
    }
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    how many }'s are at the end of the file?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A consistent indentation style would go a long way here. Half the time you put it at the end of the line and half the time its on the next line.
    However, doesn't line 126 appear to be quite an obvious mistake?

    You might also like to know that your sortArray function is horendously broken.
    Turn on your compiler warnings and pay attention to them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    #define ARRAY_SIZE 50
    ...
    void fill(int a[]) {
    ...
        for(i = 0; i < ARRAY_SIZE; i++) {
            a[i] = rand() % 101;
    ...
    }
    ...
    int findMode(int a[])
    {
        int tally[ARRAY_SIZE];
        for (i = 0; i < ARRAY_SIZE; i++) {
             tally[a[i]]++;
    What are the initial values for "tally"?
    What are the possible values for "a[i]" and what are the valid indices for "tally"?

    Your median calculation is wrong too. It should be the other way around.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2012, 04:25 PM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM

Tags for this Thread