I have some problem outputting double variable types.
Below is my script: - #include <stdio.h>
-
#include <math.h>
-
-
double x;
-
double h;
-
int n, choice;
-
-
void Euler(double x, double h, int n);
-
void RK4(double x, double h, int n);
-
double f(double x, double y);
-
double yexact(double x);
-
-
main()
-
{
-
printf("Enter the initial value of x: ");
-
scanf("%lf", &x);
-
-
printf("\nEnter step-size h: ");
-
scanf("%lf", &h);
-
-
printf("\nEnter the number of steps n: ");
-
scanf("%d", &n);
-
-
printf("\nEnter 0 for Euler Method or 1 for RK4: ");
-
scanf("%d", &choice);
-
-
-
while(choice < 0 || choice > 1)
-
{
-
printf("\nEnter 0 for Euler Method or 1 for RK4: ");
-
scanf("%d", &choice);
-
}
-
-
if(choice == 0)
-
{
-
Euler(x, h, n);
-
}
-
if(choice == 1)
-
{
-
RK4(x, h, n);
-
}
-
-
scanf("%d");
-
return 0;
-
}
-
-
-
/* Function for Euler Method */
-
-
void Euler(double x, double h, int n)
-
{
-
-
double Y, y, E;
-
int ctr;
-
Y = 1;
-
y = yexact(x);
-
printf("\nx_0 = %lf\nY_0 = 1\ny_0 = %lf\n", x, y);
-
-
for(ctr = 1; ctr < n+1; ctr++)
-
{
-
Y = Y + h*f(x, Y);
-
-
x = x + h;
-
y = yexact(x);
-
-
printf("\nx_%d = %lf\n", ctr, x);
-
printf("Y_%d = %lf\n", ctr, Y);
-
printf("y_%d = %lf\n", ctr, y);
-
E = Y - y;
-
printf("E_%d = %lf\n\n", ctr, E);
-
}
-
}
-
-
/* Function for RK4 */
-
-
void RK4(double x, double h, int n)
-
{
-
double Y, y, E;
-
int ctr;
-
double k_1, k_2, k_3, k_4;
-
-
Y = 1;
-
y = yexact(x);
-
printf("\nx_0 = %lf\nY_0 = 1\ny_0 = %lf\n", x, y);
-
-
for(ctr = 1; ctr < n+1; ctr++)
-
{
-
k_1 = h*f(x, Y);
-
k_2 = h*f(x + 0.5*h, Y + 0.5*k_1);
-
k_3 = h*f(x + 0.5*h, Y + 0.5*k_2);
-
k_4 = h*f(x + h, Y + k_3);
-
-
Y = Y + (k_1 + 2*k_2 + 2*k_3 + k_4)/6;
-
x = x + h;
-
y = yexact(x);
-
-
printf("\nx_%d = %lf\n", ctr, x);
-
printf("Y_%d = %lf\n", ctr, Y);
-
printf("y_%d = %lf\n", ctr, y);
-
E = Y - y;
-
printf("E_%d = %lf\n\n", ctr, E);
-
}
-
}
-
-
/* Function for f(x,y) */
-
-
double f(double a, double b)
-
{
-
double c;
-
c = -2*b -0.25*a*a + 0.125;
-
return c;
-
}
-
-
/* Function for y(x) */
-
-
double yexact(double d)
-
{
-
double y_exact;
-
y_exact = exp(-2*d) - 0.125*d*d + 0.125*d;
-
return y_exact;
-
}
-
What is displayed on the screen is only 7 digits, but I need it to be double precision. I can't see the problem, please help.
4 2051 DeMan 1,806
Top Contributor
%d print an int argument in decimal
%ld print a long int argument in decimal
%c print a character
%s print a string
%f print a float or double argument
%e same as %f, but use exponential notation
%g use %e or %f, whichever is better
%o print an int argument in octal (base 8)
%x print an int argument in hexadecimal (base 16)
%% print a single %
That is, you should use %f for doubles (or %e for scientific notation). You can also format the output to give (among other things) the sepecified number of decimal places so if
n=3.14159265358 97
printf("%.2f", n); //If memory serves me correct
would print 3.14
you can similarly force the number of characters before the decimal point eg
printf("%2.2f", n); //or similar
would print 03.14
searching for formatted print on the net could explain better....
I've tried forcing 13 digits but it just stops calculating correctly, just a load of zeros. I'm using Bloodshed Dev c++, could it be something to do with the compiler. Although I have tried everything I can think of.
DeMan 1,806
Top Contributor
sorry, tyhe biggest point I was trying to allude to is that to print a double you should be using %f (float, rather than %d (decimal integer) :)
I am using %lf for the doubles. But it's ok now it's been dealt with. It must be something picky with the compiler or something.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Peter Wright |
last post by:
How can VBA prevent the advisory popup "Outputting to ..." when using
Access 2002:
DoCmd.OutputTo acOutputReport, stDocName, acFormatRTF ?
Set Warnings is ignored.
TIA
Peter Wright
|
by: Jacek Dziedzic |
last post by:
Hello!
I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:
#include <iostream>
#include <iomanip>
using namespace std;
|
by: Andy |
last post by:
Hi
I'm really stuck outputting a double number to the console with three
decimal places if the furthest right value is a zero.
I can coutput the number 4.546 as 4.546 but then if I output 0.220 it comes
out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1.
How can I format it to include the zeros?
|
by: cephelo |
last post by:
I have no problems outputting the attribute value when the node is in
context, for example, @id when an <status> node is in context.
However, I am having trouble outputting it in a <xsl:value-of />
element:
<xsl:value-of select="procresults/settings@priority />
where it looks like
|
by: Diwa |
last post by:
In TC++PL 3edn, in section 21.2.2, Stroustrup
notes that put( ) and write( ) simply write chars.
Therefore, << for outputting chars need not be a member
What does he mean here ?
Also, class basic_ostream does not have
operator << (char ch)
| |
by: Peter Nimmo |
last post by:
Hi,
I am writting a windows application that I want to be able to act as if
it where a Console application in certain circumstances, such as error
logging.
Whilst I have nearly got it, it doesn't seem to write to the screen in
the way I would expect.
The output is:
|
by: Matt |
last post by:
Hello. I'm having a very strange problem that I would like ot check
with you guys.
Basically whenever I insert the following line into my programme to
output the arguments being passed to the programme:
printf("\nCommand line arguement %d: %s.", i , argv );
The porgramme outputs 3 of the command line arguements, then gives a
segmentation fault on the next line, followed by other strange
|
by: Markus |
last post by:
Little problem here.
How would i go about outputting "»" through giving something a value.
i.e.
if(validateEmail(emailValue) === false){
emailStyle.color = "red";
emailText.value = "&raqou; Invalid Email!";
} else {
|
by: billelev |
last post by:
This is probably a very easy question to answer:
I have been outputting some text to a message box, similar to the following:
strOutput = "---" & Chr(10) & Chr(10)
strOutput = strOutput & "VAR:" & AddTabs(1) & Format(rsVar!, "currency")
strOutput = strOutput & Chr(10) & Chr(10)
strOutput = strOutput & "Position Value:" & AddTabs(1) & Format(rsVar!, "currency") & Chr(10)
strOutput = strOutput & "Position Risk:" & AddTabs(1) &...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, 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: 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, Z-Wave, Wi-Fi, 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 no-code 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN 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
| |