Like Tree1Likes
  • 1 Post By Tclausex

Stuck on this kinda long code

This is a discussion on Stuck on this kinda long code within the C Programming forums, part of the General Programming Boards category; Hello. I've been working on this code that simulates a fishing tournament. There's 14 teams (although there is no "team ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    31

    Stuck on this kinda long code

    Hello. I've been working on this code that simulates a fishing tournament. There's 14 teams (although there is no "team 14", goes from team 13 to team 15), each team picks 5 fish, and the program reads in the team number, the length, weight, and if the fish is dead or alive for each fish. I figured out a lot of stuff, like the total weight of each team, etc. However, I'm having problems on two things, and that is the penalties I need to calculate in. Here they are:

    Dead penalty
    1 Dead = 0.25 lbs
    2 Dead = .625 lbs
    3 Dead = 1.125 lbs
    4 Dead = 1.75 lbs
    5 Dead = 2.5 lbs

    Length Penalty
    1 lb subtracted for each fish under minimum length
    Minimum length is 7 inches

    I'm guessing I should've figured out the penalties before I calculated all the weights, because now I'm just confused on where to even begin. I need to subtract the weight penalties from all the weight calculations. Anyways, here's my code, any help would be greatly appreciated!

    Code:
    #include <stdio.h>
    
    //prototypes
    void greeting();
    void readfile();
    void displayResults(double avglength, double avgweight, double heaviest, double longest, int frequency, int heavyteam, int longteam);
    void readfile(int team[], double length[], double weight[], int deadfish[]);
    
    
    int main()
    {
            int team[70];
            double length[70];
            double weight[70];
            int deadfish[70];
            double sumlength = 0;
            double sumweight = 0;
            int frequency = 0;
            double heaviest = 0;
            double longest = 0;
            double sum_weight_team[15];
            int longteam;
            int heavyteam;
    
    
            greeting(); //calls method 'greeting'
    
    
            int i;
            int j;
    
    
            for(i=0; i<70; i++)
            {
                    team[i], length[i], weight[i], deadfish[i];
            }
    
    
                    readfile(team, length, weight, deadfish);
    
    
            for(i=0; i<70; i++)
            {sumlength += length[i];} //Gets the sum of the length of all the fish.
            double avglength = sumlength/70; //Gets the average length of all the fish.
    
    
            for(i=0; i<70; i++)
            {sumweight += weight[i];} //Gets the sum of the weight of all the fish.
            double avgweight = sumweight/70; //Gets the average weight of all the fish.
    
    
            for(i=0; i<70; i++)
            {
            if(deadfish[i] == 0) //Finds the number of dead fish (dead fish = 0).
                    {frequency++;}
            }
    
    
            for(i=0; i<70; i++)
            {
            if(weight[i]>heaviest) {heaviest=weight[i];
            heavyteam = team[i]; }
            }
    
    
    
            for(i=0; i<70; i++)
            {
            if(length[i]>longest) {longest=length[i];
            longteam = team[i]; }
            }
    
    
            for(j=0; j<70; j++)
            {
                    sum_weight_team[team[j]] += weight[j];
            }
    
    
            for(j=1; j<16; j++)
            {
                    if(j == 14) continue;
                    printf("The total weight of team %d is: %lf.\n", j, sum_weight_team[j]);
            }
    
    
            displayResults(avglength, avgweight, heaviest, longest, frequency, heavyteam, longteam);
    
    
            return 0;
    }
    
    
    void greeting()
    {
            printf("\n");
    }
    
    
    void readfile(int team[], double length[], double weight[], int deadfish[])
    {
            FILE *infile = fopen("input.txt", "r");
            if(infile !=NULL)
            {
            int i = 0;
            while(fscanf(infile, "%d %lf %lf %d", &team[i], &length[i], &weight[i], &deadfish[i])!=EOF)
            {
                    i++;
            }
            fclose(infile);
            }
    
         
    
    
            else {printf("The file was not opened successfully.\n");}
    }
    
    
    
    
    void displayResults(double avglength, double avgweight, double heaviest, double longest, int frequency, int heavyteam, int longteam)
    {
            printf("The average length overall of all the fish is %.4lf and the average weight overall is %.4lf.\n", avglength, avgweight);
            printf("The number of dead fish overall is %d.\n", frequency);
    
    
    
    
            printf("The heaviest fish is %lf caught by team %d.\n", heaviest, heavyteam);
            printf("The longest is %lf caught by team %d.\n", longest, longteam);
    }


  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    
    double sum_weight_team[15];
    ...
    for(j=1; j<16; j++)         
    { 
        if(j == 14) continue;
        printf("The total weight of team %d is: %lf.\n", j, sum_weight_team[j]);
    }
    


    See a problem?

    It's a bad idea to code your array sizes everywhere. Not only are you prone to error, but what if your boss tells you the arrays need to be twice the size due to a design change? That's a lot of hunting and editing. Use define for array dimensions.
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    31
    Ohhhhhhh alright. Yeah I see. I figured it out. Thank you for the tip!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-06-2011, 12:04 AM
  2. Replies: 1
    Last Post: 06-28-2011, 05:27 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. kinda stuck.. help please?
    By w0nger in forum C Programming
    Replies: 26
    Last Post: 11-22-2008, 05:11 AM
  5. Replies: 6
    Last Post: 11-14-2005, 05:23 AM

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