Thread: Computing a ratio with an array

  1. #1
    Registered User
    Join Date
    Feb 2014
    Location
    Orlando, Florida
    Posts
    3

    Computing a ratio with an array

    What is wrong with this code cause im not getting the correct ratio of total people to cumulative length?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    //define constant variables
    #define FIRST_CAR_LENGTH 10
    #define NORMAL_CAR_LENGTH 8
    #define CAR_CAPACITY 4
    
    
    int main(void){
        
        //Set up some variable with values that come from input or calculations
        double t_track;
        double train_length;
        double useable_track;
        int t_cars, maxcar, n;
        int t_people, maxpeep;
        int t_trains;
        int i; 
        float ratio_array[100];
        float sum;
        
        //initialize array values to zero
        for(i=0;i<99;i++){
            ratio_array[i]=0;
        }
        
        maxpeep = 0;
        maxcar = 0;
        
        
        //Prompting the user for data and assigning it to variables
        printf("What is the total length of the track, in feet?\n");
        scanf("%lf", &t_track);
        printf("what is the maximum length of a train, in feet?\n");
        scanf("%lf", &train_length);
        
        useable_track = (t_track * .25);
       
       //for loop to calculate all different train lengths
       for(i=10;i<=train_length;i+=8){ 
        
        t_cars = ((i-FIRST_CAR_LENGTH)/(NORMAL_CAR_LENGTH))+1;
        t_trains = (useable_track/i);
        t_people = (t_cars * CAR_CAPACITY * t_trains);
        
        if(maxpeep < t_people)
            maxpeep = t_people;
        if(maxcar < t_cars)
            maxcar = t_cars;
        
        ratio_array[t_cars-1] = (t_people/(i*t_cars)); 
        n++;
         }
        
        for(i=0;i<99;i++){
            sum += ratio_array[i];
        }
        
        printf("your ride can have at most %d people on it at one time\n", maxpeep);
        printf("This can be achieved with trains of %d cars\n", maxcar-1);
        printf("AVG ratio: %.3f\n", (sum/n));
        
        printf("\n");
        
        system("Pause");
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Let's say you do 25/4. (I'm looking at line 53 as posted.) The result of that division is 6 (there is a remainder of 1, but you didn't ask for it so you don't get it). Note that this implies that if i*t_cars > t_people, then ratio_array will be zero (since the remainder is ignored). If you want to do division with decimals, you need to have decimals in the problem (or introduce one, by doing something like "t_people*1.0/(i*t_cars)").

  3. #3
    Registered User
    Join Date
    Feb 2014
    Location
    Orlando, Florida
    Posts
    3
    No that's not working, but I appreciate the help! here is an example out put:What is the total length of the track, in feet?
    4025
    What is the maximum length of a train, in feet?
    89
    Your ride can have at most 480 people on it at one time.
    This can be achieved with trains of 6 cars.
    AVG Ratio: 0.467

    and my AVG Ratio is out putting 0.251

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    As I'm looking over that loop again: (1) The whole point of putting the macros at the top is so that you wouldn't type things like "i=10" and "i+=8"; (2) What is the significance of the divisor i*t_cars? i is the length of a train, t_cars is the number of cars; I don't see why those should be multiplied.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    IMO, you shouldn't be doing your math calculations, inside a for loop. A switch statement or a sequence of if statements, is needed.

    To debug what you have, simply add a printf() statement right after your calculation line of code, and a getchar(). (Or use your debugger, and watch the result of the calculations, as they occur, step by step).

    In general, loops with calculations in them, are used to refine the answer (think of a loop calculating the digits of Pi) - where the previous calculation is needed to reach the next answer. That shouldn't be the case with this problem.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Adak View Post
    IMO, you shouldn't be doing your math calculations, inside a for loop. A switch statement or a sequence of if statements, is needed.

    To debug what you have, simply add a printf() statement right after your calculation line of code, and a getchar(). (Or use your debugger, and watch the result of the calculations, as they occur, step by step).

    In general, loops with calculations in them, are used to refine the answer (think of a loop calculating the digits of Pi) - where the previous calculation is needed to reach the next answer. That shouldn't be the case with this problem.
    I think the idea (so far as I can tell) is to do the problem for each possible train-length (one car, two cars, three cars, etc) and storing the results for later work.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And a step in the right direction would be getting the math correct for one ratio (that is now incorrect), imo.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You have 100 elements and are only initialising 99 of them to 0... although you're only summing 99 of them as well. But then dividing by fubar. This is probably the issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program is not computing averages from an array. Simple C
    By Mike Garber in forum C Programming
    Replies: 10
    Last Post: 09-23-2013, 10:43 PM
  2. Reading from file to an array and computing the grades
    By acmarshall in forum C++ Programming
    Replies: 4
    Last Post: 12-24-2011, 07:42 PM
  3. Computing the sum of each diagonal in an array
    By Seeshi_suin in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 11:41 PM
  4. 2:1 Aspect Ratio
    By Wraithan in forum Game Programming
    Replies: 6
    Last Post: 02-09-2006, 02:17 AM
  5. Golden Ratio
    By mas in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2004, 09:20 AM

Tags for this Thread