Thread: Problem With Program

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Problem With Program

    Program is designed to return the average of a series of 7 scores, with the max and min removed, a la judging in ice skating.

    I'm getting no return, with a printf check before any code not returning.

    Any help is greatly appreciated.

    Code:
    /*Brendan Keeler - Oct. 5, 2009 - The purpose of this program is to read from the file in an array, compute maximum, minimum, average, and return average from function to main and then printing average.*/
    
    #include <stdio.h>
    
    void read_file(int (*scores)[7]){
    	FILE *fin;
    	int i=0;
    	fin = fopen("input.txt", "r");
    	while(!feof(fin)) { 
          	/* loop through and store the numbers into the array */
          		fscanf(fin, "%d", scores[i]);
          		i++;
    	}
    	fclose(fin);
    }
    
    int compute(int (*scores)[7]){
    	int i = 0;
    	int total=0, average, max=*scores[1], min=*scores[1];
    	for(i = 0; i<7; i++){
    		total = total + *scores[i];
    		if (*scores[i] > max){
    			max = *scores[i];
    		}
    		if (*scores[i] < min){
    			min = *scores[i];
    		total = total - max - min;
    		}
    	}
    	average = total/7;
    	return average;
    }
    
    void show_average(average){
    
    	printf("The average of the scores, when the max and min are removed is %d", average);
    }
    
    main(){
    	int numbers[7], average;
    	read_file(&numbers);
    	int mean;
    	average = compute(&numbers);
    	show_average(mean);
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Well, I changed the |feof to just feof and now it just returns a nonsensical value.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your read_file looks really broken: you have no way of making sure you don't overwrite the end of the array, which doesn't really come into play since you are putting things who-knows-where (hint: not in the array). This idea of passing a pointer to an array seemed like a good idea for about 0.37 seconds as I was reading it, but it in fact is a very bad idea.

  4. #4
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    Quote Originally Posted by thefinalhero View Post
    Code:
    	fin = fopen("input.txt", "r");
    	while(!feof(fin)) { 
          	/* loop through and store the numbers into the array */
          		fscanf(fin, "%d", scores[i]);
          		i++;
    	}
    	fclose(fin);
    feof() should not be what your while loop tests. Instead, test against the result of fscanf():

    Code:
    while (fscanf(fin, "%d", scores[i])
      i++;
    When fscanf() returns 0, the while loop will end. It will return 0 when it is not able to read anything further from the file. At that point you can use feof() to test whether it reached the end of the file or not with feof(fin). feof() indicates an "error state" and will only return true if this particular error state is true (that is, if fin has reached the end-of-file). There are other error states that could occur, including fscanf() was unable to read an integer (since you are using %d).

    If you know your data file is definitely just numbers, then this should end appropriately at the end of the file, but in a Real Program(TM) you would want to verify why the data stopped being read.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Thanks guys. Made those adjustments and a few others and it worked out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM