Home > Software engineering >  What am I doing wrong here to get the ratio summed up?
What am I doing wrong here to get the ratio summed up?

Time:02-07

So I've got this issue where for some reason my ratio isn't being added up, I'm still new to C and I don't understand what's going on. Here's the code in question. TotalRatio isn't working at all for me.

All of the code should be working fine and here's what I should be getting instead of it ending abruptly

What is the total length of the track, in feet? 1000 What is the maximum length of a train, in feet? 42 Your ride can have at most 112 people on it at one time. This can be achieved with trains of 4 cars. AVG Ratio: 0.451

#include <stdio.h>

#define FIRST_CAR_LENGTH 10
#define NORMAL_CAR_LENGTH 8
#define CAR_CAPACITY 4

int main(){
    
    int totalLength, trainLength, quarter, totalPeople, surplus, totalCar, i, currentLength, currentTrains, currentCars, currentPeople, cumulCarLength,number;
    printf("What is the total length of the track, in feet?\n");
    scanf("%d", &totalLength);
    printf("What is the maximum length of a train, in feet?\n");
    scanf("%d", &trainLength);
    quarter=totalLength/4;
    totalCar=(trainLength-FIRST_CAR_LENGTH)/NORMAL_CAR_LENGTH 1;
    int numberA=totalCar;
    float ratio[number], totalRatio, average, currentRatio;
    int variables[totalCar];
    int max_People=0;
    int max_Cars=0;
    for (i=0; i<totalCar;i  ){
        if(i==0){
            currentLength=FIRST_CAR_LENGTH (NORMAL_CAR_LENGTH*i);
            max_People=quarter/currentLength*CAR_CAPACITY;
            max_Cars=1;
            currentCars=1;
            currentTrains=quarter/currentLength;
            currentPeople=currentTrains*CAR_CAPACITY;
            cumulCarLength=currentTrains*currentLength;
            ratio[i]=(float)currentPeople/cumulCarLength;
        } else {
            currentLength=FIRST_CAR_LENGTH (NORMAL_CAR_LENGTH*i);
            currentCars=(currentLength-FIRST_CAR_LENGTH)/NORMAL_CAR_LENGTH 1;
            currentTrains=quarter/currentLength;
            currentPeople=currentTrains*currentCars*CAR_CAPACITY;
            if(max_People<currentPeople){
                max_People=currentPeople;
                max_Cars=currentCars;
            }
            cumulCarLength=currentTrains*currentLength;
            ratio[i]=(float)currentPeople/cumulCarLength;
        }
    }
    printf("Your ride can have at most %d people on it at one time.\n",max_People);
    printf("This can be achieved with trains of %d cars.\n",max_Cars);
    
    totalRatio=0;
    for (i=0; i<totalCar;i  ){
        //something is wrong with this line of code
        totalRatio  = ratio[i];
    }
    printf("AVG Ratio: %f",totalRatio);
    
    return 0;
}

CodePudding user response:

well for a start this is wrong

float ratio[number]

because you haven't initialized number so it has a random value. Fix that first

  •  Tags:  
  • Related