![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Simple program odd results
Ive been out of coding practice for several months now so my debugging is rusty. I cant seem to figure out why Im getting the results that I am. Its a simple program used to aid my calculations in a diff. eq. course.
#include<stdio.h>
#include<math.h>
int main()
{
float y;
float f;
float h;
int n = 0;
float t;
while(1){
printf("Enter starting time: \n");
scanf("%f", &t);
printf("Enter starting y value: \n");
scanf("%f", &y);
printf("Enter time step(h): \n");
scanf("%f", &h);
f = 3 + t - y;
printf("y(%f) = %f\n", t, y);
printf("f(%f,%f) = %f\n", t, y, f);
for(n = 0; n <= 1/h; n++)
{
printf("n = %f\n", n);
n = n+1;
printf("n = %f\n", n);
t = t + h;
y = y + f*h;
printf("y(%f) = %f \n", t,y);
f = 3 + t - y;
printf("f(%f,%f) = %f\n", t, y, f);
}
}
return 0;
}Enter starting time: 0 Enter starting y value: 1 Enter time step(h): 0.05 y(0.000000) = 1.000000 f(0.000000,1.000000) = 2.000000 n = 0.000000 n = 0.000000 y(0.050000) = 1.100000 f(0.050000,1.100000) = 1.950000 n = 0.050000 n = 0.050000 y(0.100000) = 1.197500 f(0.100000,1.197500) = 1.902500 n = 0.100000 n = 0.100000 y(0.150000) = 1.292625 f(0.150000,1.292625) = 1.857375 n = 0.150000 n = 0.150000 y(0.200000) = 1.385494 f(0.200000,1.385494) = 1.814506 n = 0.200000 n = 0.200000 y(0.250000) = 1.476219 f(0.250000,1.476219) = 1.773781 n = 0.250000 n = 0.250000 y(0.300000) = 1.564908 f(0.300000,1.564908) = 1.735092 n = 0.300000 n = 0.300000 y(0.350000) = 1.651663 f(0.350000,1.651663) = 1.698337 n = 0.350000 n = 0.350000 y(0.400000) = 1.736580 f(0.400000,1.736580) = 1.663420 n = 0.400000 n = 0.400000 y(0.450000) = 1.819751 f(0.450000,1.819751) = 1.630250 n = 0.450000 n = 0.450000 y(0.500000) = 1.901263 f(0.500000,1.901263) = 1.598737 The for loop seems to be the root of the problem, and I wasnt getting expected behavior from the "n" variable so I tried to brute force a behavior (n = n + 1) but I still get back the previous value for n, and n seems to always increase by h no matter what I specify in the loop. I know this is a very trivial problem, but I cant figure out what the bug is. Thanks for the help. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Re: Simple program odd results
My window for editing has expired and I thought I should add that Im getting correct approximations independant of the step size used. "n" just seems to not be playing nicely.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Re: Simple program odd results
for(n = 0; n <= 1/h; n++)
{
printf("n = %f\n", n);
n = n+1;Why are you incrementing n twice in the same for loop? The n++ in the loop declaration performs the n = n+1 for you.Edit: And as a side note. The <= comparator for an integer and float might not behave exactly as you expect. Look at the following sample. It says that 20 is not <= 20.0. But change n to 19, and you get True. C Syntax (Toggle Plain Text)
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Re: Simple program odd results
yes, using a float in the condition of a for loop isnt a good idea, but Im not sure if thats really the problem since it does execute the correct number of loops. I did discover that the printf statement of n has something to do with the odd results. Since its declared as an int and I try to print a float bad things happened and I would appreciate some insight into why.
#include<stdio.h>
int main()
{
int n = 0;
int i = 0;
for(i = 0; i < 10; i++)
{
printf("n = %f\n", n);
n += 1;
}
return 0;
}Outputs: n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 n = -0.025121 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5
![]() |
Re: Simple program odd results
Your results are hardly surprising. You are attempting to print n out, but the %f format specifier tells printf() that n is a (double) floating point value. printf() will not magically convert n to a floating point value; it will print out the floating point value that happens to be represented with a double containing the same set of bits as the int named n.
Formally, passing an argument to printf() that doesn't match the corresponding format (eg using %f for an int) yields undefined behaviour. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PLS Help me in a very simple (noob) program about textfields and textareas. | javaN00b | Java | 3 | Mar 29th, 2006 9:45 PM |
| Simple program with IE window | cygnusx | C++ | 3 | Feb 2nd, 2006 4:48 PM |
| problem...a simple program... | n00b | C++ | 13 | Sep 22nd, 2005 5:08 AM |
| Simple VB Program | jaymunee80 | Visual Basic | 2 | Jul 6th, 2005 4:56 AM |