5 Replies - 186 Views - Last Post: 21 September 2012 - 10:41 AM Rate Topic: -----

#1 bradleycmetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 09:24 AM

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
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;
       }



Is This A Good Question/Topic? 0
  • +

Replies To: Issues Calculating Numbers Within an Array

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 233
  • View blog
  • Posts: 549
  • Joined: 27-June 09

Re: Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 09:57 AM

What issue are you having? Is it not compiling or just printing the wrong value?
Was This Post Helpful? 0
  • +
  • -

#3 bradleycmetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Re: Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 10:05 AM

View Postmojo666, on 21 September 2012 - 09:57 AM, said:

What issue are you having? Is it not compiling or just printing the wrong value?


Well it compiles and all values other than avg ratio are correct. I am getting 0.000000 for avg_ratio. I feel like when I try to store a value in line 8 "(poss_peop/(poss_trains*i))" each time it loops, it is not storing it in the array. At the end of the program I try to calculate the average of the ratios in the array (avg of every value) but it comes out 0.000000 so I dont think its storing anything? My peers and I are baffled...
      
int i, poss_trains, peop, poss_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];      
      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++;
      }

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1887
  • View blog
  • Posts: 5,674
  • Joined: 05-May 12

Re: Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 10:10 AM

If poss_people is less then poss_trains*i, then you'll always get zero because you are doing integer division.
Was This Post Helpful? 1
  • +
  • -

#5 bradleycmetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Re: Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 10:16 AM

I made this code to print the value in each array column right after I store it...
      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++;



I get:
0.000000
0.000000
gigantic number
0.000000
0.000000

View PostSkydiver, on 21 September 2012 - 10:10 AM, said:

If poss_people is less then poss_trains*i, then you'll always get zero because you are doing integer division.


I made them into floats...
It didnt do anything
      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++;
      }


It should be a decimal since it will always be less than 1.
Was This Post Helpful? 0
  • +
  • -

#6 bradleycmetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Re: Issues Calculating Numbers Within an Array

Posted 21 September 2012 - 10:41 AM

Thanks for all the responses. I had to make a bunch of my values floats instead of integers. I fixed them, and I get the correct ratio.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1