Thread: Can someone please help me with my program? I am new to C programming.

  1. #31
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    okay
    Last edited by matthayzon89; 02-28-2010 at 08:35 PM.

  2. #32
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Adak any ideas?

    I tried changing my code a bit....This is as close as I got but i am still far from where I need to be


    Code:
    for(n=1; n <= ttl_golfers; n++){
        for(i = 0; i < 18; i++) {
          fscanf(ifp, "%d", &tries[n][i]);
          score[n][i] = tries[n][i]- parscore[i];
        }
        printf("Golfer#%d's score:%d\n\n", n, score[n][j]);
        }
    Last edited by matthayzon89; 02-28-2010 at 08:44 PM.

  3. #33
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Please someone help, I am completely all out of ideas....

    Can someone just help me understand how to make the output of my program output like this:

    Golfer #1: 1 1 3 4 2 3 4 2 5 -2 1 -2 -3(upto 18 holes)
    Golfer #2:.......................... (upto 18 holes)
    ...

    This is my code up until now:

    Code:
    #include <stdio.h>
    
    int main() {
        FILE *ifp;
        int ttl_golfers, totalscore, ok, j, i, z, n, p, o;
        int tries [9][18];
        int score [9][18];
        int parscore[18];
    
        ifp = fopen("golf.txt","r");
    
        fscanf(ifp, "%d\n", &ttl_golfers);
        printf("%d golfers played on this golf course:\n\n", ttl_golfers);
    
        for (z=0; z < 18; z++)  {
            fscanf(ifp, "%d ", &parscore[z]);
        }
    
        for(n=1; n <= ttl_golfers; n++){
        for(i = 0; i < 18; i++) {
          fscanf(ifp, "%d", &tries[n][i]);
          score[n][i] = tries[n][i]- parscore[i];
        }
        printf("Golfer#%d's score:%d\n\n", n, score[n][i]);
        }
    
        fclose(ifp);
    
    system("PAUSE");
    return 0;
    
    }

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm touching it up, now.

    This is a substantial touch up, so be sure and copy it completely!

    I did not check the scores for accuracy.

    Code:
    /*
    
    Please someone help, I am completely all out of ideas....
    
    Can someone just help me understand how to make the output of my program output like this:
    
    Golfer #1: 1 1 3 4 2 3 4 2 5 -2 1 -2 -3(upto 18 holes)
    Golfer #2:.......................... (upto 18 holes)
    ...
    
    */
    #include <stdio.h>
    
    int main() {
        FILE *ifp;
        int ttl_golfers, totalscore, i, n;
        int tries [9][18];
        int score [9][18];
        int parscore[18];
    
        ifp = fopen("golf.txt","rt");
    
        fscanf(ifp, "%d\n", &ttl_golfers);
        printf("%d golfers played on this golf course:\n\n", ttl_golfers);
    
        printf("\n\n\nPar is: ");
        for (i=0; i < 18; i++)  {
            fscanf(ifp, "%d ", &parscore[i]);
            printf("%d ", parscore[i]);
        }
        printf("\n\n              Scores Compared with Par\n");
        for(i = 0; i < 80; i++)
          putchar(196);
        printf(" Player  Hole: ");
        printf("1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18\n");
        for(i = 0; i < 80; i++)
          putchar(196);
        for(n=0; n < ttl_golfers; n++){
          if(n) putchar('\n');
          printf("Golfer#%d's:  ", n+1);
          for(i = 0; i < 18; i++) {
            fscanf(ifp, "%d", &tries[n][i]);
            score[n][i] = tries[n][i]- parscore[i];
            printf("%3d",  score[n][i]);
          }
        }
        putchar('\n');
    
        fclose(ifp);
    
        system("PAUSE");
         
        return 0;
    
    }
    Last edited by Adak; 02-28-2010 at 10:46 PM.

  5. #35
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    awesome, thank you.

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome, Matt. Don't expect it so easy next time. I will require more productive work from *you*, and please, no more double posting, on the same exact problem. That will cause most of us to ignore you, and if it continues, the mod will close the thread.

    Char 196 is an ascii char that is usually used to draw horizontal lines (like the top or bottom of a box or window border). It makes the output look much better than it would with just hyphens, instead

  7. #37
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Adak, THANK YOU! That was wayyy more than I needed, thank you~! your really great.

    But, I really was not looking for an easy solution, I was working on this program since this morning, I was trying different loops for hoursss! I was just looking for advice and help with segments of my code. Thank you adak


    Also, I double posted because it is similar than the last problem but the code is completely different, so I thought maybe it made sense to start on new thread, but I guess not..

    I didn't mean to double post.

    Take care!
    Thank You

  8. #38
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    This is the "simple" version of my new code. It works perfectly and prints perfectly for 8 golfers. The problem is I need it to work for up to 100 golfers and whenever I change the data in my golf.txt file to have more golfers it does not work correctly, why is this? The logic is set up in a way to where it should work no matter what....

    Code:
    #include <stdio.h>
    
    int main() {
        FILE *ifp;
        int ttl_golfers, totalscore, i, n;
        int tries [9][18];
        int score [9][18];
        int parscore[18];
    
        ifp = fopen("golf.txt","r");
    
        fscanf(ifp, "%d\n", &ttl_golfers);
        printf("\n%d golfers played on this golf course:\n-------------------------------------\n", ttl_golfers);
    
        printf("\n\nPar is: ");
        for (i=0; i < 18; i++)  {
            fscanf(ifp, "%d ", &parscore[i]);
            printf("%d ", parscore[i]);
        }
        printf("\n\n");
        for(n=0; n < ttl_golfers; n++){
          if(n) putchar('\n');
          printf("\nGolfer #%d's:  ", n+1);
          for(i = 0; i < 18; i++) {
            fscanf(ifp, "%d", &tries[n][i]);
            score[n][i] = tries[n][i]- parscore[i];
            printf("%3d",  score[n][i]);
          }
        }
    
        fclose(ifp);
    
        getch();
        system("PAUSE");
        return 0;
    
    }

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The arrays tries and scores don't have enough rows for more golfers. For right now, I'd just make these two arrays with 125 rows: tries[125][18], etc.

    Later, you'll want to learn to malloc the number of rows that you need, by reading the file data first, and THEN setting up the number of rows, to match that number.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matthayzon89
    What exactly is a buffer?
    I do not think anyone has actually answered this directly, but you can check the FOLDOC entry for buffer. With respect to nonpuz's statement, it just means "an array of characters".

    For an alternative definition, see buffer
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thank you everyone, you guys are very helpful.

  12. #42
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I was wondering, I talked to one of my friends and they said i would be able to use two 1d arrays for this problem if i want to, is that really possible? and is my method probably the best way to go? why? I would just like to understand when it is smart to use 1d vs. 2d arrays [coming from people who have experiences with them]

  13. #43
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Matt, You could use a 1-d array of structs (though each struct would have an array inside).
    Or I guess you could use a 1-d array of length 18xN (This is really what a 2-d array is, but the compiler simplifies the syntax for you).

    The array of structs is probably the best route to go. Structs help to keep your data organized and reduce the possibility of making mistakes such as transposing the array indices (eg writing [j][i] instead of [i][j]).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matthayzon89 View Post
    I was wondering, I talked to one of my friends and they said i would be able to use two 1d arrays for this problem if i want to, is that really possible? and is my method probably the best way to go? why? I would just like to understand when it is smart to use 1d vs. 2d arrays [coming from people who have experiences with them]
    For what you've done so far (and no more), you could get by with just one 1D array of 18 elements, for the par scores. Everything else can be generated just as it's needed. The problem with that "minimal" type of program design, is that you're stuffed if you ever want to do anything more with the data. Like go back and correct a score - that would be a major pita, with this kind of design.

    Moving up, you could use just one 2D array and have the rows organized:

    row 1 is for the par scores,
    row 2 for the tries of each golfer
    row 3 is the 'real' scores of each golfer

    In choosing your data structures, use the one that fits best, that makes it really clear what you're doing and, of course, is also efficient. In your case, the design is very clear, and obviously efficient.

    In the case of interleaving rows I mention just above, the efficiency is good, but the clarity takes a step backward. If you wanted to look at the scores for golfer #7, would you know right off the bat, what row to look at?

    If you said "14", right away, then it might be good for you to use it. If not, I wouldn't use it. The run time of this program is very short, and most of that, depends on the time it takes to read data from the file. So any change in data structure for better efficiency, is going to be a waste of time. If you want a very slight increase in speed, then get a faster hard drive.

    The keyboarder entering the data - there's the real bottleneck.
    Last edited by Adak; 03-01-2010 at 01:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM