Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 4 arrays. a=6 indexes, b=20 indexes, x&t=10001 indexes. I have to calculate 10001 values and then sum it and then sum it all up. I have to operate the formula using unique combinations of (a,b) against every value of (x,t).

C++
long double temps[10001];
    long double result = 0;
    long double resids[120];
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 20; j++)
        {
            for (int k = 0; k < 10001; k++)
            {
                temps[i] = pow((x[k] - (a[i] + (0.955 - a[i])*(exp(-(b[j]) * t[k])))), 2);
                result += temps[i];
            }
            for (int l = 0; l < 120; l++)
            {
                resids[l] = result;
            }
        }
    }
Posted
Comments
CPallini 21-Aug-15 6:44am    
Could you please provide more details? It is not clear what the actual question is.
KarstenK 21-Aug-15 6:50am    
Please clearify your question. Use in the text the same variable names as in your code. It is best practice to use long or meaningful names.

I guess you need a two dimensional array. Like results[6][20];
09hadi 21-Aug-15 7:11am    
double a[6], b[20], x[10001], t[10001];
these are the arrays
Sergey Alexandrovich Kryukov 21-Aug-15 11:12am    
And the problem is..?
—SA
09hadi 21-Aug-15 14:28pm    
I cant find the place where the fourth loop should be placed in order to get the values into the resid array. I know this one is stupid

1 solution

I see multiple problems with this code. To specifically answer your question about the result array ...

C++
long double temps[10001];
long double result = 0;
long double resids[120];
for (int i = 0; i < 6; i++)
{
    for (int j = 0; j < 20; j++)
    {
        result = 0; // unsure
        for (int k = 0; k < 10001; k++)
        {
            temps[i] = pow((x[k] - (a[i] + (0.955 - a[i])*(exp(-(b[j]) * t[k])))), 2);
            result += temps[i];
        }
        resids[i * 20 + j] = result; // <-- 120 results go here.
    }
}


I can't tell whether "result" should be reset to zero at the start of each "i" pass or each "j" pass.
 
Share this answer
 
Comments
09hadi 22-Aug-15 3:44am    
Thank you very much.
09hadi 24-Aug-15 14:16pm    
can I trouble you a bit more? the result of these calculations comes down to 20 decimal points. which double is unable to handle. Can you help me with that as well? How can I cater to that?
[no name] 24-Aug-15 14:47pm    
If you need more than 15 digits (double precision) AND you are okay with fixed point math, a signed long will give you almost 20 digits. Your long values will have an implied decimal point. As long as all values have the same implied decimal point, you can use the + and - operators without shifting. Multiplication and division are a bit more complicated. You'd have to write your own code to print these fixed point values. If you need a full 20 digits or more, use "long long" as fixed point values.
09hadi 27-Aug-15 6:57am    
can u give me a sample please?
[no name] 27-Aug-15 17:57pm    
How many digits do you want to the left of the decimal point? How many digits do you want to the right of the decimal point?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900