Thread: Golfgame

  1. #46
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Looks like you rearranged the wrong parts. You need to slow down. Don't just move a few lines around and hope it comes out right. You might as well put a hundred monkeys at the keyboard and hope they get it right. Programming is more about problem solving than typing. Can you solve this problem yourself with pencil and paper? If the answer is "no", then you sure as hell can't program it. Go through an example by hand, and pay careful attention to each little step you do, taking note of all the details. If you are totaling up the score for a golfer, do you start at 42 or 17? No, you start at 0. Do you write down the total score after every individual number you add? No, you write down the total score when you're all done. When you have mastered the process on paper and pencil, write some pseudo code. Pseudo code should have the same structure as real code, and the right logical flow to solve your problem, but you don't have to worry about syntax and other minutia. When the pseudo code is done, and you've "run it" in your head with some sample data, then you can turn it into real code. You should only write at most 5-10 lines at a time, then compile and test, and fix all errors and warnings before moving on (compile errors and runtime/logical errors).

    Here's pseudo code to show what you did from line 43 on (the indentation shows you which loop the code belongs to):
    Code:
    for each golfer
        you don't initialize score to zero here, so for the second through fourth golfer, you're not starting at zero, and your total score will be off
        for each hole that golfer played
            calculate the breakdown for that golfer on that hole
            add that breakdown to the total score for that golfer
            print the total score so far for that golfer -- you are doing this once per hole, not once per golfer
        close the file -- you are doing this once per golfer, you should be doing it after the loops are all done
        return 0; -- again, you are doing this once per golfer, you should be doing it after the loops are all done

  2. #47
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    int
    Code:
    #include <stdio.h>
    
    
    
    
    int main() {
        FILE *ifp;
        int golfer, totalscore, i, j;
        int hit[4][18];
        int breakdown [18];
        int parscore[18];
    
    
    
    
        //Input File
        ifp = fopen("input.txt","r");
    
    
    
    
        //Player
        fscanf(ifp, "%d\n", &golfer);
        printf("%d golfers played on this golf course:\n\n", golfer);
    
    
    
    
        //Par score
        printf("\n\nTotal Par Score ");
        for (i=0; i < 18; i++)  {
            fscanf(ifp, "%d ", &parscore[i]);
            printf("%d ", parscore[i]);
        }
        printf("\n\n\n");
    
    
    
    
    
    
    
    
        //Golf Scores
        for(j=0; j < golfer; j++){
          putchar('\n');
          printf("Player %d Score: %d\n  ", j+1, breakdown);
          for(i = 0; i < 18; i++) {
            fscanf(ifp, "%d", &hit[j][i]);
            breakdown[j] = hit[j][i]- parscore[i];
            printf("%2d",  breakdown[j]);
    
    
            totalscore += breakdown[j];
          }
        }
    
    
       putchar('\n');
    
    
    
    
        fclose(ifp);
    
    
    
    
        system("PAUSE");
    
    
    
    
        return 0;
    
    
    
    
    }
    This is what I came out with bro. I worked for the last two hours on this, I know Im slow at it and alot ofppl can do it in 5 minutes but I apologize if i cant catch on like the rest. I really appreciate your time and effort man

  3. #48
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Have you run this code? Does the output look like what you want? If not, what output do you get and what output do you expect?

  4. #49
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Yes I have ran the code and no it doesnt look like it but its very close....

    I get:

    Player 1 score: 2686356
    -2 01 0 0 0 1 0 -1 0 1 0 0 1 -1 0 -1 0

    Player 2 score: 2686356
    0 1 2 0 2 -1 1 3 -1 0 1 0 1 1 -2 0 1 0

    and Im suppose to be getting

    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    Player 2 is the winner!

  5. #50
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to zero your totalscore variable just before the loop controlled by i.

    You need to index breakdown with i, not j.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #51
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Honestly, have you paid any attention to what I've said? Go back and re-read my posts #40 and #42, and tell me why on earth you are still trying to print breakdown. Why aren't you heeding the compiler warnings about bad printf parameters? Why do you still think using the name of the array will magically return the sum of it's elements? Then, go and re-read my post #46 and tell my why you are not initializing total for each golfer.

    What variable, out of all the variables in your program do you think contains the total score for each golfer? Why aren't you printing that variable? I'm starting to think you didn't write this code, or you're waiting for me to get fed up and just hand you a solution.

  7. #52
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Its all good man I just did the rest on my own and just hope for the best. Thanks for helping though I apologize for working your nerves God Bless

Popular pages Recent additions subscribe to a feed