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 2025
%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.1415926535897
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.
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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 &...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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: 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...
| |