d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Issues Calculating Within An Array.
Add Reply New Topic New Poll
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 21 2012 10:25am
I have created a program that optimizes the amount of people able to ride a roller coaster at one time given a maximum train length, maximum track length, and a parameter "train length may not exceed 1/4 of track length". My calculations are all correct, as I have done many tests, all yielding correct numbers. My issue comes when I try to calculate the average of an array. Each time through the master loop, after comparing poss_peop to most_peop, I calculate a passenger:trackused ratio. As I said, all of my arithmetic is correct, so my issue I believe lies within storing these values and retrieving them from the array.

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
Code
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?

Code

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
Code
#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;
      }
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 21 2012 11:19am
Code
     int i, peop, most_peop=0,poss_trackl=trackl*.25,num_cars=1,best_num_cars;
     int c, num_lengths=(1+((maxtrainl-10)/8)), arrayval=0;
     float avgratio[20],poss_trains,poss_peop;
     
     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++;
         printf("%f",avgratio[arrayval]);
         system("pause");
         if (poss_peop>most_peop){
         most_peop=poss_peop;
         best_num_cars=num_cars;
         }
         num_cars++;
     }
     int sum=0, avg_ratio=0;
     for (i=0;i<num_lengths;i++)    {
         sum+=avgratio[i];
         }
     avg_ratio=(sum/num_lengths);


updated made floats so I could get a value less than 0, but I'm still getting 0.
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 21 2012 12:13pm
Quote (TheDiscoveryChannel @ Sep 21 2012 09:19am)
Code
int i, peop, most_peop=0,poss_trackl=trackl*.25,num_cars=1,best_num_cars;
     int c, num_lengths=(1+((maxtrainl-10)/8)), arrayval=0;
     float avgratio[20],poss_trains,poss_peop;
     
     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++;
         printf("%f",avgratio[arrayval]);
         system("pause");
         if (poss_peop>most_peop){
         most_peop=poss_peop;
         best_num_cars=num_cars;
         }
         num_cars++;
     }
     int sum=0, avg_ratio=0;
     for (i=0;i<num_lengths;i++)    {
         sum+=avgratio[i];
         }
     avg_ratio=(sum/num_lengths);


updated made floats so I could get a value less than 0, but I'm still getting 0.


forgot to update but there were multiple values that needed to be changed to float..

Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Sep 21 2012 01:55pm
you define a float array of size 20 with the name avgratio. then you have a loop where i is first 10 and then 18 and then 24 and the loop stops when this number is higher than maxtrainl. so what actually is maxtrainl? if this is as example 10000, then your loop will obviously do a lot of steps. each loop step you increase the arrayval, where arrayval easily can get 20 or higher. if arrayval is 20 and you access avgratio[arrayval], then you get a segmentation fault because avgratio[19] is the highest number you can use when you have an array of 20 elements.

i see no logic in your code, at the "if"-statement you dont have an indention and you also don't tell us what values u put in your variables at the scanf. (if the scanf is still in the nevest version of the code) -> i can't help you, that's too messy IN MY OPINION ;)

average of an array? should be something like this:
Code

#define LENGTH 20
...
float array[LENGTH];
for(int i=0; i<LENGTH; i++) //20 elements are accessed with the numbers 0 to 19
 array[i] = 1.0*i; //fill with random data
float sum = 0;
for(int i=0; i<LENGTH; i++)
 sum += array[i];
float average = sum/LENGTH;
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 21 2012 03:09pm
Quote (Richter @ Sep 21 2012 11:55am)
you define a float array of size 20 with the name avgratio. then you have a loop where i is first 10 and then 18 and then 24 and the loop stops when this number is higher than maxtrainl. so what actually is maxtrainl? if this is as example 10000, then your loop will obviously do a lot of steps. each loop step you increase the arrayval, where arrayval easily can get 20 or higher. if arrayval is 20 and you access avgratio[arrayval], then you get a segmentation fault because avgratio[19] is the highest number you can use when you have an array of 20 elements.

i see no logic in your code, at the "if"-statement you dont have an indention and you also don't tell us what values u put in your variables at the scanf. (if the scanf is still in the nevest version of the code) -> i can't help you, that's too messy IN MY OPINION ;)

average of an array? should be something like this:
Code
#define LENGTH 20
...
float array[LENGTH];
for(int i=0; i<LENGTH; i++) //20 elements are accessed with the numbers 0 to 19
 array[i] = 1.0*i; //fill with random data
float sum = 0;
for(int i=0; i<LENGTH; i++)
 sum += array[i];
float average = sum/LENGTH;



My question was just about why the arrays werent reading in. I fixed the issue. The issue was in the variables as I defined certain variables as integers instead of floats. Thanks for your help though.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll