One-Dimensional Arrays

Hello,

I have been given the following assignment:

INTRODUCTION:
A railroad track has a spur line that is a dead end. However, at the end there is a railroad bumper that has been designed to stop a moving railroad car. The railroad car has a mass of 20,000 kilograms and the car is moving at 20 meters per second. At impact, the horizontal displacement in meters and the velocity in meters per second of the bumper as a function of time is given by:

displacement(t)=4.219(e^(-1.58t)-e^(-6.32t))

velocity(t)=26.67e^(-6.32t)-6.67e^(-1.58t)

TEST:
Write a C program that will read from the input file called time the time values into a one-dimensional array. The program will use the time array to compute two additional one-dimensional arrays. The first will be the horizontal displacement array and the second will be the velocity array of the bumper at each time value after impact. Display a table such that the first column is time in seconds, the second column is displacement in meters, and the third column is velocity in meters per second.

The problem I am having is figuring out what the (t) stands for after displacement, velocity, and the exponents. My original thought was time but I'm not sure because my program doe not calculate correctly. Any help would be greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  DESCRIPTION OF VARIABLES:
  NAME         | TYPE   | DESCRIPTION                                
 
  seconds      | double | one-dimensional array of time data in seconds
  displacement | double | computed horizontal displacement in meters
  velocity     | double | computed velocity in meters per second
  i            | int    | "for" loop index variable
  ndata        | int    | number of time data inputs
***********************************************************************/

/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "c:\\users\\billy\\desktop\\engr 200\\time.txt"
#define outputfile "c:\\users\\billy\\desktop\\engr 200\\bumper.txt"

/* Main function */
int main(void)
{
   /* Declare and initialize variables */
   double seconds[51], displacement[51], velocity[51];
   int i, ndata;       
   FILE *report, *table;

   /* Open input and output files */
   report = fopen(inputfile,"r");
	table = fopen(outputfile,"w");
      
   /* Verify input file, quit if empty, else read input data */
	if(report == NULL)
	{
	   printf("\n\nError opening input file.");
      printf("\nProgram terminated.");
      printf("\n\n\nPress Enter to quit.\n");
      getchar(); getchar();
      return 0;
   }
   else
	{
       /* Read control number */
       fscanf(report,"%i",&ndata);
       
       /* Read input data into array */
       for(i=0; i<=ndata-1; i++)
          fscanf(report,"%lf",&seconds[i]);
   }
   
   /* Compute the horizontal displacement and velocity */
   for(i=0; i<=ndata-1; i++)
   {
   displacement[i] = 4.219 * (exp(-1.58*seconds[i]) - exp(-6.32*seconds[i]));
   
   velocity[i] = (26.67 * exp(-6.32*seconds[i])) - 
	              (6.67 * exp(-1.58*seconds[i]));
   }
      
   /* Print report to computer screen and output file */
   printf("******************************************"
          "\n            STOPPING ANALYSIS"
          "\n\n   Time      Displacement       Velocity"
			 "\n(seconds)      (meters)       (meters/sec)");
	fprintf(table,"******************************************"
          "\n            STOPPING ANALYSIS"
          "\n\n   Time      Displacement       Velocity"
			 "\n(seconds)      (meters)       (meters/sec)");

   for(i=0; i<=ndata-1; i++)
   {
		printf("\n   %4.2f         %5.3f            %6.3f",
		       seconds[i],displacement[i],velocity[i]);
	   fprintf(table,"\n   %4.2f         %5.3f            %6.3f",
		       seconds[i],displacement[i],velocity[i]);       
   }

   printf("\n******************************************");
   fprintf(table,"\n******************************************");		 

   /* Close input and output files */
   fclose(report);
   fclose(table);
   
   /* Exit program */
   printf("\n\n\nPress Enter to quit.\n");
   getchar(); getchar();
   return 0;
}   
/*********************************************************************/
The t stands for time. Your calculations look right. Show some sample input/output so that we can see the wrong values your program is giving
Thanks for the quick response! Below are the inputs and outputs:

Input:

51
0.00
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
0.09
0.10
0.11
0.12
0.13
0.14
0.15
0.16
0.17
0.18
0.19
0.20
0.21
0.22
0.23
0.24
0.25
0.26
0.27
0.28
0.29
0.30
0.31
0.32
0.33
0.34
0.35
0.36
0.37
0.38
0.39
0.40
0.41
0.42
0.43
0.44
0.45
0.46
0.47
0.48
0.49
0.50

Output:

******************************************
STOPPING ANALYSIS

Time Displacement Velocity
(seconds) (meters) (meters/sec)
0.00 0.000 20.000
0.01 0.192 18.471
0.02 0.370 17.041
0.03 0.533 15.703
0.04 0.684 14.451
0.05 0.823 13.281
0.06 0.950 12.186
0.07 1.067 11.164
0.08 1.173 10.208
0.09 1.271 9.315
0.10 1.360 8.481
0.11 1.441 7.702
0.12 1.514 6.975
0.13 1.580 6.296
0.14 1.640 5.663
0.15 1.694 5.072
0.16 1.742 4.522
0.17 1.784 4.009
0.18 1.822 3.531
0.19 1.855 3.086
0.20 1.884 2.672
0.21 1.909 2.287
0.22 1.930 1.929
0.23 1.947 1.596
0.24 1.962 1.287
0.25 1.973 1.000
0.26 1.982 0.734
0.27 1.988 0.487
0.28 1.992 0.259
0.29 1.993 0.048
0.30 1.993 -0.147
0.31 1.990 -0.327
0.32 1.986 -0.494
0.33 1.981 -0.647
0.34 1.973 -0.787
0.35 1.965 -0.917
0.36 1.955 -1.036
0.37 1.944 -1.144
0.38 1.932 -1.244
0.39 1.920 -1.334
0.40 1.906 -1.417
0.41 1.891 -1.491
0.42 1.876 -1.559
0.43 1.860 -1.620
0.44 1.844 -1.675
0.45 1.827 -1.724
0.46 1.809 -1.768
0.47 1.791 -1.806
0.48 1.773 -1.840
0.49 1.755 -1.870
0.50 1.736 -1.896
******************************************
The only thing I can say that might be wrong might be the formula, but even then I'm not sure it is wrong. If you look at that time 0, where the meters per second is still 20, this makes sense because it is saying that at the moment of impact, the rail road car is still doing it's 20 meters per second.
Then after that the whole time where the meters per second is still positive just shows the velocity of the rail road car is decreasing. So picture if you can the rail road car had just hit this dead end and for the those times where the velocity is positive, the rear of the car is up in the air due to Netwon's law of motion.
Now when the velocity becomes negative, this is just the recoil of the impact that is now pushing it backwards and the rear of the car is down on the tracks again and the car is slowly moving backwards, but not as fast as before.
I was going to say the same earlier; your data look consistent to me.

I didn't though; I assumed you have additional data you are comparing these results to which makes you doubt your results?
No, I do not have different data to compare my results to. I simply calculated so of the formulas with a calculator and the answer is not consistent with the my output. I think the only reason the time 0 is coming out correctly is because it's multiplying by 0 so you will always get that answer. Does that make sense?
Just choosing numbers at random to test on my phone's calculator
1
2
3
4
5
0.10 -> 1.35987694m -> 8.48066523m/s

0.27 -> 1.98802027m -> 0.48741426m/s

0.45 -> 1.8266598m -> -1.7239971m/s


I think that's random enough to show that you were probably calculating wrong. Make sure you don't have another weird setting enabled on your calculator
Thanks guys. Apparently, I was calculating wrong. I just verified 4 more random numbers that came out correct. Thanks again!
Topic archived. No new replies allowed.