#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Floating number error


    can anyone find the mistake in the following code:


    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    int degree;
    float fun01coff[100];
    float fun02coff[100];
    void main()
    {
    printf(" enter the degree of equation\n\n");
    scanf("\n%d", &degree);
    printf(" enter the coeffiecients\n\n");
    int i =0;
    for(i =0; i<= degree; i++)
    {
    scanf("\n %d", &fun01coff[i]);
    }
    int deg = degree;
    float res = 0.0;
    for( i = 0; i<= deg; i++)
    {
    printf("\n printing value of i = %d",i);
    res = res + (float)(fun01coff[i]*(pow(0,deg-i)));
    printf(" \n\n answer = %f", res);
    }
    }


    i wanted to find the value of polynomial for some value of x such as x = 0
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    Well the first mistake is that you've been here for nearly a year, but you're still posting code without using [code][/code] tags.

    So consider your post as being ignored for the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,087
    Rep Power
    2222
    Also, just exactly why do you think that there's any mistake in your code?

    IOW, what are the symptoms that lead you to believe that there's a mistake? We cannot read your mind and we don't have time to play stupid guessing games.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    This line is wrong:
    Code:
     scanf("\n %d", &fun01coff[i]);
    You've declared fun01coff as float. You shouldn't be using %d to read into it. I'll leave it to you to figure out what the correct format string should be, as a homework assignment :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo