My question is can I declare an array like I did on the second line, and am I storing the value in the array by using
avgratio[arrayval]=(poss_peop/(poss_trains*i))[code/]. Also with [code] for (c=0;c<num_poss_lengths;c++){
sum+=avgratio[c]
am I manipulating the stored value for column "c" in the array?
int c, num_poss_lengths=(1+((maxtrainl-10)/8));
int avgratio[num_poss_lengths];
for (i=10;i<=maxtrainl;i+=8){
......
avgratio[arrayval]=(poss_peop/(poss_trains*i));
arrayval++
}
......
int c,sum=0, avg_ratio=0;
for (c=0;c<num_poss_lengths;c++){
sum+=avgratio[c];
}
avg_ratio=(sum/num_poss_lengths);
Thanks a bunch for all of the help! All I really need is for the error in methodology to be pointed out to me, then I can try and search through lecture notes again...
Full Code for reference
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIRST_CAR_LENGTH 10
#define NORMAL_CAR_LENGTH 8
#define CAR_CAPACITY 4
int main(void) {
int trackl, maxtrainl;
printf("What is the total length of the track, in feet?\n");
scanf("%d",&trackl);
printf("What is the maximum length of a train?\n");
scanf("%d",&maxtrainl);
int i, poss_trains, peop, poss_peop, most_peop=0,poss_trackl=trackl*.25,num_cars=1,best_num_cars;
int c, num_poss_lengths=(1+((maxtrainl-10)/8));
int avgratio[num_poss_lengths], arrayval=0;
for (i=10;i<=maxtrainl;i+=8){
poss_trains=(poss_trackl/i);
peop=(num_cars*CAR_CAPACITY);
poss_peop=(poss_trains*peop);
avgratio[arrayval]=(poss_peop/(poss_trains*i));
arrayval++
if (poss_peop>most_peop){
most_peop=poss_peop;
best_num_cars=num_cars;
}
num_cars++;
}
int c,sum=0, avg_ratio=0;
for (c=0;c<num_poss_lengths;c++) {
sum+=avgratio[c];
}
avg_ratio=(sum/num_poss_lengths);
printf("Your ride can have at most %d people on it at one time.\n",most_peop);
printf("This can be achieved with trains of %d cars.\n",best_num_cars);
printf("AVG ratio: %2d\n",avg_ratio);
system("pause");
return 0;
}

New Topic/Question
Reply




MultiQuote





|