Floating point error

This is a discussion on Floating point error within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> void main() { float x[20],fx[20],p[20]={1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; int i,n,j; float a,b; float z; float sum=0.0; clrscr(); printf("Enter the number ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    Floating point error

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     float x[20],fx[20],p[20]={1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
     int i,n,j;
     float a,b;
     float z;
     float sum=0.0;
     clrscr();
     printf("Enter the number of interpolating points ");
     scanf("%d",&n);
     for(i=0;i<n;i++)
     {
      printf("\nEnter the values of x and f(x) ");
      scanf("%f %f",x[i],fx[i]);
     }
     printf("\nEnter the value at which you wish to find the values of Lagrangee's polynomial ");
     scanf("%f",&z);
     for(i=0;i<n;i++)
     {
      for(j=0;j<n;j++)
      {
       if(i==j)
        j++;
       a=(z-x[i+j]);
       b=(x[i]-x[i+j]);
       p[i]=p[i]*(a/b);
      }
     }
     for(i=0;i<n;i++)
     {
      sum=sum+p[i]*fx[i];
     }
     printf("%f",sum);
     getch();
    }
    This program is showing no compilation and linking error but showing
    "Floating point errorivide by 0
    Null pointer assignment"
    Why is it so?
    Please help in finding out the error.


    Please reply quickly...

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    use int main() not void main()

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    scanf("%f %f",x[i],fx[i]);

    scanf needs pointers to variables, like

    scanf("%f",&z);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    After correcting the scanf problem it is still showing the error "Floating point error divide by 0"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    Use a debugger to help you find the problem. Since the error message mentions division by zero, put a break point around the places where division is performed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    How to debug please help!!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I doubt you want x[i+j] but x[j] instead -- you're treating j as a proper index in the for loop, not the delta between two indices.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    21
    thx to all.I have finally cracked it...

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Quote Originally Posted by ankitsinghal_89 View Post
    How to debug please help!!
    For Windows, Visual Studio is a great tool. There is also the command-line GDB and Microsoft's command-line WinDbg.
    On Linux, there is only the command-line-ish GDB.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    For Windows, Visual Studio is a great tool. There is also the command-line GDB and Microsoft's command-line WinDbg.
    On Linux, there is only the command-line-ish GDB.
    Actually there is a GUI gdb called ddd.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21