Can someone please tell me where I am going wrong??

D

Dildo Boy

I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...

Here is what I have so far and have been tinkering with it for EVER!
any help would be greatly appreciated, thanks guys...

#include <stdio.h>
#include <math.h>


int main() {

int i,n;
double x;
float sum, term;

printf("Enter a number in the interval [1,2]> ");
scanf("%d", &n);



sum = 0;
i = 0;

do {
i = i+1;
x = (double) i;
term = pow(x, i)/i;
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;

} while(i <= n);

printf("The answer is %lf\n",sum);
}
 
O

osmium

Dildo Boy said:
I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...

Here is what I have so far and have been tinkering with it for EVER!
any help would be greatly appreciated, thanks guys...

#include <stdio.h>
#include <math.h>


int main() {

int i,n;
double x;
float sum, term;

printf("Enter a number in the interval [1,2]> ");
scanf("%d", &n);



sum = 0;
i = 0;

do {
i = i+1;
x = (double) i;
term = pow(x, i)/i;
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;

} while(i <= n);

printf("The answer is %lf\n",sum);
}

Hey there, Dildo Boy. Did you also save America?
 
B

Ben Pfaff

Dildo Boy said:
int main() {

int i,n;
double x;
float sum, term;

printf("Enter a number in the interval [1,2]> ");
scanf("%d", &n);

The only integers in that interval are 1 and 2. Do you want to
read a floating-point number instead? If so, change n to type
double and use %lf instead of %d.
sum = 0;
i = 0;

do {
i = i+1;

Most C programmers would simply write "i++" here.
x = (double) i;

The cast is superfluous.
term = pow(x, i)/i;
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;

} while(i <= n);

I doubt this is the correct termination condition, because as
written it would cause the loop body to execute no more than 3
times given n in the range [1,2].
printf("The answer is %lf\n",sum);

%f is the correct printf format specifier for a double.

You should return a value from main (probably 0).

I won't comment on whether you're using a correct numerical
method here, because I don't know.

Finally: it's a little odd that you use variable "x" here just to
assign the value of another variable which is used exactly once.
I would probably just write your assignment to "term" as
term = pow(i, i)/i;
and drop variable "x" entirely.
 
D

Daniel Rudy

At about the time of 3/5/2007 8:55 AM, Dildo Boy stated the following:
I am trying to write a program which asks the user to enter a number
in the interval [1,2], the program then gives the natural logarithm of
that number, using the series for log(x+1)...

Here is what I have so far and have been tinkering with it for EVER!
any help would be greatly appreciated, thanks guys...

#include <stdio.h>
#include <math.h>


int main() {

Needs to read as:

int main(void)
int i,n;
double x;
float sum, term;

make x, sum, and term should all be either float or double.

printf("Enter a number in the interval [1,2]> ");
scanf("%d", &n);



sum = 0;
i = 0;

do {

I think a for loop would be better here:

for (i = 0; i < n; i++)
i = i+1;
x = (double) i;
term = pow(x, i)/i;
if(i % 2 == 1)
sum = sum + term;
else
sum = sum - term;

} while(i <= n);

printf("The answer is %lf\n",sum);
}

This program is not numerically accurate according to the formula that
you gave earlier when you said that you "saved America".


--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
B

Beej Jorgensen

Daniel Rudy said:
Needs to read as:

int main(void)

Actually I think it's ok and believe the two to be equivalent. (I
almost posted this question a couple days ago.)

(The definite gripe is with void main() as opposed to int main().)

Data points:

The C FAQ says int main() is acceptable.
http://c-faq.com/ansi/maindecl.html

The C99 Standard (9899:TC2--maybe fixed in final?) uses int main() in
two examples. [6.5.3.4p7, 6.7.5.3p20]

Rabid-pedantic gcc refuses to warn on int main().

Support:

C99 Introduction:

# Certain features are obsolescent, which means that they may be
# considered for withdrawal in future revisions of this International
# Standard. They are retained because of their widespread use, but their
# use in new implementations (for implementation features) or new
# programs (for language [6.11] or library features [7.26]) is
# discouraged.

C99 6.11.6, Function declarators:

# The use of function declarators with empty parentheses (not
# prototype-format parameter type declarators) is an obsolescent
# feature.

C99 6.7.5.3p10, Function declarators (including prototypes):

# The special case of an unnamed parameter of type void as the only item
# in the list specifies that the function has no parameters.

--p14:

# An identifier list declares only the identifiers of the parameters of
# the function. An empty list in a function declarator that is part of a
# definition of that function specifies that the function has no
# parameters. The empty list in a function declarator that is not part
# of a definition of that function specifies that no information about
# the number or types of the parameters is supplied.

Enough of this. I need to actually work. :)

-Beej
 
D

Daniel Rudy

At about the time of 3/5/2007 2:20 PM, Beej Jorgensen stated the following:
Daniel Rudy said:
Needs to read as:

int main(void)

Actually I think it's ok and believe the two to be equivalent. (I
almost posted this question a couple days ago.)

(The definite gripe is with void main() as opposed to int main().)

Data points:

The C FAQ says int main() is acceptable.
http://c-faq.com/ansi/maindecl.html

The C99 Standard (9899:TC2--maybe fixed in final?) uses int main() in
two examples. [6.5.3.4p7, 6.7.5.3p20]

Rabid-pedantic gcc refuses to warn on int main().

I agree that void main() is a bad idea, but int main() on gcc is just
for one compiler. Who knows what another compiler will do with that
construct. The best thing to do is int main(void). That removes all
doubt about what your intentions are.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
F

Flash Gordon

Daniel Rudy wrote, On 06/03/07 02:58:
At about the time of 3/5/2007 2:20 PM, Beej Jorgensen stated the following:
Daniel Rudy said:
int main() {
Needs to read as:

int main(void)
Actually I think it's ok and believe the two to be equivalent. (I
almost posted this question a couple days ago.)

(The definite gripe is with void main() as opposed to int main().)

Data points:

The C FAQ says int main() is acceptable.
http://c-faq.com/ansi/maindecl.html

The C99 Standard (9899:TC2--maybe fixed in final?) uses int main() in
two examples. [6.5.3.4p7, 6.7.5.3p20]

Rabid-pedantic gcc refuses to warn on int main().

I agree that void main() is a bad idea, but int main() on gcc is just
for one compiler. Who knows what another compiler will do with that
construct. The best thing to do is int main(void). That removes all
doubt about what your intentions are.

int main() is guaranteed to work by the C standard. The problem is it is
not guaranteed to produce a diagnostic if you do something like:

int main()
{
return main(3);
}

Where as this will definitely generate a diagnostic on any conforming
implementation:
int main(void)
{
return main(3);
}

Of course, this example is very stupid, but there are times where it
makes sense to call main recursively and the full prototype form ensures
that you pass (or don't pass as the case may be) compatible parameters.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top