Need help finishing a few functions.

This is a discussion on Need help finishing a few functions. within the C Programming forums, part of the General Programming Boards category; Hello I am a beginner with C programming and just now getting to conditionals. I have my entire program shelled ...

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

    Need help finishing a few functions.

    Hello I am a beginner with C programming and just now getting to conditionals. I have my entire program shelled out but many of the functions I have no clue how to write out and implement can anyone help?

    Here is an example run through of the program.

    "Enter the positive mass for 3 particles: "(user input)"4.5 1.2 18.0""
    Max mass: 18.000
    Mean mass: 7.900
    Mass at 0: 4.500
    Mass at 1: 1.200
    Mass at 2: 18.000"

    Here is my program so far. Apologies for any spacing errors copying and pasting . Thanks in advance for the help.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #define TRUE 1
    #define FALSE 0
    
    double maxOf3(double* numbers);
    
    double meanOf3(double* numbers);
    
    int validateAll(double* masses);
    
    int validateMass(double* masses, int index);
    
    void writeMass(double* masses, int index);
    
    int main(void){
    
      double masses[3];
    
      printf("Enter the positive mass for 3 particles: ");
      scanf("%lf%lf%lf", &masses[0], &masses[1], &masses[2]);
    
      if (!validateAll(masses)) {
    
         return EXIT_FAILURE;
      }
    
      printf("Max mass: %.3lf\n", maxOf3(masses));
      printf("Mean mass: %.3lf\n", meanOf3(masses));
    
      writeMass(masses, 0);
      writeMass(masses, 1);
      writeMass(masses, 2);
    
      return EXIT_SUCCESS;
    
    }
    
    int validateAll(double* masses){
        return EXIT_FAILURE;
      }
    
      printf("Max mass: %.3lf\n", maxOf3(masses));
      printf("Mean mass: %.3lf\n", meanOf3(masses));
    
      writeMass(masses, 0);
      writeMass(masses, 1);
      writeMass(masses, 2);
    
      return EXIT_SUCCESS;
    
    }
    
    int validateAll(double* masses){
    
       int rvalue = 0;
    
       rvalue = rvalue + validateMass(masses,0);
       rvalue = rvalue + validateMass(masses,1);
       rvalue = rvalue + validateMass(masses,2);
    
       if ( rvalue >= 3){
          return TRUE;
       }
    
       else {
          return FALSE;
       }
    }
    
    int validateMass(double* masses, int index){
    
       if(masses[index] > 0){
          return TRUE;
       }
    }
    void writeMass(double* masses, int index){
    
    }
    
    double maxOf3(double* numbers){
    
    }
    
    double meanOf3(double* numbers){
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    This definitely smells like homework. Give it a shot first and tell us if you get stuck on something specific.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Oh I've done whats there already but don't really know how to start the others =\ . And yup its homework but I thought asking questions about it are allowed. I don't want anyone to do it for me but instead maybe give me an idea of how one of the empty functions I have are supposed to be coded. Was sick last week and missed most of the lesson on this so I am really clueless here lol

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Mo777 = Mr.777?

    Is Mr.777 what you log in with when you act like you know everything, and Mo777 is what you log in with when you need help?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Lol no I really know nothing about this. And Mo is short for Morgan not Mr.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    I have a hard time believing you even tried. If, as you asserted, you actually wrote that main function, and both validateAll functions (and the garbage code in between the two validateAll functions), you have more than enough skills to code writeMass at the very least. Also, think about how you would determine the others if you had to do it by hand. How do you figure out the largest of 3 values step by step? What about the mean?

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    I wrote that with the help of a teacher. And I've been absent for a week so things have slipped my mind. Why waste both of our time to come in here to insult my work and imply that I am lying? I can tell your last questions are there to help me think about it but I don't know the ways to tell the program to run it.

    Sorry if I seem like a slacker just thought a C programming board could.. well you know help me with C programming....

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what do you need help with? We aren't here to do it all for you. We're not a "give us your code or design specs, we'll do it all for you!" site. Tell us exactly what you are having problems with, and we'll help. This isn't "fill in the blanks" programming.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Okay, so the writeMass function is going to use a printf statement, probably not too different from those you used in your main function. Then describe for me in English how you would determine the maximum and mean values for 3 numbers.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    I didn't expect you to be and I thought I made myself clear I am having trouble starting my unfinished functions. I have no good idea how to start them...

    Ok so printf. And the maximum would of course be the highest of the 3 numbers listed while the mean would be the sum of the 3 numbers divided by 3. How do I put that into C?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Mo777 View Post
    And the maximum would of course be the highest of the 3 numbers listed while the mean would be the sum of the 3 numbers divided by 3. How do I put that into C?
    No. That would be the average.

    (X + Y + Z) / 3 = Average of X Y and Z, not the highest value.

    You need to be able to write each step out in your own words before you even try to put it into code. Or at least, be able to express in words what you want done as a summary first, then break that down into small steps, until you have a list of the smallest steps possible to get the job done.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    So you have some idea of how to solve your program in general. Now, you need to know how to compare numbers, add them and divide them in C. There's a brief tutorial here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy. You also need to read your textbook from class, and get class notes for the lectures you missed from a friend/classmate.

    Quote Originally Posted by quzah View Post
    No. That would be the average.
    The mean is the average (well, one type of average if you're being picky). It's the sum of all data divided by the number of data points, and it's one of the functions Mo777 needs to write. Maximum number is another function that needs to be written. Both were described, albeit briefly, in that sentence you quoted.
    Last edited by anduril462; 03-09-2011 at 04:38 PM.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    I thought the mean is the average....

    No book(professor is weird) or classmates that I have spoken to but that link looks helpful thank you. Sorry if I seemed lazy and just looking for someone to code it for me in my earlier post but I am very new to this and honestly have no clue how to start it. I will look into that link and hopefully find some info to get started. Thanks again.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    Both were described, albeit briefly, in that sentence you quoted.
    Ah. I read it all together, as a single sentence: "the highest would be x+y+z/3"


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Yes, mean is the average. You'll learn a lot more by reading and doing rather than by copy-pasting, so dig around the web for C tutorials, and get yourself a good book, even if the class doesn't require it. We have some recommendations here: C Book Recommendations. As a hint, Quzah's mathematical description of average in post #11 is remarkably close to how it will look in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM

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