linear interpolation help

My program is almost done, but I keep getting an error in line 22.

Page 1 of 1

3 Replies - 3741 Views - Last Post: 10 August 2010 - 07:21 PM Rate Topic: -----

#1 Guest_Jon*


Reputation:

linear interpolation help

Posted 10 August 2010 - 07:11 PM

This program is supposed to get an input from the user and use linear interpolation to solve for the y and z components. When I try to compile, line 22 returns this error: expected primary-expression before ']' token.

Ok, so here's the program:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

double linear (double, double );

int main ()
{


double altit[17], temp[17], pres[17], alt;
ifstream input ("atmosphere.txt");
cout <<"please enter an altitude: ";
cin >> alt;

for (int i=0; i<17; i++)
{
input >> altit[i] >> temp[i] >> pres[i];
}

int base = linear(altit[], alt);
double x1 = altit[base], x2 = altit[base++], y1 = temp[base], y2 = temp[base++], z1 = pres[base], z2 = pres[base++];

double y = y1+(((y2-y1)/(x2-x1))*(alt-x1));
double z = y1+(((y2-y1)/(x2-x1))*(alt-x1));

cout <<"  x       y        z\n\n";
cout << alt <<"    "<<y<<"     "<<z;

system("pause");
return(0);
}

double linear (double array[], double alt)
{
	for (int j=0; j<17; j++)
	{
		if (alt >array[j] && alt<array[j++])
		{
			return j;
		}

	}
	return 0; // error case
}




I'm stumped, so any help will be great. Thanks!

Is This A Good Question/Topic? 0

Replies To: linear interpolation help

#2 Guest_karl*


Reputation:

Re: linear interpolation help

Posted 10 August 2010 - 07:16 PM

View PostJon, on 10 August 2010 - 06:11 PM, said:

This program is supposed to get an input from the user and use linear interpolation to solve for the y and z components. When I try to compile, line 22 returns this error: expected primary-expression before ']' token.

Ok, so here's the program:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

double linear (double, double );

int main ()
{


double altit[17], temp[17], pres[17], alt;
ifstream input ("atmosphere.txt");
cout <<"please enter an altitude: ";
cin >> alt;

for (int i=0; i<17; i++)
{
input >> altit[i] >> temp[i] >> pres[i];
}

int base = linear(altit[], alt);
double x1 = altit[base], x2 = altit[base++], y1 = temp[base], y2 = temp[base++], z1 = pres[base], z2 = pres[base++];

double y = y1+(((y2-y1)/(x2-x1))*(alt-x1));
double z = y1+(((y2-y1)/(x2-x1))*(alt-x1));

cout <<"  x       y        z\n\n";
cout << alt <<"    "<<y<<"     "<<z;

system("pause");
return(0);
}

double linear (double array[], double alt)
{
	for (int j=0; j<17; j++)
	{
		if (alt >array[j] && alt<array[j++])
		{
			return j;
		}

	}
	return 0; // error case
}




I'm stumped, so any help will be great. Thanks!

Was This Post Helpful? 0

#3 Guest_Guest*


Reputation:

Re: linear interpolation help

Posted 10 August 2010 - 07:20 PM

you cannot call a function with a indefinite type ( -> [] )

1.) you have to try it through a pointer.

2.) But easier its to define a finite array ( a[100] ) globally and then just use it,
dont pass it through the functionparameters, use it globally.

But if your function is recursive you have to take pointers instead.

Hope that was usefull...
Was This Post Helpful? 0

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: linear interpolation help

Posted 10 August 2010 - 07:21 PM

You've declared linear() to take 2 doubles:

double linear (double, double );



but your definition says the function takes an array:

double linear (double array[], double alt)



The call should be

int base = linear(altit, alt);



See Arrays as parameters (page down to this section)

This post has been edited by n8wxs: 10 August 2010 - 07:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1