Can someone explain why my code isn\'t working? This is supposed to be linear in
ID: 664282 • Letter: C
Question
Can someone explain why my code isn't working? This is supposed to be linear interpolation. It runs but closes because it "stoped working"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int a;
int b;
int j, i;
float t1, t2, m, p2, p1, w, p,t;
double pt [75][21] = {...};
cout << "input temp"<< endl;
cin >> t;
cout << "input the pressure"<< endl;
cin >> p;
for (int i=0; i<21; i++);
for (int j=0; j<75; j++)
{
if (pt[j][i]>= p)
{
p1=pt[j-1][i];
p2 =pt[j][i];
break;
}
}
cout<<"p1"<<p1;
cout<<"p2"<<p2;
for(int i=0;i<75;i++)
for(int j=0;j<21;j++)
{
if(pt[i][j]>=t)
{
t1=pt[i][j-1];
t2=pt[i][j];
break;
}
}
cout<<"t1"<<t1;
cout<<"t2"<<t2;
m=(t2-t1)/(p2-p1);
cout.precision(4);
cout<<"The slope is: "<<m<<" ";
cin.get();
w=t1-(m*p1);
cout<<"the y interceptis: "<<w<<" ";
cout<<"the line equation is: y=("<<m<<")*p + "<<b<<" ";
{
if (p = 10)
{
a = 18;
}
if (p=10)
{
a = 5;
}
if (p=15)
{
a = 14;
}
if (p>= 1)
...
if (p>= 1e-10)
{
a=1;
}
}
b= (120+t)/5;
cout << "the answer is" << pt[a][b]<< endl;
return 0;
}
Explanation / Answer
working code
initialize you pt array. I have made it zero.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int a;
int b;
int j, i;
float t1, t2, m, p2, p1, w, p,t;
double pt [75][21];
cout << "input temp"<< endl;
cin >> t;
cout << "input the pressure"<< endl;
cin >> p;
for (int i=0; i<21; i++);
for (int j=0; j<75; j++)
{
if (pt[j][i]>= p)
{
p1=pt[j-1][i];
p2 =pt[j][i];
break;
}
}
cout<<"p1"<<p1;
cout<<"p2"<<p2;
for(int i=0;i<75;i++)
for(int j=0;j<21;j++)
{
if(pt[i][j]>=t)
{
t1=pt[i][j-1];
t2=pt[i][j];
break;
}
}
cout<<"t1"<<t1;
cout<<"t2"<<t2;
m=(t2-t1)/(p2-p1);
cout.precision(4);
cout<<"The slope is: "<<m<<" ";
cin.get();
w=t1-(m*p1);
cout<<"the y interceptis: "<<w<<" ";
cout<<"the line equation is: y=("<<m<<")*p + "<<b<<" ";
{
if (p = 10)
{
a = 18;
}
if (p=10)
{
a = 5;
}
if (p=15)
{
a = 14;
}
if (p>= 1)
{
if (p>= 1e-10)
{
a=1;
}
a=1;
}
b= (120+t)/5;
cout << "the answer is" << pt[a][b]<< endl;
return 0;
}
}