how to stop going out of bounds without number of elements

I am doing a file input into an array and the I cannot change the function protos so I cannot add a number of elements variable. I am calculating thrust from this file and it works until the time is greater than the last array input. So how do I do it without a number of elements variable?


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
 double thrust(double time, const double thrustCurve[Capacity][2]){

	double Newtons;

	if (time <= 0)
		Newtons = 0;
	else if (time <= thrustCurve[0][0])
		Newtons = ((time)/(thrustCurve[0][0]))*(thrustCurve[0][1]);
	else if (time > thrustCurve[0][0]){

		int i = 0;		// local variable for setting initial values to call array parameters
		int j = 1;		// local variable for setting initial values to call array parameters

		while(time >= thrustCurve[i][0]){

			if (time >= thrustCurve[i][0] && time < thrustCurve[j][0]){
				Newtons = ((time - thrustCurve[i][0])/(thrustCurve[j][0] - thrustCurve[i][0])) * 
					(thrustCurve[j][1] - thrustCurve[i][1]) + 
					(thrustCurve[i][1]);
			}
			else
				Newtons = 0;	

		
		}
	}


	return Newtons;
}
Post the whole SC, was getting it, but need to see all values :) cheers!
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

//	this function will read the engine thrust curve data
//	from a data file specified in the first parameter
//	the function will add the initial data point of (0,0)
//	that is not actually in the file.  Maximum of 50 data points
//	the function will return a 0 if successfull and a 1 if failed
int getThrustData(char filename[], double thrustCurve[][2]);

//	this function will return the thrust of the engine
//	for any time given an array with
//	thrust curve data
//	the input is in seconds and the ooutput value is in newtons
double thrust(double time, const double thrustCurve[][2]);

//	Constraints
const int Capacity = 50;

int main()
{
	// Output Identification
	system("CLS");
	cout << "In Class #12 by Christopher Adique -\n "
		<< "Engine Thrust from File\n\n";

	// Variables
	char filename[Capacity];
	double thrustCurve[Capacity][2];
	double time = 0;

	getThrustData(filename, thrustCurve);

	while (time != -1){
		cout << "\nEnter a time of interest (seconds) [-1 to exit]: ";
		cin >> time;

		if(time != -1)
			cout << "The engine thrust at that is " << setprecision(3) << thrust(time, thrustCurve) << " Newtons.\n";

	}

	cout << "\n\nEnd Program.\n";

	return 0;
}


//
//
//
int getThrustData(char filename[Capacity], double thrustCurve[Capacity][2]){

	// Local Variables
	ifstream fin;
	ofstream fout;
	string fname;
	string junk;
	int i = 0;
	int j = 1;

	cout << "Enter the name of the engine data file: ";
std:getline (std::cin,fname);

	fin.open(fname);

	double dataTime = 0.0f;		//file input time variable
	double dataThrust = 0.0f;	//file input thrust variable

	//check to see if see if file has been opened. 
	if (!fin){
		cout << "The file is not opening correctly" << endl;
		return 0;
	}

	//filling array with file data
	getline (fin,junk);
	while(!fin.eof()){

		fin >> dataTime >> dataThrust;

		thrustCurve[i][0] = dataTime;
		thrustCurve[i][1] = dataThrust;

		i++;
	}

}

//
//
//
double thrust(double time, const double thrustCurve[Capacity][2]){

	double Newtons;

	if (time <= 0)
		Newtons = 0;
	else if (time <= thrustCurve[0][0])
		Newtons = ((time)/(thrustCurve[0][0]))*(thrustCurve[0][1]);
	else if (time > thrustCurve[0][0]){

		int i = 0;		// local variable for setting initial values to call array parameters
		int j = 1;		// local variable for setting initial values to call array parameters

		while(time >= thrustCurve[i][0]){

			if (time >= thrustCurve[i][0] && time < thrustCurve[j][0]){
				Newtons = ((time - thrustCurve[i][0])/(thrustCurve[j][0] - thrustCurve[i][0])) * 
					(thrustCurve[j][1] - thrustCurve[i][1]) + 
					(thrustCurve[i][1]);
			}
			else
				Newtons = 0;	
                        i++;
		        j++;
		}
	}


	return Newtons;
}



the data file

the first line get junked but the rests is time and thrust

C6 18 70 0-3-5-7 .0108 .0231 E
0.031 0.946
0.092 4.826
0.139 9.936
0.192 14.090
0.209 11.446
0.231 7.381
0.248 6.151
0.292 5.489
0.370 4.921
0.475 4.448
0.671 4.258
0.702 4.542
0.723 4.164
0.850 4.448
1.063 4.353
1.211 4.353
1.242 4.069
1.303 4.258
1.468 4.353
1.656 4.448
1.821 4.448
1.834 2.933
1.847 1.325
1.860 0.000
Last edited on
it works the way i want until the input time is greater than the biggest time vale in the array.
Topic archived. No new replies allowed.