nested loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evantri
    New Member
    • Sep 2006
    • 25

    #1

    nested loop

    i tried to do this nested loop function

    double minx = -1.0
    double maxx = 1.0
    double miny = -1.0
    double maxy = 1.0
    double x;
    double y;

    for (x=minx;x<=maxx ;x+0.5)
    {
    for (y=miny;y<=maxy ;y+0.5)
    {
    z = x*y;
    }
    }
    printf(value of z is %.10lf\n",z);


    but after compiling the program it does nothing. could someone help me why the program does not print out the value of z here?
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    #2
    Your for loop is incorrect. It goes to infinte.
    Put somthing like this

    Code:
    for (x=minx;x<=maxx;x = x+0.5)
    {
       for (y=miny;y<=maxy;y = y+0.5)
        {
           z = x*y;
        }
    }

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Yes or

      Code:
      for (x=minx;x<=maxx;x+=0.5)
      {
          for (y=miny;y<=maxy;y+=0.5)
          {
              z = x*y;
          }
      }
      Additionally you have

      left out ; at the end of the lines declaring minx, maxx, miny and maxy
      not declared z
      missed a " out og your printf statement


      Note you will only get 1 line of output since the printf statement is not in any for loop;

      Comment

      • bitong
        New Member
        • Sep 2006
        • 40

        #4
        Question:

        Isn't it that if you declare double you use %e instead of %f? just asking =)

        Originally posted by evantri
        i tried to do this nested loop function

        double minx = -1.0
        double maxx = 1.0
        double miny = -1.0
        double maxy = 1.0
        double x;
        double y;

        for (x=minx;x<=maxx ;x+0.5)
        {
        for (y=miny;y<=maxy ;y+0.5)
        {
        z = x*y;
        }
        }
        printf(value of z is %.10lf\n",z);


        but after compiling the program it does nothing. could someone help me why the program does not print out the value of z here?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by bitong
          Question:

          Isn't it that if you declare double you use %e instead of %f? just asking =)
          You are correct, %lf actually refers to a type of long double (80 bits on some machines).

          Interestingly though, in scanf I have delt with numerous problems on here recently where the cause seemed to be if you give scanf %f then it expects pointer to float. I thought than scanf and printf basically took the same format strings but floating point values seem to be a situation where they differ.

          I think that this may be because floats are automatically promoted to doubles in a function call where as points remain exactly what they are.

          Comment

          Working...