replace values in two-dimensional

This program using only two-dimensional array. it reads from file which consists of 51 rows observations and 3 columns consists time in seconds, displacement in meter and velocity in meter per second respectively. displacement and velocity are zero initialed. After compiler reads the data, it requires to compute displacement and velocity then replace them in second and third columns and print that to computer screen.
I did most the part and computation but when I tried to replace displacement and velocity computed values into second and third columns, the compiler stops and crashes sometimes.

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
85
PROGRAM DESCRIPTION:
  This program computes horizontal displacement and velocity of the bumber at
  each time value after impact, replace the zero values that were initially read
  into arry. A report is printed to the computer screen and an output file.

  DESCRIPTION OF VARIABLES
  NAME     | TYPE   | DESCRIPTION
 
  train    | double | two-dimensional array of train values
  dis      | double | horizontal displacement in meter
  velocity | double | velocity in meter per second
  nimpact  | int    | number of impacts
  nvalue   | int    | number of values
  i        | int    | outer for loop control variable (impact)
  j        | int    | inner for loop control variable (value)
*******************************************************************************/

/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "I:\\train.txt"
#define outputfile "I:\\train_report.txt"

/* Main function */
int main(void)
{
   /* Declare variables */
   double train[51][3],velocity, dis;
   int i,j,nimpact, nvalue;
   FILE *obs, *report;

   /* Open input file */
   obs = fopen(inputfile,"r");
   report = fopen(outputfile,"w");
   
   /* Verify input file and read input data */
   if(obs == NULL)
   {
      printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
      printf("\n\n   PROGRAM TERMINATED.\n\n\n");
      return 0;
   }
   else
   {
      /* Read number of impacts and values */
      fscanf(obs,"%i %i",&nimpact,&nvalue);
      
      /* Read input data into train array */
      for(i=0; i<=nimpact-1; i++)
      {
         for(j=0; j<=nvalue-1; j++)
            fscanf(obs,"%lf",&train[i][j]);
      }
   }

   /* Compute displacement and velocity */

   for(j=0; j<=nvalue-1; j++)
   {
      for(i=0; i<=nimpact-1; i++)
         dis= 4.219*(exp(-1.58*train[i][1])-exp(-6.32*train[i][1]));
         velocity= (26.67*exp(-6.32*train[i][1]))-(6.67*exp(-1.58*train[i][1]));
  }
      for(i=0; i<=nimpact-1; i++)
      train[i][2]=dis;
      train[i][3]=velocity;
    


   /* Print headings */
   printf("************ START REPORT ***********");
   printf("\n     TRAIN CAR STOPPING ANALYSIS"
          "\n\n\n  Time      Displacement     Velocity"
          "\n\n   (s)           (m)           (m/s)");
    
          
   /* Print array */
   for(i=0; i<=nimpact-1; i++)
   {
      for(j=0; j<=nvalue-1; j++)
      {
         printf("\n%6.3f         %6.3f         %6.3f",train[i][j], train[i][2], train[i][3]);
      }
   }
          

   /* Close input file */
   fclose(obs);
   fclose(report);
  
   /* Exit program */
   return 0;
}

Last edited on
Here is the data file

51 3
0.00 0.0 0.0
0.01 0.0 0.0
0.02 0.0 0.0
0.03 0.0 0.0
0.04 0.0 0.0
0.05 0.0 0.0
0.06 0.0 0.0
0.07 0.0 0.0
0.08 0.0 0.0
0.09 0.0 0.0
0.10 0.0 0.0
0.11 0.0 0.0
0.12 0.0 0.0
0.13 0.0 0.0
0.14 0.0 0.0
0.15 0.0 0.0
0.16 0.0 0.0
0.17 0.0 0.0
0.18 0.0 0.0
0.19 0.0 0.0
0.20 0.0 0.0
0.21 0.0 0.0
0.22 0.0 0.0
0.23 0.0 0.0
0.24 0.0 0.0
0.25 0.0 0.0
0.26 0.0 0.0
0.27 0.0 0.0
0.28 0.0 0.0
0.29 0.0 0.0
0.30 0.0 0.0
0.31 0.0 0.0
0.32 0.0 0.0
0.33 0.0 0.0
0.34 0.0 0.0
0.35 0.0 0.0
0.36 0.0 0.0
0.37 0.0 0.0
0.38 0.0 0.0
0.39 0.0 0.0
0.40 0.0 0.0
0.41 0.0 0.0
0.42 0.0 0.0
0.43 0.0 0.0
0.44 0.0 0.0
0.45 0.0 0.0
0.46 0.0 0.0
0.47 0.0 0.0
0.48 0.0 0.0
0.49 0.0 0.0
0.50 0.0 0.0
How many columns does the table have?
Which column do you write to?
1
2
double train[51][3];
train[i][3] = velocity;
Valid subscripts for a dimension of size 3 are 0, 1, and 2. You seem to be using 1, 2 and 3, which accesses outside the bounds of your array.
keskiverto

3 columns.
after i compute displacement and velocity, I have to write
-displacement into column 2.
-velocity into column 3.

Note: computation of displacement and velocity is from column 1 which is time .
So, this is the original equation
displacement= 4.219*(exp(-1.58*time)-exp(-6.32*time))
velocity= (26.67*exp(-6.32*time))-(6.67*exp(-1.58*time))
Zhuge,

True, but if I use for j loop 2 dimension, the compiler shows nothing.
The compliler was repeating the rows 3 times so I fix it


PROGRAM DESCRIPTION:
This program computes horizontal displacement and velocity of the bumber at
each time value after impact, replace the zero values that were initially read
into arry. A report is printed to the computer screen and an output file.

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
85
86
DESCRIPTION OF VARIABLES
  NAME     | TYPE   | DESCRIPTION
 
  train    | double | two-dimensional array of train values
  dis      | double | horizontal displacement in meter
  velocity | double | velocity in meter per second
  nimpact  | int    | number of impacts
  nvalue   | int    | number of values
  i        | int    | outer for loop control variable (impact)
  j        | int    | inner for loop control variable (value)
*******************************************************************************/

/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
#define inputfile "I:\\ENGR200\\train.txt"
#define outputfile "I:\\ENGR200\\train_report.txt"

/* Main function */
int main(void)
{
   /* Declare variables */
   double train[52][3],velocity=0.0, dis=0.0;
   int i,j,nimpact, nvalue;
   FILE *obs, *report;

   /* Open input file */
   obs = fopen(inputfile,"r");
   report = fopen(outputfile,"w");
   
   /* Verify input file and read input data */
   if(obs == NULL)
   {
      printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
      printf("\n\n   PROGRAM TERMINATED.\n\n\n");
      return 0;
   }
   else
   {
      /* Read number of impacts and values */
      fscanf(obs,"%i %i",&nimpact,&nvalue);
      
      /* Read input data into train array */
      for(i=0; i<=nimpact-1; i++)
      {
         for(j=0; j<=nvalue-1; j++)
            fscanf(obs,"%lf",&train[i][j]);
      }
   }
   
/* Compute displacement and velocity */

   for(j=0; j<=nvalue-1; j++)
   {
      for(i=0; i<=nimpact-1; i++)
         dis= 4.219*(exp(-1.58*train[i][0])-exp(-6.32*train[i][0]));
         velocity= (26.67*exp(-6.32*train[i][0]))-(6.67*exp(-1.58*train[i][0]));
    }
    
      for(i=0; i<=nimpact-1; i++)
      {
          train[i][1]=dis;
          train[i][2]=velocity;
      }
    


   /* Print  headings */
   printf("************ START REPORT ***********");
   printf("\n     TRAIN CAR STOPPING ANALYSIS"
          "\n\n\n  Time      Displacement     Velocity"
          "\n   (s)           (m)           (m/s)");
    
          
   /* Print array */
   for(i=0; i<=nimpact-1; i++)
   {
       printf("\n%6.3f         %6.3f         %6.3f",train[i][0], train[i][1], train[i][2]);
      
   }
          

   /* Close input file */
   fclose(obs);
   fclose(report);
  
   /* Exit program */
   return 0;
}
Last edited on
Please use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/

Note: Your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Compute displacement and velocity */
for(j=0; j<=nvalue-1; j++)
{
  for(i=0; i<=nimpact-1; i++)
    dis= 4.219*(exp(-1.58*train[i][0])-exp(-6.32*train[i][0]));

  velocity= (26.67*exp(-6.32*train[i][0]))-(6.67*exp(-1.58*train[i][0]));
}

for(i=0; i<=nimpact-1; i++)
{
  train[i][1]=dis;
  train[i][2]=velocity;
}

Does the same as:
1
2
3
4
5
6
7
8
9
/* Compute displacement and velocity */
dis     = 4.219*(exp(-1.58*train[nimpact-1][0]) - exp(-6.32*train[nimpact-1][0]));
velocity= (26.67*exp(-6.32*train[nimpact][0])) - (6.67*exp(-1.58*train[nimpact][0]));

for ( i=0; i < nimpact; ++i )
{
  train[i][1] = dis;
  train[i][2] = velocity;
}

Topic archived. No new replies allowed.