0

I wrote this program for my C class. It basically takes the user to a horse track, display odds on different horses and allows the user to make a bet. Originally, my instructor only wanted us to write the results to a text or binary file, so the past results could be viewed whenever the user wanted.

He recently told us that he wants us to include bubble sort to group the horses in order i.e. horse 1, horse 1, horse 1, horse 1, horse 2, horse 2...etc.

I am sure I can figure out the bubble sort using strcmp(), but he also wants us to display how many times that horse has won the race in the past.

My question is: will I be able to make such a display dealing only with char/string arrays? I don't want to spend my next four hours building a solution that cannot work.

Thanks in advance,

p.s. Here is the function for that part of the program.

void viewWinners() {
    FILE *zacksTrackStats;

    char horses[MAX_SIZE] = {0};        

    if ((zacksTrackStats = fopen("zacksTrackStats.txt", "r")) == NULL)
    {
        perror ("error");
        exit (0);
    }

    while (fgets(horses, sizeof(horses), zacksTrackStats) != NULL)
    {
        printf ("%s", horses);
    }

    fclose(zacksTrackStats);
    pause;
}
1
  • 1
    It's software - you can do almost anything. Do you have a more specific question? Commented Jan 30, 2013 at 21:55

3 Answers 3

1

Of course you can. Are the actual names "horse 1" and "horse 2"? If so you can just store each horse's data in an integer array. If not then you have to make a lookup table. Store information for how many times each horse has won and print the result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Does it matter how the data is written to a text file or can I manipulate the data however I want when I read from the file?
Just manipulate the data however you want when you read from the file. The user isn't supposed to know how you store your data. The important thing from the user side is that the information is displayed the way that they want to see it.
1

It is entirely possible to translate txt to numbers and vice versa.

Check out this older post: Converting string to integer C

Comments

1

Yes you can. To manipulate data from the file you can use fscanf (or sscanf).

sscanf(char *source, format, &dest, ...)

For example :

int occurences[NUMBER_OF_HORSES_MAX];
int count = 0;
int temp = 0;
int i;

for(i = 0; i < NUMBER_OF_HORSES_MAX; ++i)
{
    occurences[i] = 0;
}

while (fgets(horses, sizeof(horses), zacksTrackStats) != NULL)
{
    sscanf(horses, "%d", &temp);
    occurences[temp] += 1;

    printf ("Current horse : %s", horses);

    count++;
}

for(i = 0; i < count; ++i)
{
    printf("Horse %d has won %d times\n", i, occurences[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.