473,795 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Outputting problem

3 New Member
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 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....
Dec 12 '06 #2
superja
3 New Member
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 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) :)
Dec 14 '06 #4
superja
3 New Member
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
2422
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
2793
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;
2
2775
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?
2
1477
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
3
1322
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)
4
10518
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:
17
3383
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
1
1214
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 = "&raqou; Invalid Email!"; } else {
12
13428
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) &...
0
9519
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,...
0
10437
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, 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...
1
10164
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,...
0
10001
tracyyun
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...
0
9042
agi2029
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...
1
7538
isladogs
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...
0
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 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.