473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

integer index problem

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

Nov 14 '05 #1
8 1619
Fan Zhang <fa******@sas.upenn.edu> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2
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.
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:ck**********@chessie.cirr.com...
Fan Zhang <fa******@sas.upenn.edu> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Nov 14 '05 #3

"Fan Zhang" wrote in message
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
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

Nov 14 '05 #4
"Fan Zhang" <fa******@sas.upenn.edu> wrote in message
news:ck***********@netnews.upenn.edu...
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
Nov 14 '05 #5
Fan Zhang wrote:
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

Nov 14 '05 #6
"Fan Zhang" <fa******@sas.upenn.edu> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Thanks for everybody's input. The problem was from the definition of radian.
I figured it out after reading the posts. Thanks a lot!
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:dl******************@newssvr33.news.prodigy.c om...
Fan Zhang wrote:
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

Nov 14 '05 #8
"Fan Zhang" <fa******@sas.upenn.edu> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: johnny | last post by:
Can anyone tell me what is wrong with the following code fragment? char a = x; char b = y; int result = strtol(&a, NULL, 10) + strtol(&b, NULL, 10); I got the same result using...
1
by: jprunier | last post by:
Hi I isolated this code which gives me odd results and I am not sure to understand why. Basically I declared "positions" as a map that I am using to store pairs of integers. The key used for the...
2
by: John | last post by:
Hello everyone, I'm currently writing a program to keep track of schedule changes at a school. The goal is to have someone using the program to declare changes, then the program writes a html...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
5
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was...
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
21
by: no1zson | last post by:
I do not even know how to correctly ask this question. I have an item field in the code I am about to post. Simple intger meant to be an item number for a cd. The user enters this number. Over the...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.