473,396 Members | 2,099 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,396 software developers and data experts.

Outputting problem

I have some problem outputting double variable types.

Below is my script:


Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2. #include <math.h>
  3.  
  4. double x;
  5. double h;
  6. int n, choice;
  7.  
  8. void Euler(double x, double h, int n);
  9. void RK4(double x, double h, int n);
  10. double f(double x, double y);
  11. double yexact(double x);
  12.  
  13. main()
  14. printf("Enter the initial value of x: ");
  15. scanf("%lf", &x);
  16.  
  17. printf("\nEnter step-size h: ");
  18. scanf("%lf", &h);
  19.  
  20. printf("\nEnter the number of steps n: ");
  21. scanf("%d", &n);
  22.  
  23. printf("\nEnter 0 for Euler Method or 1 for RK4: ");
  24. scanf("%d", &choice); 
  25.  
  26.  
  27. while(choice < 0 || choice > 1)
  28. {
  29. printf("\nEnter 0 for Euler Method or 1 for RK4: ");
  30. scanf("%d", &choice);
  31. }
  32.  
  33. if(choice == 0)
  34. {
  35. Euler(x, h, n); 
  36. }
  37. if(choice == 1)
  38. {
  39. RK4(x, h, n);
  40. }
  41.  
  42. scanf("%d");
  43. return 0;
  44. }
  45.  
  46.  
  47. /* Function for Euler Method */
  48.  
  49. void Euler(double x, double h, int n)
  50.  
  51. double Y, y, E;
  52. int ctr; 
  53. Y = 1;
  54. y = yexact(x);
  55. printf("\nx_0 = %lf\nY_0 = 1\ny_0 = %lf\n", x, y);
  56.  
  57. for(ctr = 1; ctr < n+1; ctr++)
  58. {
  59. Y = Y + h*f(x, Y);
  60.  
  61. x = x + h;
  62. y = yexact(x);
  63.  
  64. printf("\nx_%d = %lf\n", ctr, x);
  65. printf("Y_%d = %lf\n", ctr, Y);
  66. printf("y_%d = %lf\n", ctr, y);
  67. E = Y - y;
  68. printf("E_%d = %lf\n\n", ctr, E);
  69. }
  70. }
  71.  
  72. /* Function for RK4 */
  73.  
  74. void RK4(double x, double h, int n)
  75. {
  76. double Y, y, E;
  77. int ctr;
  78. double k_1, k_2, k_3, k_4; 
  79.  
  80. Y = 1;
  81. y = yexact(x);
  82. printf("\nx_0 = %lf\nY_0 = 1\ny_0 = %lf\n", x, y);
  83.  
  84. for(ctr = 1; ctr < n+1; ctr++)
  85. {
  86. k_1 = h*f(x, Y);
  87. k_2 = h*f(x + 0.5*h, Y + 0.5*k_1);
  88. k_3 = h*f(x + 0.5*h, Y + 0.5*k_2);
  89. k_4 = h*f(x + h, Y + k_3);
  90.  
  91. Y = Y + (k_1 + 2*k_2 + 2*k_3 + k_4)/6;
  92. x = x + h;
  93. y = yexact(x);
  94.  
  95. printf("\nx_%d = %lf\n", ctr, x);
  96. printf("Y_%d = %lf\n", ctr, Y);
  97. printf("y_%d = %lf\n", ctr, y);
  98. E = Y - y;
  99. printf("E_%d = %lf\n\n", ctr, E);
  100. }
  101.  
  102. /* Function for f(x,y) */
  103.  
  104. double f(double a, double b)
  105. {
  106. double c;
  107. c = -2*b -0.25*a*a + 0.125;
  108. return c;
  109. }
  110.  
  111. /* Function for y(x) */
  112.  
  113. double yexact(double d)
  114. {
  115. double y_exact;
  116. y_exact = exp(-2*d) - 0.125*d*d + 0.125*d;
  117. return y_exact;
  118. }
  119.  
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.
Dec 12 '06 #1
4 2025
DeMan
1,806 1GB
%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....
Dec 12 '06 #2
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.
Dec 12 '06 #3
DeMan
1,806 1GB
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) :)
Dec 14 '06 #4
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.
Dec 15 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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
13
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...
2
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...
2
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...
3
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,...
4
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...
17
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...
1
Markus
by: Markus | last post by:
Little problem here. How would i go about outputting "&raquo;" through giving something a value. i.e. if(validateEmail(emailValue) === false){ emailStyle.color = "red"; emailText.value...
12
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 &...
0
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
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
marktang
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,...
0
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...
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...
0
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...

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.