Do-While Loop Problem

Hello,
I am not the most familiar with c++ but have a general understanding and I am currently having a problem with making a do-while loop work. Here is my code:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
double x[100], y[100], t[100], I[100], den, den1, den2, R, a, b, C;
double sumx(0), sumy(0), sumxy(0), sumx2(0), sumy2(0);
int n, B(1);

cout << "Enter the number of data values, n: ";
cin >> n;
cout << "\nEnter " << n << " values of t (time) separated by a space: ";
for (int i=1; i <= n; ++i)
{
cin >> t[i];
}
cout << "\nEnter " << n << " values of I (intensity) separated by a space: ";
for (int i=1; i <= n; ++i)
{
cin >> I[i];
}
do
{
for (int i=1; i <= n; ++i)
{
x[i] = log(B+t[i]);
sumx = sumx+x[i];
sumx2 = sumx2+(x[i]*x[i]);
y[i] = log(I[i]);
sumy = sumy+y[i];
sumy2 = sumy2+(y[i]*y[i]);
sumxy = sumxy+(x[i]*y[i]);
}
den = (n*sumx2)-(sumx*sumx);
if (den==0)
cout << "\nSoulution is not possible.";
if (den > 0)
{
a = ((sumy*sumx2)-(sumx*sumxy))/den;
b = ((n*sumxy)-(sumx*sumy))/den;
C = exp(a);
}
den1 = sqrt((n*sumx2)-(sumx*sumx));
den2 = sqrt((n*sumy2)-(sumy*sumy));
R = ((n*sumxy)-(sumx*sumy))/(den1*den2);
} while ((++B) <= 20);

For this particular problem I have n=10 values for time and intensity and I am wanting to perform the calculations within the do-while loop for B=1, B=2,...B=20 generating a table with the values for B, C, and R (did not include that portion of code). Running the program, however, the data is evaluated for B=21 only, not at each individual iteration between 1 and 20 as I am wanting. A do-while loop may not be the solution but any input for solving this problem would be greatly appreciated.
The do while is evaluating for B=1 to 20. May be you are printing the values outside the do while. And hence it is only printing for B = 21. After you come out of the while, the value of B will be 21. And hence it will print the value when B is 21.

For example:
Printing within the do while loop will give you the table you want.
1
2
3
4
5
.
.
R = ((n*sumxy)-(sumx*sumy))/(den1*den2);
cout<<"B = "<<B<<" C = "<<C<<" R = "<<R<<endl;
} while ((++B) <= 20);



Within the do while:
Looks like the formula is not given correctly.

For example, when I give n=1 and time = 1 and Intensity = 22,
each time the value of "(n*sumy2)-(sumy*sumy)" is negative.
And for den2, you are trying to calculate the square root of a negative value.

Hope this helps.
Last edited on
Thanks kameswarib! I did have the table commands outside of the do-while loop and went ahead and changed that portion. Now, however, I am getting all of the correct values for B, and the first correct values for C and R at B=1. From B=2 to 20, my initial C value is just repeated and I am getting an error for the R values.

I know the equation is strange for calculating R, but it does work correctly and has been validated by the first value, so I know that isn't the issue. I'll keep messing with it to see if I can't figure things out, but if anyone has any input I'd appreciate it.
Topic archived. No new replies allowed.