Pulling numbers from a .txt file and displaying certain things (averages, etc.)

This is a discussion on Pulling numbers from a .txt file and displaying certain things (averages, etc.) within the C Programming forums, part of the General Programming Boards category; Hello. I'm gonna try and explain this the best I can. Pretty much I'm working on something that simulates a ...

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

    Pulling numbers from a .txt file and displaying certain things (averages, etc.)

    Hello. I'm gonna try and explain this the best I can. Pretty much I'm working on something that simulates a fishing tournament. I made a .txt file with four columns. Column 1 is team number, column 2 is length, column 3 is weight, and column 4 is number of dead fish.

    There's 70 fish total, 5 per team, and there are 14 teams (although the last team is called team "15", I just skipped 14 for some reason, will fix it later).

    The part I'm having issues with is that I want to find the total weight of fish for EACH team. Should I do a for loop, and then do something like
    Code:
    if(team[i]=1) {sumweight +- weight[i]}
    ??

    Here's the code (ignore the empty functions):
    Code:
    #include <stdio.h>
    
    
    //prototypes
    void greeting();
    void readfile();
    void displayResults(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;
    
            greeting(); 
            int i;
    
            for(i=0; i<70; i++)
            {
                    scanf("%d %lf %lf %d", &team[i], &length[i], &weight[i], &deadfish[i]); //Reads the values in the redirected file.
            }
    
            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];}
            }
    
            for(i=0; i<70; i++)
            {
            if(length[i]>longest) {longest=length[i];}
            }
    
            printf("The average length overall of all the fish is %lf and the average weight overall is %lf.\n", avglength, avgweight);
    printf("The number of dead fish overall is %d.\n", frequency);
            printf("The heaviest fish is %lf and the longest is %lf.\n", heaviest, longest);
    }
    
    void greeting()
    {
            printf("");
    }
    
    void readfile() {} //empty methods
    void displayResults(int team, double length, double weight, int deadfish) {} //empty methods
    Thank you very much!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Given a suitable array, and values of team[i] in a nice range, you can do
    Code:
    sumweight[team[i]] += weight[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. pulling numbers from files....
    By Confuzz in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2004, 07:49 PM
  3. File IO - Pulling numbers out of nowhere.
    By punkrockguy318 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2003, 01:47 PM
  4. Replies: 0
    Last Post: 04-27-2003, 06:57 PM
  5. Replies: 8
    Last Post: 04-22-2002, 10:02 PM

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