Find the answer to your Linux question:
Results 1 to 6 of 6
i am facing problem in output after 10 iteration when i am using this command #include<stdio.h> main() { float x=1.0; do { x=x+0.05; printf("\n x=%f",x); } return 0; }...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    1

    do while output error

    i am facing problem in output after 10 iteration when i am using this command

    #include<stdio.h>
    main()
    {
    float x=1.0;
    do
    {
    x=x+0.05;
    printf("\n x=%f",x);
    }
    return 0;
    }

  2. #2
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,344
    The while bit seems to be missing. I think your code should be something like
    Code:
    #include <stdio.h>
    int main(void)
    {
        float x = 1.0;
    
        do{
               x+=0.05;
               printf("\nx = &#37;f", x);
            }
            while (Some Condition)
    
        return 0;
    }

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Location
    Meerut, Up, India
    Posts
    6

    Error in the output of the C program

    I am facing problem in the out put of the following Programs (after 10 iteration output is wrong)

    #include<stdio.h>
    main()
    {
    float x = 1.0;
    do{
    x = x+0.05;
    printf("\n x = %f", x);
    }while(x<2.0);
    return 0;
    }

  4. #4
    Just Joined! vijay_kernel's Avatar
    Join Date
    Apr 2007
    Posts
    77
    There is no conditional expression like,

    Wrong syntax:

    do
    {
    statment 1;
    statment 2;
    .......
    }

    So you need to follow the below one ,

    Correct syntax:

    do
    {
    statment 1;
    statment 2;
    .......
    }while(condition);

    OR

    while(condition)
    {
    statment 1;
    statment 2;
    .......
    }

    Or if you are new to the C programming then you need to refer some good basic C tutorial.

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Location
    Meerut, Up, India
    Posts
    6

    Wrong output of the program though the code is written correctly

    I am facing problem in the output of the following Programs (after 10 iteration output is wrong)

    #include<stdio.h>
    int main()
    {
    float x = 1.0;
    do{
    x = x+0.05;
    printf("\n x = %f", x);
    }while(x<2.0);
    return 0;
    }

    The Output of the above program is as follows which is wrong after 10 iteration
    x=1.050000
    x=1.100000
    x=1.150000
    x=1.200000
    x=1.250000
    x=1.300000
    x=1.350000
    x=1.400000
    x=1.450000
    x=1.500000
    x=1.549999
    x=1.599999
    x=1.649999
    x=1.699999
    x=1.749999
    x=1.799999
    ......
    .......
    x=2.049999


    I have used gcc compiler

    # gcc filename.c

    Please try to tell the appropriate reason because our research program is held due to this type of error.Also try tell whether programming in under Linux will
    be suitable or not in the context of accuracy in the results.

  6. #6
    drl
    drl is online now
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by gopalpathak
    Please try to tell the appropriate reason because our research program is held due to this type of error.Also try tell whether programming in under Linux will
    be suitable or not in the context of accuracy in the results.
    Your machine probably has only 6 digits of precision in a float.

    You can change the float to double. For information on your combination compiler and hardware arithmetic properties of your computer, see: Enquire.c You can download the source code for enquire and run it on your machine -- that's where I got the 6-digit-float information about my computer. I changed the float to double in your code and got more digits of precision.

    Some machines may see a significant speed decrease with double precision arithmetic.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...