Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When running a code, it says that it \"stopped working\", can someone tell me wh

ID: 664322 • Letter: W

Question

When running a code, it says that it "stopped working", can someone tell me what I did wrong please? I am supposed to be able to plug in a temperature and pressure and using linear interpolation get back any point between two coordinates using the y=mx+b equation.

#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<<endl;
cout<<"p2"<<p2<<endl;
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<<endl;
cout<<"t2="<<t2<<endl;
m=(t2+t1)/(p2+p1);
cout.precision(4);
cout<<"The slope is: "<<m<<" ";
cin.get();

w=t1-(m*p1);
cout<<"the y intercept is: "<<w<< endl;
cout<<"the line equation is: y=("<<m<<")*p + "<<b<<" ";

{

if (p == 5)

{
a = 16;
}
if (p==10)
{
a = 17;
}
if (p==15)
{
a = 18;
}
if (p==20)
{
a=19;
}
if (p==25)
{
a=20;
}
if (p==30)
{
a=21;
}
if (p==35)
{
a=22;
}
if (p==40)
{
a=23;
}
if (p==45)
{
a=24;
}
if (p==50)
{
a=25;
}
if (p==55)
{
a=26;
}
if (p==60)
{
a=27;
}
if (p==65)
{
a=28;
}
if (p==70)
{
a=29;
}
if (p==75)
{
a=30;
}
if (p==80)
{
a=31;
}
if (p==85)
{
a=32;
}
if (p==90)
{
a=33;
}
if (p==95)
{
a=34;
}
if (p==100)
{
a=35;
}
if (p==105)
{
a=36;
}
if (p ==110 )
{
a=37;
}
if (p==115)
{
a=38;
}
if (p==120)
{
a=39;
}
if (p==125)
{
a=40;
}
if (p==130)
{
a=41;
}
if (p==135)
{
a=42;
}
if (p==140)
{
a=43;
}
if (p==145)
{
a=44;
}
if (p==150)
{
a=45;
}
if (p==155)
{
a=46;
}
if (p==160)
{
a=47;
}
if (p==165)
{
a=48;
}
if (p==170)
{
a=49;
}
if (p==175)
{
a=50;
}
if (p==180)
{
a=51;
}
if (p==185)
{
a=52;
}
if (p==190)
{
a=53;
}
if (p==195)
{
a=54;
}
if (p==200)
{
a=55;
}
if (p==205)
{
a=56;
}
if (p==210)
{
a=57;
}
if (p==215)
{
a=58;
}
if (p==220)
{
a=59;
}
if (p==225)
{
a=60;
}
if (p==230)
{
a=61;
}
if (p==235)
{
a=62;
}
if (p==240)
{
a=63;
}
if (p==245)
{
a=64;
}
if (p==250)
{
a=65;
}
if (p==255)
{
a=66;
}
if (p==260)
{
a=67;
}
if (p==265)
{
a=68;
}
if (p==270)
{
a=69;
}
if (p==275)
{
a=70;
}
if (p==280)
{
a=71;
}
if (p==285)
{
a=72;
}
if (p==290)
{
a=73;
}
if (p==295)
{
a=74;
}
if (p==300)
{
a=75;
}

if (p== 1)
{
a = 12;
}
if (p== 2)
{
a = 13;
}
if (p== 3)
{
a = 14;
}
if (p== 4)
{
a = 15;
}

if (p == 1e-1)
{
a = 10;
}
if (p == 1e-2)
{
a = 9;
}
if (p== 1e-3){
a = 8;
}
if (p== 1e-4)
{
a = 7;
}
if (p== 1e-5)
{
a = 6;
}
if (p== 1e-6)
{
a = 5;
}
if (p== 1e-7)
{
a=4;
}
if (p== 1e-8)
{
a=3;
}
if (p== 1e-9)
{
a =2;
}
if (p== 1e-10)
{
a=1;
}
}


b= (120+t)/5;


cout << "the answer is" << pt[a][b]<< endl;

return 0;
}

Explanation / Answer

The following were the errors in the code picked up by my C++ Compiler:

ERROR 1:

double pt [75][21] = {...}

The above line was missing a semicolon at the end. Rememebr every statement in a C++ code should end with a semicolon ; More over, even after the semicolon, it is complaining as "Expression Expected". The error was fixed after initializing with some valid values as given below:

Correction 1:

double pt [75][21] = { {1,2}, {2, 3} };

Error 2: Line number 29 complaints that variable i is declared multiple times. This is because the same variable i is already declared as an int in line 15: int j,i;

for (int i=0; i<21; i++)

Correction 2:

for ( i = 0; i<21; i++)

Error 3: same as i, this time for j - line 30

Correction 3:

for (j = 0; j<75; j++)

Errors 4 and 5: same in lines 43 and 44

Corrections 4 and 5:

for ( i = 0; i<75; i++)

for (j = 0; j<21; j++)

After these 5 correction, the program compiled and RAN OK and produced the following oututs for the sample inputs:

Run 1:

input temp

3

input the pressure

43

p1 = -1.82589e-31

p20.000618

t1=2

t2=3

The slope is: 8090.8643

the y intercept is: 2

the line equation is: y=(8090.8643)*p + 11900

the answer is1.3755e+169

Run 2:

input temp

30

input the pressure

20

p1 =-1.82589e-31

p20.000618

t1=0.011001

t2=1.126896e-40

The slope is: 17.801

the y intercept is: 0.011

the line equation is: y=(17.801)*p + 11900

the answer is 0

Run 3: with temperature = 40 and pressure = 7

input temp

40

input the pressure

7

p1 = -1.82589e-31

p20.000618

t1=0.011001

t2=1.126896e-40

The slope is: 17.801

the y intercept is: 0.011

the line equation is: y = (17.801) * p + 11900

the answer is -9.8894e-150

The complete code after corrections:

#include <iostream.h>

#include <iomanip.h>
#include <string.h>

// using namespace std; // Since the code is compiled in Turbo C++ compiler, namespace is commented out

int main()
{
int a;
int b;
int j, i;
float t1, t2, m, p2, p1, w, p,t;


    double pt [75][21] = {{1,2 },{2,3 } };


cout << "input temp"<< endl;
cin >> t;


cout << "input the pressure"<< endl;
cin >> p;

for ( i=0; i<21; i++);
    for ( j=0; j<75; j++)
{

    if (pt[j][i]>= p)
    {
   p1=pt[j-1][i];
        p2=pt[j][i];
            break;
    }
}

cout<<"p1 ="<<p1<<endl;
    cout<<"p2"<<p2<<endl;
   for( i=0;i<75;i++)
       for( j=0;j<21;j++)
       {
           if(pt[i][j]>=t)
           {
               t1=pt[i][j-1];
               t2=pt[i][j];
               break;
           }
       }
       cout<<"t1="<<t1<<endl;
       cout<<"t2="<<t2<<endl;
   m=(t2+t1)/(p2+p1);
   cout.precision(4);
   cout<<"The slope is: "<<m<<" ";
   cin.get();

   w=t1-(m*p1);
   cout<<"the y intercept is: "<<w<< endl;
   cout<<"the line equation is: y=("<<m<<")*p + "<<b<<" ";

{

   if (p == 5)

{
      a = 16;
}
if (p==10)
{
      a = 17;
}
if (p==15)
{
      a = 18;
}
if (p==20)
{
      a=19;
}
   if (p==25)
{
      a=20;
}
   if (p==30)
{
      a=21;
}
   if (p==35)
{
      a=22;
}
   if (p==40)
{
      a=23;
}
   if (p==45)
{
      a=24;
}
   if (p==50)
{
      a=25;
}
   if (p==55)
{
      a=26;
}
   if (p==60)
{
      a=27;
}
    if (p==65)
{
      a=28;
}
   if (p==70)
{
      a=29;
}
   if (p==75)
{
      a=30;
}
   if (p==80)
{
      a=31;
}
   if (p==85)
{
      a=32;
}
   if (p==90)
{
      a=33;
}
   if (p==95)
{
      a=34;
}
   if (p==100)
{
      a=35;
}
   if (p==105)
{
      a=36;
}
   if (p ==110 )
{
      a=37;
}
   if (p==115)
{
      a=38;
}
if (p==120)
{
      a=39;
}
   if (p==125)
{
      a=40;
}
   if (p==130)
{
      a=41;
}
   if (p==135)
{
      a=42;
}
   if (p==140)
{
      a=43;
}
   if (p==145)
{
      a=44;
}
   if (p==150)
{
      a=45;
}
   if (p==155)
{
      a=46;
}
   if (p==160)
{
      a=47;
}
   if (p==165)
{
      a=48;
}
if (p==170)
{
      a=49;
}
   if (p==175)
{
      a=50;
}
   if (p==180)
{
      a=51;
}
   if (p==185)
{
      a=52;
}
   if (p==190)
{
      a=53;
}
   if (p==195)
{
      a=54;
}
   if (p==200)
{
      a=55;
}
   if (p==205)
{
      a=56;
}
   if (p==210)
{
      a=57;
}
   if (p==215)
{
      a=58;
}
   if (p==220)
{
      a=59;
}
   if (p==225)
{
      a=60;
}
   if (p==230)
{
      a=61;
}
   if (p==235)
{
      a=62;
}
   if (p==240)
{
      a=63;
}
   if (p==245)
{
      a=64;
}
   if (p==250)
{
      a=65;
}
   if (p==255)
{
      a=66;
}
   if (p==260)
{
      a=67;
}
   if (p==265)
{
      a=68;
}
   if (p==270)
{
      a=69;
}
   if (p==275)
{
      a=70;
}
   if (p==280)
{
      a=71;
}
   if (p==285)
{
      a=72;
}
   if (p==290)
{
      a=73;
}
   if (p==295)
{
      a=74;
}
   if (p==300)
{
      a=75;
}

if (p== 1)
{
      a = 12;
}
if (p== 2)
{
      a = 13;
}
if (p== 3)
{
      a = 14;
}
if (p== 4)
{
      a = 15;
}

if (p == 1e-1)
{
      a = 10;
}
if (p == 1e-2)
{
      a = 9;
}
if (p== 1e-3){
    a = 8;
}
if (p== 1e-4)
    {
        a = 7;
    }
    if (p== 1e-5)
    {
        a = 6;
    }
if (p== 1e-6)
    {
        a = 5;
}
if (p== 1e-7)
    {
     a=4;
    }
    if (p== 1e-8)
    {
        a=3;
    }
    if (p== 1e-9)
    {
        a =2;
    }
    if (p== 1e-10)
    {
        a=1;
    }
}


b= (120+t)/5;


cout << "the answer is" << pt[a][b]<< endl;

    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote