Something Borrowed

I wasn’t sure exactly where to start as far as actual coding goes for this project, so what do I do? I decided to try copying some one dimensional heat transfer code written in C++ from one of my old engineering books [1] to see if I could get it to work myself. This particular code would seem like a great starting point for what I wanted to do… if it wasn’t for the fact that it’s quite advanced if you ask me. As it turns out, the code has provided quite the learning experience already but not exactly becuase of its build. It started with a foray into the Standard Library. I still haven’t found cohesive documentation on that, but I may just not be looking hard enough. I also don’t know if it would truly help, as I imagine it is quite large and basically not something that one just holds in their head to write from scratch. It all started with this error message:

heat-trans.cpp:6:18: fatal error: conio.h: No such file or directory
#include

compilation terminated.

Some internet and soul searching later, I learned that “conio.h” header file is a Windows only header file. I didn’t even fully realize up to this point that there were OS specific header files. Within these searches I found suggestions to use the “curses.h” header file in place of “conio.h”. This may help me in the future, but it seemed that for now neither was needed. I will be reading up on these header files further in the future but they’re being tabled for now.

With the header file issue out of the way, it’s to compile successfully! No. The new errors became this:

heat-trans.cpp:20:2: error: expected initializer before ‘double’

double P[10] = {0}; // Recursion variable

heat-trans.cpp:24:2: error: ‘P’ does not name a type

P[0]=X[0]/W[0];

heat-trans.cpp:25:2: error: ‘Q’ does not name a type

Q[0]=Z[0]/W[0];

heat-trans.cpp:27:2: error: expected unqualified-id before ‘for’

for(int i=1;i>=0;j–)

heat-trans.cpp:34:22: error: ‘j’ does not name a type

for(int j=m-2;j>>=0;j–)

heat-trans.cpp:39:2: error: expected unqualified-id before ‘for’

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

heat-trans.cpp:39:14: error: ‘i’ does not name a type

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

heat-trans.cpp:39:18: error: ‘i’ does not name a type

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

heat-trans.cpp:44:1: error: expected declaration before ‘}’ token

}

It actually took me a couple of minutes of staring at the errors, staring at the source, and even trying to insert more variable declarations. The simplest of mistakes causing quite the problem. This is where I come back my ideas of using such… rudimentary…  methods helped me learn to be careful about my syntax. This mistake cost me a solid couple minutes of WTFing, but I finally reread the first line of the error and took a closer look at that line of the source. I finally noticed there was no “ { “ for my initial block.

Successful compile! So what do you do once you’ve successfully compiled code? You go about changing it, of course. I started with updating the “ \n “ syntax to the “ endl “ in this block of code.

// Display the solution array
    for(int i=0;i<m;i++)
    {
        cout<<"\n";
        cout<<T[i];
    }

To look like this

    // Display the solution array
    for(int i=0;i<m;i++)
    {
        //cout<<"\n";
        cout<<endl<<cout<<T[i]<<endl;
    }

Or so I thought. My output suffered much more than I thought it would. It went from simply not being as pretty as I wanted (no final carriage return at the very end of the displayed answer matrix) to printing what looks like memory addresses to me.

0

0.129759

0.117587

0.244196

0.235105

0.408588

0.445225

0.734025

0.681594

1.054tylerdurden@codebase:~/Documents

 

To

0x804a2040

 

0x804a2040.129759

 

0x804a2040.117587

 

0x804a2040.244196

 

0x804a2040.235105

 

0x804a2040.408588

 

0x804a2040.445225

 

0x804a2040.734025

 

0x804a2040.681594

 

0x804a2041.054

tylerdurden@codebase:~/Documents/Cprogs$

I had my final carriage return, but it had come at quite the cost. A little bit of reflecting on my newly learned syntax and I quickly saw my malfunction. I was basically processing “ endl “ into the output of the answer matrix. I terminated the new line command and all was well. I now have the output I need in the format I’m looking for. The final form of this piece of code (before cleaning) came out like this:

    for(int i=0;i<m;i++)
    {
        //cout<<"\n";
        cout<<endl;cout<<T[i];
    }
    cout<<endl;cout<<endl;
}

 

And the output clearly separated from the rest of the rest of the activity in the terminal.

tylerdurden@codebase:~/Documents/Cprogs$ ./heat-trans

0

0.129759

0.117587

0.244196

0.235105

0.408588

0.445225

0.734025

0.681594

1.054

 

tylerdurden@codebase:~/Documents/Cprogs$ 

So what did I learn from this exercise? Libraries and syntax! Two things that are easy to remember at the top level but can be difficult to implement for a beginner to the language. I wil say that the syntax error was one of the types of mistakes that shouldn’t have happened in the first place but, that said, I now have the experience to know that syntax error when I see it again. And I’m not even going to try fooling myself, I will see it again.

Though this program is not mine, I will be posting it on my GitHub as I hope to build upon at least the concepts if not the execution as well. I will also likely be using this program as a testing ground for new concepts as I learn them. Where exactly to go from here, I haven’t exactly made up my mind about. This program was a pretty big jump up from basically no knowledge to handling matrices in a language that doesn’t do that natively in the way something like MatLab does. Mostly likely my next step is trying to write my own, very simple, program based on the few things I’ve learned so far.

Wondering why I went so big on my first program? Wondering why I’m holding steady to my methods after such simple mistakes held me back a few minutes? Have some suggestions on where I can brush up on my Standard Library knowledge? I welcome it all in the comments.

  1. Kreith, F., Manglik, R.M., Bohn, M. S.; Principles of Heat Transfer 7th ed. Stanford, CT: Cengage Learning 2009
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s