Help beginner with program please

This is a discussion on Help beginner with program please within the C Programming forums, part of the General Programming Boards category; I need help writing the mean and validateAll functions. Any help would be greatly appreciated. Code: #include <stdio.h> /*for printf, ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Help beginner with program please

    I need help writing the mean and validateAll functions. Any help would be greatly appreciated.
    Code:
    #include <stdio.h> /*for printf, scanf*/
    #include <stdlib.h> /*for EXIT_SUCCESS*/
    #define TRUE 1 /*Allows us to use TRUE in place of 1*/
    #define FALSE 0 /*Allows us to use FALSE in place of 0*/
    #define PARTICLE_COUNT 3
    
    /*Returns the maximum value of the array of numbers with the
    	given length. If the length is 0,the result is unspecified */
    double max(double* numbers, int length);
    
    /*Returns the mean value of the array of numbers with the given
    	length. If the length is 0, the result is unspecified. The mean
    	is often called the average. It is the sum of numbers divided
    	by how many there are.*/
    double mean(double* numbers, int length);
    
    /*Assumes PARTICLE_COUNT masses, and validates all of them by calling
    	validateMass for each one. If any of the masses is invalid, 
    	validateAll returns 0 (false). If all of them are valid, 
    	validateAll returns 1 (true)*/
    int validateAll(double* masses);
    
    /*Validates the mass at the given indiex. The mass is valid if
    	it is postive (neither zero nor negative). If it is
    	invalid, print a message saying which index is the bad value.
    	If the mass is is invalid, validateMass returns 0 (false).
    	If valid, it returns 1 (true).*/
    int validateMass(double* masses, int index);
    
    /*Prints the index and the mass at that index*/
    void writeMass(double* masses, int index);
    
    /**
     *Reads three masses from the user, validates, them, and displays the
     *max and mean mass as well as repeating the three values back to the
     *user.
     */
    int main(void){
        /*Stores the masses for 3 particles.*/
    	double masses[PARTICLE_COUNT];
    	int index;
    /*Input.*/	
    printf("Enter the positive mass for 3 particles: ");
    
    for(index=0;index < PARTICLE_COUNT; index++){
    	scanf("%lf", &masses[index]);
    }
    /*Validation.*/
    if(!validateAll(masses)){
    	return EXIT_FAILURE;
    }
    
    /*Max and mean.*/
    printf("Max mass: %.3lf\n", maxOf3(masses));
    printf("Mean mass: %.3lf\n", meanOf3(masses));
    
    /*Output the masses themselves.*/
    for (index = 0; index < PARTICLE_COUNT; index++){
    	writeMass(masses, PARTICLE_COUNT);
    }
    
    
    /*No errors.*/
    return EXIT_SUCCESS;
    }
    
    
    double max(double* numbers, int length){
       int n;
    	if (length == 0){
    		return 0.0;
    	}
       double maxNumber = numbers[0];
    	for (n = 1; n < length; n++){
    		if (numbers[n] > maxNumber){
    			maxNumber = numbers[n];
    		}
    	}
       return maxNumber ;
    }
    
    /*Adds the numbers together and divides by 3,
    	returning the mean*/
    double mean(double* numbers, int length){
        double sum;
    	
    
    	for (numbers =0;numbers < PARTICLE_COUNT; numbers++){
    	sum= sum+ numbers[index];
    	}
        return sum/PARTICLE_COUNT;
    }
    
    /*Checks to see if all three masses are positive by
    	using validateMass. A positve number will be given
    	a value of 1. If all three numbers given are positive,
    	the values will add up to three and the function
    	will return true. If not, it will return false.*/
    int validateAll(double* masses){
        int allValid = TRUE;
    	int n;
    	for (masses=0; masses < PARTICLE_COUNT; masses++){
    		if(!validateMass(masses[n])){
    			allValid = FALSE;
    		}	
        }
    
    /*Checks if the individual masses are positive. If any 
    	of the masses are not positive, a message staing 
    	which masses are not positive is written*/
    int validateMass(double* masses, int index){
        if(masses[index] > 0){
            return TRUE;
        }
        else {
            printf("Mass at %d not positive: %lf \n", index, masses[index]);
            return FALSE;
        }
    }
    
    /*Prints the masses in order*/
    void writeMass(double* masses, int index){
        printf("Mass at %d: %lf \n", index, masses[index]);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You will be passing an array (which in C is the simple name of the array, serving as a constant pointer to it's zeroth element), to the mean function, and the length of the array, so you now can easily make a for loop to sum up all the values, thereof.

    Code:
    double sum= 0.0;
    for(i=0;i<length;i++) {
      sum += array[i];
    }
    return (sum/length);

    and you'll be returning mean, so the function that called this one, will need to "catch" the return value:

    So in the calling function:
    Code:
    mean = getMean(arrayName, length);
    The above is not a runnable function, but code to lead you toward creating your own runnable function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-13-2010, 11:49 PM
  2. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21