getting error while trying to pass an array by reference

I'm trying to create a program that will print the coefficient of lift by reading in a data file which contains the flight path angle and its corresponding coefficient of lift. I read both of those in as two seperate 1-D arrays. the program is supposed to check to see if the data file is in order, and if it is not it uses the void function reorder() to reorder the data file to ascending order.

here is my source code:

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
#include <iostream> 
#include <fstream> 


using namespace std;

int ordered(double fp_angle[], int num_pts);
void reorder(int count_pts,double &x, double &y);
int main(void)
{   
    int i,num_pts;
    double fp_angle[num_pts], lift[num_pts],order,interp;
    
    string filename; 
    ifstream infile; 
         
    cout << "Enter the name of the input file." << endl; 
    cin >> filename;    
    cout << endl; 
         
    infile.open(filename.c_str()); 
         
        if(!infile) 
          { 
                  cout << "File could not be opened.\n"; 
                  return 0;
          } 
         
    i=0;
    cout<<"Enter the number of points in the file: ";
    cin>>num_pts;
    while (infile>>fp_angle[i]>>lift[i])
    {
          i++;
          order=ordered(fp_angle,num_pts);
          cout<<"Checking to see if file is in order...";
          if (order==0)
          {
               cout<<"This file is not ascending order";
               reorder(num_pts, fp_angle[i], lift[i]);
          }
          else 
          {
               cout<<"Enter a flight path angle: ";
               cin>>fp_angle[i];
               
               interp=lift[i-1]+((fp_angle[i]-fp_angle[i-1])*(lift[i+1]-lift[i-1])/(fp_angle[i+1]-fp_angle[i-1]));
               

               
               }
    }
system("pause");
return 0;
}
int ordered(double fp_angle[], int num_pts)
{
    for(int k=0;k<=num_pts;k++)
    {
        if(fp_angle[k]<fp_angle[k+1])
            return 1;
        if(fp_angle[k]>fp_angle[k+1])
            return 0;
    }
}
void reorder(int count_pts,double &x, double &y)
{
     int j,m,k,swap;
     for (k=0; k<=count_pts-2; k++)
     {
         m=k;
         for(j=k+1; j<=count_pts-1;j++)
         {
             if (x[j]<x[m])
                m=j;
         }
         swap=x[m];
         x[m]=x[k];
         x[k]=swap;
     }
}


within the void function i get this error any time the array is present..
invalid types `double[int]' for array subscript

I do not understand how to fix this,can someone tell me where i went wrong?
thank you
Last edited on
You cannot pass arrays by reference. You have to pass a pointer to it instead.
I'm sorry i'm fairly new with c++, can you explain what you mean by that?
Dont pass fp_angle[]

Pass double* fp_angle where fp_angle is pointing at the array you were trying to pass.

You called it correctly, but your function header and function definition need to be:

int ordered(double* fp_angle, int num_pts)
Last edited on
No, that is not the issue. Reorder needs to be defined like so:

 
void reorder(int count_pts, double* x, double* y); //x and y are pointers to doubles or arrays of doubles 
The c-arrays are always passed to functions as a pointer to the first element, never by copying the whole thing. That's just how the language works. Do it the same way that you did it in the ordered function.
void reorder(int count_pts, double x[], double y[])
Last edited on
Ah, ok, I was looking at the wrong function and thought that's what he was talking about.... lol
Wait, the OP is calling the function like this as if he wants to pass one value from the array. I'm not sure what he is trying to do. You are calling reorder by passing one value from each array but then accessing those variables as though they are arrays. It doesn't make sense at all but I don't have the time to analyze the program right now. The OP will have to explain what he is trying to do.

reorder(num_pts, fp_angle[i], lift[i]);
Last edited on
yes, i just noticed i made that mistake and fixed it.
it should be:
reorder(num_pts, fp_angle,lift);

thank you everyone for your help
Last edited on
Topic archived. No new replies allowed.