Programming part of a project...

I'm going to need loops here, but I'm not sure how (logically).

Ultimately, I need to find the value for CF. The equation for CF is as follows:

CF = dQ*(1.578 x 108) * (CstHeat)

Now, CstHeat is a constant and defined elsewhere, but the equation for dQ is:

dQ = Q3*[1 – {(b/a)/(1 + ((b*F)/k*ln(b/a)))}]

No, all variables in this equation are constants, except for Q3. So to define Q3, we use the equation:

Q3 = 2*3.14*a*F*(Ta – Tair)*L

Unfortunately, it doesn't stop here. Like the equations above, all of the variables are constants here except 'Tair', which should be 1 of 3 numbers. -10, 0, and 10.

So in the end, I should end up with 3 values for CF, based on the 3 values in 'Tair'.

Originally, I started coding from Q3 with a 'for' loop. and was planning on running each of the other equations within that loop, but I can't figure out which loops to use, or even it's the most efficient way to do it. Any pointers or suggestions?

I would start by declaring three functions for CF, dQ and Q3.

Q3 has just one varaible, Tiar, so the declaraion could be
1
2
3
4
double Q3(int Tair)
{
 return (2*3.14*a*F*(Ta – Tair)*L);
};

If you also include Tair as a parameter for dQ and CF
1
2
double dQ(int Tair);
double CF(int Tair);

In CF you pass Tair to dQ, which passes Tair to Q3 (which uses it to calculate the value).
Then you simply call CF in a loop
EG
1
2
3
4
for (int Tair=-10; Tair <=10; Tair+=10)
{
    cout << CF(Tair) << endl;
}

Thanks, I tried coding parts of the project so far...But I've come up with an error...Any idea what this means?

"too few arguments to function `double getdQ(float, float, float, float, float)'"

The actual coded line looks like this:

double getdQ(float Q3, float a, float b, float F, float k);

It's one of my function declarations...
The declaration looks OK, other than the mix of float and double - it is simpler to use just double for all floating point numbers unless you have a particular reason to just use float.

I would suspect you have a call to the function somewhere that only has 4 parameters, for example, but would need to see the rest of the code to verify this.

In the original post you said that everythign except Tair was a constant - if this is the case you can declare them as global constants and remove the need for all the parameters to your functions.

If you do use a number of global constans this way you should probably try and use a more descriptive varaible name for them - and example would be the speed of light, which is usualy coded as 'c' in equations. It is better to have a constant 'SPEED_OF_LIGHT' as this is immediatly obvious what it is for - the equations that use it become larger, but again more obvious to a programmer what they mean.
So I just changed all of the floats in the program to doubles, to make it easier. I do have a call to the function in the program, but it contains the same parameters as defined above.

In the original post, I was trying to break down the problem to make it easier, to work it in sections. I don't think it's possible though, haha. Like I said above, Tair is going to be -10, 0, or 10. But, since I was trying to make it simpler, I left out another variable, which I'll call 'thick' which will have 10 values (1 - 10). The variable above "b" is actually 'a' + 'thick'. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Project 2

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;

double getQ3(double a, double Ta, double L);
double getdQ(double Q3, double a, double b, double F, double k);
double getCF(double dQ, double CstHeat);
double getCI(double b, double a, double L);

int main ()
{
    double a, b, L, dQ, Q3, CF, CI, thick, Ta, Tair, CstHeat; //declare constants
    
    a = 0.05;
    b = a+thick;
    L = 100;
     
    Q3 = getQ3(a, Ta, L);
    dQ = getdQ(Tair, thick);
    CF = getCF(dQ, CstHeat);
    CI = getCI(b, a, L);
}
double Q3(double a, double Ta, double L)
{
       Ta = 150;
}
double dQ(double Q3, double a, double b, double F, double k)
{
       k = 0.1;
       F = 3.0;
}
double CF(double dQ, double CstHeat)
(
       CstHeat = 0.00000000111;
}
double CI(double b, double a, double L)
{
       Cvol = 325;
       CL = 1.50;
}
    system ("pause");
}  


Note that function CI is "part a" of this project.

Ultimately, I want to subtract CI from CF to where CI is cost of insulation and where CF is fuel savings. I want to be able find the largest overall savings.

The main part that I am hung up on is that I have to use Tair and b(which is a+thick) in different functions, and in the end, I need to be able to output a table for the values of CI, CF and (CF-CI) -and since each of those contains a variable with many values, I just can't wrap my head around the way to do it...

Edit: CI= (b^2-a^2)(L)(Cvol) + (L)(CL)
Last edited on
To declare a constant you use the 'const' keyword, eg
1
2
const double a = 0.05;
const double L = 100.0;

Similarly for Ta, k, F, Cstheat, CVol and CL (as these all appear to be constants).
If you make these declarations global (IE just after using namespace std;) then you can use these cosntant anywhere in the program.
Then you can begin to build up the functions.
The simplest will be
1
2
3
4
double b(double thick)
{
    return (a+thick);  //a is a const, so does not need to be a parameter
}

which then allows you to create
1
2
3
4
double CI(double thick)
{
   return (((b(thick)*b(thick))-(a*a))*L*CVol + (L+CL));  //Lots of constants used
}

Try just using that to ouput your table for CI. You can then look at doing something similar for CF, then all you have to do is combine them for CF-CI and job done :-)

Note that you have system ("pause")at line 46 which does nto appear to be connected to anything - I belive this should be in main() (as the last statement before the closing '}'
Last edited on
OK, so in the function "b", do I have to run a 'for loop' to define thick? or do I do that in the main function, before calling to function b?
You would put the loop in main().
The loop would call 'CI' with the various values of 'thick', 'CI' then in turn calls 'b' passing the value of the 'thick' parameter on as a parameter for 'b'.
EG

1
2
3
4
for (double thick = 1.0; thick <=10.00; thick +=1.0)
{
   cout << CI(thick) << endl;
}

1
2
3
4
double dQ(double Q3)
{
       return (Q3*(1-((b/a)/(1+((b*F)/k*(log(b/a)))))));
}


41 H:\Project 2-Practice Shot.cpp invalid operands of types `double ()(double)' and `const double' to binary `operator/'
41 H:\Project 2-Practice Shot.cpp invalid operands of types `double ()(double)' and `const double' to binary `operator*'
41 H:\Project 2-Practice Shot.cpp invalid operands of types `double ()(double)' and `const double' to binary `operator/'

Am I missing something?

Edit: I think it has something to do with Q3 being a function, and me trying to use the output of that within a new function 'dQ'...
Last edited on
You should always try and keep variable and function names unique as far as possible - if not then the best you can hope for is code that is hard to understand, alternatives are failing to compile or worse (in many ways) code which behaves differently to yuor expectations.

If Q3 is a function then you do not need it to be passed to function dQ as a parameter, you can just call the function within the function dQ (see the 'b' and 'CI' code in an earlier post).
An alternative is to have the value as a parameter (but with a different name, eg 'value_Q3', so it is clear which you mean) and when you USE the function you put the function call in place of the parameter.

So if you had
double Q3(double some_varaible);
you could call dQ as follows
1
2
3
double x,y;
y = 4.53;
x = dQ(Q3(y));

Does that help?
Topic archived. No new replies allowed.