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 can't figure out what i'm doing wrong.
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", &x);
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); 15 2718
EDot wrote:
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 can't figure out what i'm doing wrong.
any help would be greatly appreciated, thanks guys...
#include <stdio.h>
#include <math.h>
int main() {
If main is not going to accept command line parameters, specify void.
int i,n;
double x;
float sum, term;
Why not make sum and term double as well?
printf("Enter a number in the interval [1,2]");
Either end the printf string with a newline or call fflush(stdout)
just after printf. Otherwise, your output may appear delayed because
of buffering.
scanf("%d", &x);
You're telling scanf to look for a decimal integer value and store it
into a double object. Are you sure that this is what you want?
sum = 0;
i = 0;
do {
i = i+1;
x = (double) i;
And you're overwriting your previous value in x.
term = pow(x, i)/i;
Here x and i will have the same value due to your previous assignment.
if(i % 2 == 1)
sum = sum + term;
else
sum = sum  term;
} while(i <= n);
You're comparing i against an uninitialised value, (n); Undefined
behaviour.
printf("The answer is %lf\n",sum);
For printing a floatpoint object use the %f format specifier. The %lf
format specifier was added with C99, as an alternative, but many
compilers don't yet fully support it.
EDot wrote:
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)...
it would be better if you posted all of the assignment.
Here is what I have so far and can't figure out what i'm doing wrong.
perhaps if you said what the symptom was... What is going wrong?
#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", &x);
sum = 0;
i = 0;
do {
i = i+1;
could use (same result, but less to type)
i++;
x = (double) i;
term = pow(x, i)/i;
you can avoid calling pow() in the loop. What is the difference
between pow(x,i) and pow(x,i+1), is there a pattern?
if(i % 2 == 1)
sum = sum + term;
or
sum += term;
else
sum = sum  term;
} while(i <= n);
this isn't going to give you six decimal places is it?
And n is not initialised.
>
printf("The answer is %lf\n",sum);
if you did what the assignment said and printed x, sum and log(x)
you might have been able to debug this yourself (x would be wrong)

Nick Keighley
Nick Keighley wrote:
EDot wrote:
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)...
note that's log(x + 1), not log(x)...
<snip>

Nick Keighley
"EDot" writes:
>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)...
<snip>
You are still fighting basic I/0 instead of the thing your instructor is
dwelling on. I/O, in any language, is a bitch, plain and simple. You seem
to have tried so here is a skeleton you should be able to use as a test rig.
The stuff just before return 3.1416 needs a bit of refinement, which I will
leave to you. The getchar() sprinkled around are to make life simpler when
debugging with the compiler I happen to use. You may not need them.
#include <stdio.h>
#include <stdlib.h /* exit() */
double ln(double x)
{
if( x<1  x>+1)
{
printf("Argumen t provided to function ln out of range.\nAbortin g\n");
fflush(NULL);
getchar();
getchar();
exit(1);
}
/* Need something here ... */
return 3.1416;
}
/*************** ***/
int main(void)
{
double z, y;
printf("Enter a decimal number in the range 1..2\n");
scanf("%lf", &z);
y = ln(z1);
printf("Natural log of %f is %f\n", z, y);
fflush(NULL);
getchar();
getchar();
return 0;
}
On Mar 6, 3:16 am, "Nick Keighley" <nick_keighley_ nos...@hotmail. com>
wrote:
Nick Keighley wrote:
EDot wrote:
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)...
note that's log(x + 1), not log(x)...
The series expansion for log(x+1) is:
x  x^2/2 + x^3/3  x^4/4 ...
Handbook of Mathematical Functions, equation 4.1.24: http://www.math.sfu.ca/~cbm/aands/page_68.htm
user923005 wrote:
Nick Keighley wrote:
>EDot wrote:
>>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)...
note that's log(x + 1), not log(x)...
The series expansion for log(x+1) is:
x  x^2/2 + x^3/3  x^4/4 ...
Handbook of Mathematical Functions, equation 4.1.24: http://www.math.sfu.ca/~cbm/aands/page_68.htm
#include <stdlib.h>
double ln(double x) {
double lnx, lnxold, delta;
int n;
if ((x <= 0)  ((x = x  1.0) >= 1.0)) exit(EXIT_FAILU RE);
lnxold = 0; delta = x; n = 1;
while ((lnx = lnxold + delta) != lnxold) delta *= x / ++n;
return lnx;
} /* untested, known to be slow */

<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
"A man who is right every time is not likely to do very much."
 Francis Crick, codiscover of DNA
"There is nothing more amazing than stupidity in action."
 Thomas Matthews
CBFalconer wrote:
>
user923005 wrote:
Nick Keighley wrote:
EDot wrote:
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)...
note that's log(x + 1), not log(x)...
The series expansion for log(x+1) is:
x  x^2/2 + x^3/3  x^4/4 ...
Handbook of Mathematical Functions, equation 4.1.24: http://www.math.sfu.ca/~cbm/aands/page_68.htm
#include <stdlib.h>
double ln(double x) {
double lnx, lnxold, delta;
int n;
if ((x <= 0)  ((x = x  1.0) >= 1.0)) exit(EXIT_FAILU RE);
lnxold = 0; delta = x; n = 1;
while ((lnx = lnxold + delta) != lnxold) delta *= x / ++n;
return lnx;
} /* untested, known to be slow */
Correction, above is flawed: It also sums the wrong series! as
does the following. I leave it to the student to correct the value
of delta.
#include <stdio.h>
#include <stdlib.h>
double ln(double x) {
double lnx, lnxold, delta;
int n;
if ((x <= 0)  ((x = x  1.0) >= 1.0)) exit(EXIT_FAILU RE);
lnxold = 0; delta = x; n = 1;
while ((lnx = lnxold + delta) != lnxold) {
delta *= x / ++n;
lnxold = lnx;
}
return lnx;
} /* known to be slow */
/*  */
int main(int argc, char* *argv) {
double x;
if ((argc != 2)  (1 != sscanf(argv[1], "%lf", &x))) {
fprintf(stderr, "Usage: lnx value (>0 and <2\n");
exit(EXIT_FAILU RE);
}
printf("Ln(%f) = %f\n", x, ln(x));
return 0;
}

<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
"A man who is right every time is not likely to do very much."
 Francis Crick, codiscover of DNA
"There is nothing more amazing than stupidity in action."
 Thomas Matthews
On Tue, 06 Mar 2007 21:40:38 0500, CBFalconer <cb********@yah oo.com>
wrote:
>CBFalconer wrote:
>> user923005 wrote:
Nick Keighley wrote: EDot wrote:
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)...
note that's log(x + 1), not log(x)...
The series expansion for log(x+1) is:
x  x^2/2 + x^3/3  x^4/4 ...
Handbook of Mathematical Functions, equation 4.1.24: http://www.math.sfu.ca/~cbm/aands/page_68.htm #include <stdlib.h>
double ln(double x) { double lnx, lnxold, delta; int n;
if ((x <= 0)  ((x = x  1.0) >= 1.0)) exit(EXIT_FAILU RE); lnxold = 0; delta = x; n = 1; while ((lnx = lnxold + delta) != lnxold) delta *= x / ++n; return lnx; } /* untested, known to be slow */
Correction, above is flawed: It also sums the wrong series! as does the following. I leave it to the student to correct the value of delta.
#include <stdio.h> #include <stdlib.h>
double ln(double x) {
double lnx, lnxold, delta;
int n;
if ((x <= 0)  ((x = x  1.0) >= 1.0)) exit(EXIT_FAILU RE);
lnxold = 0; delta = x; n = 1;
while ((lnx = lnxold + delta) != lnxold) {
delta *= x / ++n;
lnxold = lnx;
}
return lnx; } /* known to be slow */
/*  */
int main(int argc, char* *argv) {
double x;
if ((argc != 2)  (1 != sscanf(argv[1], "%lf", &x))) {
fprintf(stderr, "Usage: lnx value (>0 and <2\n");
exit(EXIT_FAILU RE);
}
printf("Ln(%f) = %f\n", x, ln(x));
return 0; }
I don't get it.
When I run your program with an argument of 5.0, it returns
EXIT_FAILURE. But in my Windows Vista Calculator application, ln(5)
returns 1.6094379124341 003746007593332 262. I suspect the Windows Vista
Calculator is correct and your program is flawed, and the reason your
program is flawed has nothing to do with the value of deltanot
surprising (in my experience) for open source software.

jay
It's here: http://www.microsoft.com/windows/products/windowsvista/
jaysome said:
<snip>
I don't get it.
When I run [CBFalconer's] program with an argument of 5.0, it returns
EXIT_FAILURE. But in my Windows Vista Calculator application, ln(5)
returns 1.6094379124341 003746007593332 262. I suspect the Windows Vista
Calculator is correct and your program is flawed,
No, the program is correct, and Windows Vista Calculator is also
correct. But they do different things.
You told the calculator to give you ln(5), and (presumably) it did
precisely that (unless you have any nonDRMenabled equipment attached
to your machine, in which case I am given to understand that Windows
Vista may randomly corrupt your data  what fun!).
But you told the *program*, which is designed to tell you the logarithm
of a value in the interval [1,2], that the value in the interval [1,2]
that you had selected was 5. The program rightly rejected this value as
not being in the proper range for which the program was designed.
See the OP for details.

Richard Heathfield
"Usenet is a strange place"  dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain,  www. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: Thomas G. Marshall 
last post by:
This message is sent to these newsgroups because they are no longer
valid:
comp.lang.java
comp.lang.java.api
comp.lang.java.bugs
comp.lang.java.misc
comp.lang.java.setup
comp.lang.java.tech

by: WhataTool 
last post by:
I'm going out out of my mind trying to get this to work with no luck. The
error message I get is at the bottom. Can someone please tell me what I'm
doing wrong here. I've tried this a million different ways and can't get it
to work. I can get it to work with VBScript, but I need to do this project
in JavaScript. HELP PLEASE!?
<%@Language=JavaScript%>
<%Response.buffer=true%>
<%

by: free2cric 
last post by:
Hi,
I attanded an interview on C++
Question asked were and my answers to them..
1. In a CPP program what does memory leak occure?
 i said.. In a constructor , the pointer variables were assigned
a memory block but the programmer forgets to free them in the
destructor by usng delete operator.
2. ok, the delete operator is used .. variables were freed. but still

by: Franco, Gustavo 
last post by:
Hi, I have a question, and please I need a answer.
How can I finalize a thread running with Application.Run (I need the message
loop!!!) without call Thread.Abort?.
I want to call Application.ExitThread in the same thread that it is running.

by: David Van D 
last post by:
Hi there,
A few weeks until I begin my journey towards a degree in Computer
Science at Canterbury University in New Zealand,
Anyway the course tutors are going to be teaching us JAVA wth bluej and
I was wondering if anyone here would be able to give me some tips for
young players such as myself, for learning the language.
Is this the best Newsgroup for support with JAVA?
 
by: Martin Jørgensen 
last post by:
Hi,
In continuation of the thread I made "perhaps a stack problem? Long
calculations  strange error?", I think I now got a "stable" error,
meaning that the error always seem to come here now (tried: visual
studio 2005 + linux/macintosh gcc)...
That's a pretty good thing. I think the error still appears using both
gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't
know the difference)  but since so many functions...

by: Amali 
last post by:
I'm newdie in c programming. this is my first project in programming.
I have to write a program for a airline reservation. this is what i
have done yet. but when it runs it shows the number of seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.
#include<stdio.h>
#include<ctype.h>
#include<conio.h>

by: Dildo Boy 
last post by:
I am trying to write a program which asks the user to enter a number
in the interval , 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>

by: aslamhenry 
last post by:
please key in any 5 digits number : 56789
and the ouput is
5678 9
567 89
56 789
5 6789

by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...

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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
 
by: Hystou 
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...

by: tracyyun 
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, ZWave, WiFi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...

by: TSSRALBI 
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LANtoLAN VPNs.
The last exercise I practiced was to create a LANtoLAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

by: adsilva 
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

by: 6302768590 
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
 
by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 