Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 3rd, 2008, 4:41 PM   #1
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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;
}
and the results I get for t = 0, y = 1, h = 0.05 are:
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.
sixstringartist is offline   Reply With Quote
Old Feb 3rd, 2008, 5:29 PM   #2
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Feb 3rd, 2008, 6:23 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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)
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5. float f = 0.05;
  6. int n = 20;
  7.  
  8. printf("%f\n", 1/f);
  9. printf("%d\n", n);
  10. printf("%s\n", n <= 1/f ? "True" : "False");
  11. printf("\n");
  12.  
  13. return 0;
  14.  
  15. }
Sane is offline   Reply With Quote
Old Feb 3rd, 2008, 11:13 PM   #4
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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
but if I change the %f in the print statement to a %d I get the expected results.
sixstringartist is offline   Reply With Quote
Old Feb 4th, 2008, 2:29 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC