# How can I tell when I've reached the end of an array?

• Drew88
In summary, the function receives two one-dimensional arrays that correspond to the flight-path angles and the corresponding coefficients of lift. The function sorts the flight-path angles into ascending order while maintaining the correspondence between the flight-path angles and the corresponding coefficients of lift.
Drew88
Ok, for my first post, here goes:

I'm working on an assignment for school that states:

Write a function that receives two one-dinemsional arrays that correspond to the flight-path angles and the corresponding coefficients of lift. The function should sort the flight-path angles into ascending order while maiantaing the correspondence between the flight-path angles and the corresponding coefficients of lift. Assume that the corresponding function prototype is:
void reorder(double& x, double& y);

Using this prototype and the way in which I call it, I think I'm just sending the values of the first element in each array, which I guess is fine. After I pass them to the call function, I read all the values in the original two arrays into two new arrays by incrementing the memory values and then dereferencing the memory values (You'll probably be able to understand much better when you see the code).

My problem is, my loops don't know when to stop reading values into the two new arrays, and I just put in some arbitrary conditions to make the loops stop, which aren't a complete colution.

How can I make the loops "know" when they've read all the values into the new arrays?

Thank you very much.

My code is as follows:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int n=100;
void reorder(double& x, double& y);

int _tmain(int argc, _TCHAR* argv[])
{
ifstream flight, coef;
string filename1, filename2;
double temp(0), temp2(0), temp5(0);
int npts(0), npts2(0), j(0), k(0);
double f[n], c[n];

cout << "Enter the name of the file that contains the flight-path angles: ";
cin >> filename1;

flight.open(filename1.c_str());

if (flight.fail())
{
cerr << "ERROR OPENING FILE " << filename1 << "." << endl;
}

cout << "\nEnter the name of the file that contains the coefficients of lift: ";
cin >> filename2;

coef.open(filename2.c_str());

if (coef.fail())
{
cerr << "ERROR OPENING FILE " << filename2 << "." << endl;
}

flight >> temp;

while (npts < n && !flight.eof())
{
f[npts] = temp;

flight >> temp;

++npts;
}

coef >> temp2;

while (npts2 < n && !coef.eof())
{
c[npts2] = temp2;

coef >> temp2;

++npts2;
}

reorder(*f, *c);

cout << "\nThis is now the final order:";
for (int b=0; b<npts2; b++)
{
cout << "\n" << f << " " << c;
}

return 0;
}

void reorder(double& x, double& y)
{
const int n=100;
int npts(0), npts2(0), j(0), k(0), h(0), g(0);
double temp(0), temp2(0), temp5(0), f[n], c[n];
double * p;
p = &x;
double * u;
u = &y;

//Here is where the two loops are that read the data into two new arrays.
//As you can see, 20 and -1000 are randomly chosen values to make the loops
//break.
while (npts < 20 && *(p+h) > -1000)
{
f[h] = *(p+h);
npts = npts + 1;
h++;
}

while (npts2 < 20 && *(u+g) > -1000)
{
c[g] = *(u+g);
npts2 = npts2 + 1;
g++;
}

for (j=0; j<(npts-2); j++)
{
for(k=0; k<(npts2-1); k++)
{

if (f[k+1] < f[k])
{
temp5 = c[k];
c[k] = c[k+1];
c[k+1] = temp5;
}

double temp1(0), temp2(0);

temp1 = f[k];
temp2 = f[k+1];

if (temp1 <= temp2)
{
f[k] = temp1;
f[k+1] = temp2;
}

else
{
f[k] = temp2;
f[k+1] = temp1;
}
}
}

for (int b=0; b<npts2; b++)
{
*(p+b) = f;
*(u+b) = c;
}

return;
}

After you've read all the data from the input file, store a "sentinel" value in the array after the final "real data" value. This should be some value that cannot occur as a legitimate data value. Then when you loop through the array to extract data, stop looping when you reach the sentinel.

For example, if your data consists of angles that range from 0 to 360 degrees, your sentinel might be -1, or 361, or 1000, or...

Last edited:

It is important to have a clear understanding of the data structure you are working with in order to determine when you have reached the end of an array. In this case, it seems like you are using two one-dimensional arrays to store the flight-path angles and corresponding coefficients of lift.

One way to determine when you have reached the end of an array is to keep track of the size of the array. In your code, you have declared the size of your arrays as n=100. This means that you can use n as a variable to keep track of the size of your arrays. For example, you can initialize npts and npts2 to 0 and increment them every time you add a new element to your arrays. Once npts and npts2 reach the value of n, you know that you have reached the end of your arrays.

Another approach is to use a sentinel value to mark the end of your array. A sentinel value is a special value that is used to indicate the end of a data set. In your code, you are using the value -1000 to break out of your while loops. Instead, you can use a different value that is not likely to be a valid element in your arrays (e.g. -1) and check for that value in your loops to determine when to stop reading values into your arrays.

I would also recommend using a for loop instead of a while loop, as it allows you to specify the exact number of iterations and can help you avoid infinite loops. For example, you can use a for loop with the condition i < n to iterate through your arrays until you reach the end.

Overall, it is important to have a clear understanding of the data structure you are working with and to use appropriate loop structures to ensure that your code is efficient and avoids errors. I hope this helps and good luck with your assignment!

## 1. How do I know when I've reached the end of an array?

One way to determine the end of an array is by checking the length property of the array. This property tells you the total number of elements in the array, so if you are iterating through the array and the index is equal to the length, then you have reached the end.

## 2. What happens if I try to access an index that is beyond the end of an array?

If you try to access an index that is beyond the end of an array, you will get an "undefined" value. This means that the index does not exist in the array and you have reached the end.

## 3. Can I use a for loop to iterate through an array until the end?

Yes, a for loop is a common method for iterating through an array until the end. You can use the length property to determine when to stop the loop.

## 4. Is there a built-in method to check for the end of an array?

Yes, the array object in JavaScript has a built-in method called "forEach" which allows you to iterate through each element in the array. This method automatically stops when it reaches the end of the array.

## 5. What can I do if I need to access the last element in an array?

To access the last element in an array, you can use the length property to determine the index of the last element. Then, you can use this index to access the element using bracket notation or the array's "slice" method.

• Programming and Computer Science
Replies
23
Views
1K
• Programming and Computer Science
Replies
20
Views
1K
• Programming and Computer Science
Replies
17
Views
2K
• Programming and Computer Science
Replies
5
Views
2K
• Programming and Computer Science
Replies
1
Views
691
• Programming and Computer Science
Replies
1
Views
1K
• Programming and Computer Science
Replies
6
Views
1K
• Programming and Computer Science
Replies
11
Views
1K
• Programming and Computer Science
Replies
6
Views
2K
• Programming and Computer Science
Replies
89
Views
4K