How to use a bool function to get my results for acceleration?

I have finally figured out how to get my data from the file and output, but now I am having trouble with my acceleration function. Right now, I have the acceleration function commented out and the output for acceleration at the bottom because if I try to run the program with them in it the program stops working. I'm just hoping that someone can help me with my acceleration calculations and then finally outputting that acceleration.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//Program that reads the time and vertical velocity data and
//then calculates things from it

#include <iostream>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <iomanip>

using namespace std;

bool calculateAccelerationArray (vector<double> t_array, vector<double> v_array, vector<double> a_array);//declare functions
void displayAcceleration(vector<double> t_array, vector<double> v_array, vector<double> a_array);

int main(int argc, char *argv[])
{
      //setup things I'll be needing
    int k=0;
    double time;
double velocity;
   vector<double> t_array;
   vector<double> v_array;
   vector<double> a_array;
    const int LINE_LENGTH(50);
    char cstring1[LINE_LENGTH];

  // Statement of what the program should do
    cout << "This program should read the time and vertical velocity data from a file " << endl;
    cout << "and then use that data to do some calculations to estimate the acceleration" << endl;
    cout << "at each time step." << endl;
cout << endl;

//Get file


ifstream input1;
input1.open("rocketVelocityData.txt"); //opens the input file
input1.getline(cstring1,LINE_LENGTH); //read a blank line to skip the header

if(input1.fail()) //continue if files opened
{
    cerr << "Error opening the file." << endl;
        exit(1);
}

//Read data values from file

while(!input1.eof())
{
    input1 >> time;
    t_array.push_back(time);
    input1 >> velocity;
    v_array.push_back(velocity);
}


//calculateAccelerationArray(t_array, v_array,a_array);
displayAcceleration(t_array, v_array, a_array);

system("PAUSE");
    return 0;
}

//-----------------------------------------------------------------------------------------------------------

bool calculateAccelerationArray (vector<double> t_array, vector<double> v_array, vector<double> a_array)
{
    int k=0;
 {
     a_array[k]=0;
     a_array[1] = (v_array[2] - v_array[1])/(t_array[2] - t_array[1]);

    for(k=2; k<=t_array.size()-1;k++)
    {
    a_array[k] = ((v_array[k+1] - v_array[k-1])/(t_array[k+1] - t_array[k-1]));
    }

        a_array[t_array.size()] = (v_array[t_array.size()] - v_array[t_array.size()-1])/(t_array[t_array.size()] - t_array[t_array.size()-1]);
}
}

//-----------------------------------------------------------------------------------------------------

void displayAcceleration(vector<double> t_array, vector<double> v_array, vector<double> a_array)
{
int k=0;

    cout << "   Number     Time(s)     Velocity(m/s)       Acceleration(m/s^2)" << endl;

 for (int k=0;k<t_array.size();k++)
    {
        cout.setf(ios::fixed);
        cout.setf(ios::right);
        cout.precision(3);

        cout  << setw(4) << k << setw(10) << t_array[k] << setw(13) << v_array[k] << endl;// setw(17) << a_array[k] << endl;


    }

        return;
}


The file I'm reading from rocketVelocityData.txt gives me the time and velocity. I can post if needed

Last edited on
The problem is that at no point do you ever create any storage space on a_array. You declare it on line 23, which creates an array of size 0, i.e. with no elemtns. Then, you pass it into calculateAccelerationArray(), where you immediately try and assign values to its elements. But at that point, it still has size 0, so it doesn't have any elements.

I assume by "stops working", you mean that it crashes with some kind of error message? That's happening because at 70, you're trying to set the value of the 1st element of the vector, when the vector doesn't have any elements.

I'll also point out that you're passing your vectors into calculateAccelerationArray() by value. This means that the vectors inside that function are local copies. When the function exits, those copies will be destroyed. The vectors with the same names in the main function will be unchanged by anything that happens inside calculateAccelerationArray().

You'll need to pass them into the function by reference, instead.

How do I then assign the necessary storage space for my a_array?and yeah it is crashing with the calculation and a_array in it.

Also for the pass by reference where exactly do I need the & do I only need it for the a_array vector since the other two don't need to change?

I'm sorry I've worked on this program for class for so long that its hard for me to see what I have to do. I'm just a little burnt out on it for right now and want to get it done since it's already late. Only program that has caused me any trouble so far. Thanks for the reply MikeyBoy.
How do I then assign the necessary storage space for my a_array?and yeah it is crashing with the calculation and a_array in it.

You can specify a size for a vector upfront using the resize() method. Alternatively, push_back() assigns the storage for the new element(s) as they're added - this is what you're already doing for t_array and v_array in your main function.

I'd recommend looking at the reference material for vectors on this site, if you're going to use them. It's an incredibly useful class, and understanding properly how to use them will be helpful.

Also for the pass by reference where exactly do I need the &


It comes at the end of the type of the parameter - or, as some people prefer, at the start of the name:

1
2
3
4
5
vector<double>& a_array

// OR

vector<double> &a_array


Both are valid.

do I only need it for the a_array vector since the other two don't need to change?

You only need it for a_array, because that's the only one that changes. However, depending on the size of the vectors, it may be more efficient to do it for all of them, so you don't spend time making unnecessary copies as you pass in the vectors (although with newer compilers, this may be optimized away anyway, so it might not be worth worrying about).

If you do pass the other vectors in by reference, you can specify them as const, to indicate - and enforce - that their contents aren't changed by the function, e.g.

const vector<double>& t_array

Last edited on
Topic archived. No new replies allowed.