Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cronker01
    New Member
    • Mar 2008
    • 4

    Functions

    for my assignment I was told I have to use a function at one point. The one I put in doesn't work properly. Any help?
    [code=cpp]
    //variable declaration
    float score[7]; //declares an erray of 8 float values
    int s=0;
    float sum=0;
    int i = 0;
    int n=0;
    int position = 0;
    float Max = 0;
    for (i=0; i<=7; i++){
    cout << "Please enter each one of the eight judges scores and press enter after each one" << end$
    cin >> score[i];
    }
    float Min = score[0];
    for (n=1; n<=7; n++) {
    if (Min> score[n]){
    Min = score[n];
    position = n;
    }
    }
    cout << fixed<< showpoint << setprecision(2) << endl;
    cout <<"Min number is " << Min << endl;
    Max = score[0];
    position = 0;

    for (n=1; n<=7; n++){
    if (Max < score[n]) {
    Max = score[n];
    position = n;

    }
    }
    cout <<"Max number is " << Max << endl;
    for (s=0; s<=7; s++)
    {
    sum = sum + score[s];
    }
    cout <<"Original sum " << sum << endl;
    sum = sum - Min - Max;
    cout << fixed<< showpoint << setprecision(2) << endl;
    cout << "The point received by the diver are: " << score[2] << endl;

    [/code]
    Last edited by sicarie; Mar 23 '08, 02:52 AM. Reason: Code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    cronker01-

    We have a policy against posting full code, so I removed some stuff.

    Um, what was your assignment? Your function was pretty much useless - you could do that just as easily without.

    The rule for functions is any repeatable task should be turned into a function. Do you get an average frequently? Don't spend time typing in the * wildcard and equals, just call a function to do it. Generally a single print line is not what you call a function for - especially if you only call it once.

    So is there any repeatable task in your program? Is there a task that could be considered useful to make repeatable?

    Comment

    • cronker01
      New Member
      • Mar 2008
      • 4

      #3
      Originally posted by sicarie
      cronker01-

      We have a policy against posting full code, so I removed some stuff.

      Um, what was your assignment? Your function was pretty much useless - you could do that just as easily without.

      The rule for functions is any repeatable task should be turned into a function. Do you get an average frequently? Don't spend time typing in the * wildcard and equals, just call a function to do it. Generally a single print line is not what you call a function for - especially if you only call it once.

      So is there any repeatable task in your program? Is there a task that could be considered useful to make repeatable?
      This assignment gives you 8 judges scores and you take the one highest and one lowest scores out and average the remianing 6. I did the program without a function and it worked fine. but part of the assignment is to add a function in so I just tried adding one later but the sum value is not right at the end.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by cronker01
        This assignment gives you 8 judges scores and you take the one highest and one lowest scores out and average the remianing 6. I did the program without a function and it worked fine. but part of the assignment is to add a function in so I just tried adding one later but the sum value is not right at the end.
        Okay, so I would recommend using a function to determine the average. You can pass it an array, or you can pass it all 6 scores, and return the average.

        Does that make sense? Can you see how that would be done?

        Comment

        • cronker01
          New Member
          • Mar 2008
          • 4

          #5
          Originally posted by sicarie
          Okay, so I would recommend using a function to determine the average. You can pass it an array, or you can pass it all 6 scores, and return the average.

          Does that make sense? Can you see how that would be done?
          It does make sense but I'm not sure how to do the code.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            What part are you having trouble with? You said you had the first part - where you accepted the input, computed the average, and printed it out. Just move the 'average' code into a function.

            You will have to decide - probably based on your input, but you could do it any way you wanted - how you will pass that information, but the only code that will change is the call to the function instead of computing the average.

            Comment

            • cronker01
              New Member
              • Mar 2008
              • 4

              #7
              Originally posted by sicarie
              What part are you having trouble with? You said you had the first part - where you accepted the input, computed the average, and printed it out. Just move the 'average' code into a function.

              You will have to decide - probably based on your input, but you could do it any way you wanted - how you will pass that information, but the only code that will change is the call to the function instead of computing the average.
              I think I got it. Don't really know how but it still work. Thanks for taking a look though. I'm working on another question now that I'm having more problems with. It's forming an answer key for a true false quiz.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by cronker01
                I think I got it. Don't really know how but it still work. Thanks for taking a look though. I'm working on another question now that I'm having more problems with. It's forming an answer key for a true false quiz.
                Well, if you post what you have, even if you don't think its right, we can help you refine it. Your compiler, as well, will point you to the line number your errors are occurring on, and most compiler error messages can be Google searched to reveal a wealth of information.

                For your question on creating an answer key for a T/F quiz, please start a new thread with you question and the corresponding code you are having difficulties with.

                Comment

                Working...