Thread: need some help with last part of arrays

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    need some help with last part of arrays

    The last part of this project is that I have to read the numbers from a text file. I've looked at all the posts and FAQ's but they haven't really help me since, I'm not allowed to use sizeof, malloc or anything like that for this project.

    I have to use fscanf to put the list of numbers from the text file. and the size of the array back to main. Part of it is I dont' know what to make with my for loop, the other part i don't know. As I said on my other post the array size is not set, instructor will test it with a random list he has of 1-100 numbers though. When I get the array back to main and the size everything I'll be done. Thank you.

    Code:
    // Compiler Includes
    #include <stdio.h>
    #include <math.h>
    // Project Includes 
    
    // Constants
    #define MAX_ARRAY_SIZE 100
    #define MAX_STRING_LENGTH 80
    
    
    
    // Function Prototypes 
    
    void PrintArray(double inputArray[], int inputArraySize);
    int GetInput( double OutPutList[]);
    double MaxNum(const double inputArray[], int inputArraySize);
    double MinNum(const double inputArray[], int inputArraySize);
    double MenNum(const double inputArray[], int inputArraySize);
    double MedNum(const double inputArray[], int inputArraySize);
    double StdDev(const double inputArray[], int inputArraySize);
    double SumNum(const double inputArray[], int inputArraySize);
    void   SrtNum(const double inputArray[], int inputArraySize);
    void   disply(double max, double min, double mean, double med, double std);
    void   bubblesort(  double a[], int sizer);
    
    //////////////////////////////////////////////////////////////////////////////// 
     
    int main(void) { 
     
     int numlist = 0;
     double testA[MAX_ARRAY_SIZE] = {0};
     double highestNum = 0;
     double lowestNum = 0;
     double MeanNum = 0;
     double MedianNum = 0;
     double StdDevNum = 0;
     double SumNumbers = 0;
    
      numlist = GetInput(testA);
    
      printf("numbers of numbers %f\n", numlist);
    
      highestNum = MaxNum(testA, numlist);
      
      lowestNum = MinNum(testA, numlist);
    
      MeanNum = MenNum(testA, numlist);
    
      MedianNum = MedNum(testA, numlist);
    
      StdDevNum = StdDev(testA, numlist);
    
      disply(highestNum, lowestNum, MeanNum, MedianNum, StdDevNum);
    
    return 0;
    
    } // end main
    
    int GetInput(double OutPutList[]) {
    
    	int numbersFound = 0;
    	int value = 0;
    	char inputFileName[MAX_STRING_LENGTH] = {'\0'};
    	FILE *inputFileHandle = NULL;   
    	double currentNumber[] = {0};
    	int  endOfFile = 0;
    	int i = 0;
    
    	//begin function
    
      printf("Enter file name: ");
      
      gets(inputFileName);
    
      inputFileHandle = fopen(inputFileName, "r");
    
    	if (inputFileHandle == NULL) {
      
    		printf("ERROR: The file %s could not be found!\n", inputFileName);
    	
    	} // end if
    	else {
    
    		endOfFile = fscanf(inputFileHandle, "%lf", &currentNumber[value]);
    	// read text numbers put them into array
    
    // having horrible time here figuring out why my array won't build
    			while (endOfFile != EOF) {
                                for (i = 0; i < ?????;  i++) {
    			    OutPutList[value] = fscanf(inputFileHandle,"%lf", &currentNumber[value]);
    			numbersFound++; // increase number counter
    			} // end for loop
    
    		endOfFile = fscanf(inputFileHandle, "%lf", &currentNumber[value]);
        } // end while
    
        // Close input file
        fclose(inputFileHandle);
    
      } // end else
    
    return numbersFound;
    } // end function
    
    
    double MaxNum(const double inputArray[], int inputArraySize) {
    
    	int	i = 0;
    	double max = inputArray[0]; // assume max number
    
    	for(i = 0; i < inputArraySize; i++) { // for loop brace
    
    		if (inputArray[i] > max) { // if brace 	
    			max = inputArray[i];
    		} // if loop end
    	} // for loop end
    return max;
    } // end function MaxNum
    
    
    double MinNum(const double inputArray[], int inputArraySize) {
    
    	int	i = 0;
    	double min = inputArray[0]; // assume max number
    
    		for(i = 0; i < inputArraySize; i++) { // for loop brace
    		
    			if (inputArray[i] < min) { // if brace 	
    			min = inputArray[i];
    			} // if loop end
    		} // for loop end
    return min;
    } // end function MinNum
    
    double SumNum (const double inputArray[], int inputArraySize) {
    
    	int i = 0;
    	double sum = 0;
    
    		for(i = 0; i < inputArraySize; i++) {
    			sum = inputArray[i]+ sum;
    		}
    return sum;
    } // end function SumNum
    
    
    double MenNum(const double inputArray[], int inputArraySize) {
    	//int	i = 0;
    	double mean = 0; // assume max number
        double total = 0;
    
    		total = SumNum(inputArray, inputArraySize);
    		mean = total/inputArraySize;
    
    return mean;
    } // end function MenNum
    
    void bubblesort( double a[], int sizer) {
    	int pass;
    	int j;
    	double hold;
    
    	for(pass = 1; pass < sizer; pass++) {
    
    		for (j = 0; j < sizer-1; j++) {
    
    			if (a[j] > a[j + 1]) {
    				
    				hold = a[j];
    
    				a[j] = a[j + 1];
    				a[j + 1] = hold;
    			} // end if
    
    		} // end inner for
    	} // end outer for
    } // end bubble sort function
    
    
    double MedNum( const double inputArray[], int inputArraySize) {
    
    	double medianNum = 0;
    	double bubbleArray[MAX_ARRAY_SIZE] = {0};
    	int k = 0;
    
    		for(k = 0; k < inputArraySize; k++) {
    			bubbleArray[k] = inputArray[k];
    		}	
    
    			bubblesort(bubbleArray, inputArraySize); 
    
    				if(inputArraySize%2==0)	{ // if array size modulated by 2 leaves no remainder
    									  // array size is even
    					medianNum = (bubbleArray[(inputArraySize)/2] + bubbleArray[inputArraySize/2-1])/2;
    				} // end if
    				else // else array size is odd
    				{
    					medianNum = bubbleArray[inputArraySize/2];
    				} // end else
               
    
    return medianNum; // return the median number
    } // end function MedNum
    
     
    double StdDev(const double inputArray[], int inputArraySize) {
    
    	int	i = 0;
    	double mean = 0;
    	double std = 0;
    	double x = 0;
    
    		mean = MenNum(inputArray, inputArraySize);
    
    			for (i = 0; i < inputArraySize; i++) {
    				x += (inputArray[i]-mean)*(inputArray[i]-mean);
    			}
    			x = x/(inputArraySize);
    			std = sqrt(x);
    return std;
    } // end function StdDev
    
    
    
    void disply(double max, double min, double mean, double med, double std) {
    
    	printf("The higest value is: %f\n", max);
    	printf("The lowest value is: %f\n", min);
    	printf("The mean value is: %f\n", mean);
    	printf("The median value is: %f\n", med);
    	printf("The standard deviation is: %f\n", std);
    
    } // end function disply
    
    
    void PrintArray(double inputArray[], int inputArraySize) { 
     
    	int i = 0;
     
    		for (i = 0; i < inputArraySize; i++) {
    		printf("The numbers in the array are: %f\n", inputArray[i]);
    		} 
     } // end function PrintArray
    Last edited by Lince; 11-17-2006 at 10:59 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Horrible alignment. If you want somebody else understand your code - fix it.
    2.
    Code:
     printf("numbers of numbers %f\n", numlist);
    numlist is int - use correct format
    3. fscanf returns number of items read - not check it with EOF constant
    ...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    PS.
    4. don't use gets (see FAQ) - replace by fgets
    5. check return value of fgets
    6. MedNum - there is no check that the arraySize is more than 0
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    This can be done using a while loop in which you read one integer at a time (until EOF). Also, at every iteration, you increment a variable, say length, by one. So then when your loop is done you have the length of your array.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  2. Suspicious Pointer Conversion
    By mr_spanky202 in forum C Programming
    Replies: 35
    Last Post: 04-11-2003, 12:35 PM
  3. OpenGL drawing w/ Arrays or not?
    By tegwin in forum Game Programming
    Replies: 2
    Last Post: 01-14-2003, 03:31 PM
  4. arrays arrays arrays....
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2002, 07:16 AM