integer index problem

F

Fan Zhang

Dear group,

I wrote a simple code trying to use the integer index of a "for" loop inside
the loop. The idea is to convert it to double type and lead it into a
function. However it appears I can't call the integer number directly inside
the loop, otherwise, the results are not sensible.

The code is as the following,

int main()
{
int i;
double min, max,modl,tt;
max=PI/2;

for (i=1;i<=90;i++)
{
9---> min=i/radian;
modl=background+Gaussian_quadrature(max,min);
printf("%d \t %6.4f \n",i,modl);
tt=tt+1.;
}
system("pause");
return 0;
}

I tried min= (double)i/radian in line 9 as well. It does not give the right
result either. Eventually, I defined another variable tt, initialized it to
be 1 outside of the loop and increased it by one per loop. I want to know
why I could not use the index inside loop. Thanks!

Fan
 
C

Christopher Benson-Manica

Fan Zhang said:
int main()
{
int i;
double min, max,modl,tt;
max=PI/2;
for (i=1;i<=90;i++)
{
9---> min=i/radian;
}

Presumably your real code defines the variable "radian"; why don't you
post it? Whatever radian is, perhaps you've neglected to assign
something meaningful to it.
 
F

Fan Zhang

I forgot to mention that radian is a predefined value. It could not be the
problem, otherwise if I replace i with a double number the problem wont be
fixed.

Thanks for the input though.
 
M

Merrill & Michele

Dear group,

I wrote a simple code trying to use the integer index of a "for" loop inside
the loop. The idea is to convert it to double type and lead it into a
function. However it appears I can't call the integer number directly inside
the loop, otherwise, the results are not sensible.

It's not a good idea to toy with the dummy you're using for the loop. I'd
declare a double and then assign it inside the loop with a caste of the
dummy.
The code is as the following,

int main()
{
int i;
double min, max,modl,tt;
max=PI/2;

for (i=1;i<=90;i++)
{
9---> min=i/radian;
modl=background+Gaussian_quadrature(max,min);
printf("%d \t %6.4f \n",i,modl);
tt=tt+1.;
}
system("pause");
return 0;
}

Did this compile for you sans headers? My numerical analysis class was at
7:30 a.m., so I didn't pay much attention to things like background either
as I drooled on my desk. MPJ
 
M

Mike Wahler

Fan Zhang said:
I forgot to mention that radian is a predefined value.

From the perspective of the code you posted, no it's
not. Did you leave something out of your post? Without
seeing its definition (or at least your telling us exactly
what it is), we can't help much.
It could not be the
problem,

Famous last words. :)
otherwise if I replace i with a double number the problem wont be
fixed.

There are also other entites referred to in your
code for which you don't show any defintion (e.g.
'background' and 'Gaussian_quadrature')

You say your code gives incorrect results. So presumably
you have code which compiles (what you posted does not,
due to the missing definitions, as well as missing headers).

Post a compilable program.

And please don't top-post. Thanks.

-Mike
 
T

Thomas Matthews

Fan said:
Dear group,

I wrote a simple code trying to use the integer index of a "for" loop inside
the loop. The idea is to convert it to double type and lead it into a
function. However it appears I can't call the integer number directly inside
the loop, otherwise, the results are not sensible.

The code is as the following,

int main()
{
int i;
double min, max,modl,tt;
Prefer to define one variable per line. Makes modifications
easier.

max=PI/2;
Let us hope that PI is a double or a float.
This should be:
max = PI / 2.0;
The '.' indicates to the compiler to use a floating point
constant rather than an integral one.

for (i=1;i<=90;i++)
Should this be from zero to less than 90?
for (i = 0; i < 90; ++i)

{
9---> min=i/radian;
Well, you had better look up the type for "radian".
If it is an integer, you will have precision problems.
You are dividing an integer by either an integral or
floating point value (i.e. radian). You should have
your numbers either all floating point or all integral.
min = (double) i / /* (double) */ radian;

modl=background+Gaussian_quadrature(max,min);
Validate the return type of Gaussian_quadrature and
also of "background". Again, to maintain numeric
"correctness", have all values the same type in a
mathematical expression.

printf("%d \t %6.4f \n",i,modl);
tt=tt+1.;
Prefer to have digits after decimal points. This makes
reading easier and syntax errors fewer.
tt = tt + 1.0;

}
system("pause");
return 0;
}

I tried min= (double)i/radian in line 9 as well. It does not give the right
result either. Eventually, I defined another variable tt, initialized it to
be 1 outside of the loop and increased it by one per loop. I want to know
why I could not use the index inside loop. Thanks!

Fan

There are many problems here:
1. We have no idea how to differentiate expected results
from actual results. The above snippet program does
not compile.

2. Because you snippet is not complete, we cannot confirm
that your problems are not elsewhere (like maybe in
Gaussian_quadrature).

3. Please describe the requirements and behavior of your
complete program. This will help others help you.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
K

Keith Thompson

Fan Zhang said:
I forgot to mention that radian is a predefined value. It could not be the
problem, otherwise if I replace i with a double number the problem wont be
fixed.

We don't know that; if you don't post the actual code that you
compiled, we can't tell where the problem is.

Where is "radian" predefined? What does its declaration look like?

Reduce your program to a small example that exhibits the problem, one
that we can cut-and-paste, compile, and execute on our own systems.
If the workings of the Gaussian_quadrature function are irrelevant,
just print the values of min and max. Tells us what output you're
getting and what output you expected.

Looking at your original (incomplete) program, I see a call to
Gaussian_quadrature with arguments max and min, but I don't see an
initialization of min. We can only guess whether that's the problem.
Please don't make us guess.

Also, please don't top-post. In a followup, your response should
follow any quoted text. See any article in this newsgroup (including
this one) for an example.
 
F

Fan Zhang

Thanks for everybody's input. The problem was from the definition of radian.
I figured it out after reading the posts. Thanks a lot!
 
K

Keith Thompson

Fan Zhang said:
Thanks for everybody's input. The problem was from the definition of radian.
I figured it out after reading the posts. Thanks a lot!

Just out of curiosity, can you tell us (briefly) what the problem was?

And again, please don't top-post.
 
M

Merrill & Michele

Keith wrote:
Just out of curiosity, can you tell us (briefly) what the problem was?

And again, please don't top-post.

I'm curious too. Radian looks like a constant and to my knowledge, it's
held its value for the last 3 thousand years. Seeing whatever he has as
headers might help. MPJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top