I have a c++ program to write for a class and I'm
stumped. I was wondering if anybody could possibly help.
I am very much a beginner with programming of any
sort.
The program is for a heat transfer problem and the
equation to be solved looks like the following:
Where the number of iterations would be entered by the
user (n). The following would also be known: q, each
L and K and T1. And it would be solving for the Tn+1
value in each.
Use a loop. Or recursion. (Whichever your teacher most recently taught.) Or both (for extra points).
Each time you calculate the answer, you generate a new T that you can use in the next loop to generate progressively more accurate answers. All you have to do is loop the correct number of times.
You'll also have to do a little algebra to get T2 by itself.
given q, T1, K[], and L[]:
1 2 3 4 5 6
for (unsigned i = 0; i < n; i++)
{
T2 = /* solve equation here */
T1 = T2;
}
return T1;
Hope this helps.
[edit]
Oh, by the way. This kind of math problem uses what is known as induction. http://en.wikipedia.org/wiki/Mathematical_induction
(When you read the word "proof" replace it in your head with "solution to the problem". Yeah, I know, horrors.
I suppose if you want to remember every T generated you can stuff them into an array as you go, but that's the only reason to use one that I can think of.
I was unsure from your problem description whether K and L are the same every iteration or not, so if not they will have to be arrays also (or better yet, vectors).
K and L are different for each. How do you use a vector?
The biggest problem i'm having with using arrays is that I can't figure out how to take the data from them and use it in a calculation. Especially when using multiple arrays.
I'm taking this class as an independent study (so I can graduate this summer) and both books I'm using don't seem to help worth a darn.
I found the same with a lot of programming texts...
Create and initialize your arrays (assuming static data) like this:
1 2 3 4 5 6 7 8 9 10 11
// prototype for my function
double calculate_Tn( double, constdouble[], constdouble[], unsigned );
// everything I know already
double K[] = { 12.9, 23.2, 15.4, ... };
double L[] = { 99, 98, 97, 96, 95, ... };
double T1 = -7.0;
unsigned n = sizeof( K ) / sizeof( K[0] ); // num elts in K
// what I want to know
double Tn = calculate_Tn( T1, K, L, n );
Access an array just like you think:
1 2 3 4 5 6 7 8 9
int sum_of_ints( constint a[], unsigned size )
{
int result = 0;
for (unsigned n = 0; n < size; n++)
result += a[ n ];
return result;
}
1 2 3 4 5 6 7 8 9
void list_multiples_of_3( int a[], unsigned size )
{
int m = 3;
for (unsigned n = 0; n < size; n++)
{
a[ n ] = m;
m += 3;
}
}
If you don't know what a vector is yet then don't worry about it. It is just an automatic, variable-sized array.
This site has a very good set of tutorials. You can find a link to them on the upper-right side of the home page. It might be worth it to look through them.