Thread: having trouble with for loop

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    having trouble with for loop

    Hi everyone, hope you're doing well. Im new here so please go easy on me
    I am having trouble printing out a table that should look like this
    Code:
    ****************************************************************************************|
     height | base  | Cross-sectional area  |   Moment of inertia   | Section modulus       |
      cm    |  cm   |     base x height     |  (base x height^3)/12 | (base x height)/6     |
    ----------------------------------------------------------------------------------------|
      2     |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      4     |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      6     |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      8     |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      10    |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      12    |   2   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      2     |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      4     |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      6     |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      8     |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      10    |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      12    |   4   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      2     |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      4     |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      6     |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      8     |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      10    |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      12    |   6   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      2     |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      4     |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      6     |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      8     |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      10    |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      12    |   8   |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      2     |   10  |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      4     |   10  |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      6     |   10  |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      8     |   10  |                       |                       |                       |
    -----------------------------------------------------------------------------------------
      10    |   10  |                       |                       |                       |
    -----------------------------------------------------------------------------------------
    but I end up with much longer table and this is my code for the table
    Code:
    
    
    1. #include<stdio.h>
    2. int main()
    3. {
    4. int z;
    5. int y;
    6. int i;
    7. printf("****************************************************************************************|");
    8. printf("\n height | base | Cross-sectional area | Moment of inertia | Section modulus\t|");
    9. printf("\n cm | cm | base x height | (base x height^3)/12 | (base x height)/6 |");
    10. printf("\n----------------------------------------------------------------------------------------|");
    11. for(z=1;z<=6;z++)
    12. {
    13. for(i=2;i<=10;i+=2)
    14. for(y=2;y<=12;y+=2)
    15. printf("\n %d\t| %d\t|\t\t\t|\t\t\t|\t\t\t|\n-----------------------------------------------------------------------------------------",y,i);
    16. }
    17. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    18. return 0;
    19. }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    6*10*12 = 144

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What is the purpose of the outer for loop?
    for(z=1;z<=6;z++)

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by stahta01 View Post
    What is the purpose of the outer for loop?
    for(z=1;z<=6;z++)

    Tim S.
    it's oky I kinda fixed it

    Code:
    #include<stdio.h>
    
    
    
    int main()
    {
    
    
        double y;
        double i;
    
    
    
    
        printf("****************************************************************************************|");
        printf("\n height | base  | Cross-sectional area  |   Moment of inertia   | Section modulus\t|");
        printf("\n  cm    |  cm   |     base x height     |  (base x height^3)/12 | (base x height)/6     |");
        printf("\n________________________________________________________________________________________|");
    
    
    
    
    
    
    
    
    
    
           for(i=2;i<=10;i+=2){
           for(y=2;y<=12;y+=2)
            printf("\n  %.0f\t|   %.0f\t|\t%.2f\t\t|\t%.2f\t\t|\t%.2f\t\t|\n________________________________________________________________________________________",y,i,i*y,(i*y*y*y)/12,(i*y)/6);
    
    
    }
    
    
       printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
     return 0;
    }
    Im starting out on c so I may have made some silly mistakes but thanks guys if my code have other mistakes please do and tell me so I can learn from it
    although the above code runs fine without errors or warnings
    thanks again

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This may or may not be important, but the horizontal lines in your table does not actually match the horizontal lines you described in your first post.

    Code:
    // first post table formatting:
    
    --------------
      xx  |  xx  |
    --------------
      xx  |  xx  |
    --------------
    
    // your table formatting
    
    ______________
      xx  |  xx  |
    ______________
      xx  |  xx  |
    ______________
    Also, you can break your "printf()" up into several "printf()" statements to make it a little bit easier to read.

    And - rather than manually adding whitespace before each floating-point number, you can use the width modifier in addition to the precision modifier. Not only does this eliminate the need for adding manual whitespace in those spots, you can also make the decimal points line up nicely. Here is a modified version of your "printf()" statement:

    Code:
    printf("\n%3.0f\t|%4.0f\t|%11.2f\t\t|%11.2f\t\t|%11.2f\t\t|\n________________________________________________________________________________________",y,i,i*y,(i*y*y*y)/12,(i*y)/6);
    If you wanted to get really fancy, you could write code that does not contain any hard-coded whitespace, but instead calculates how much whitespace is needed, and use the wild-card width specifier (though if you're still new to learning, this might be a bit over your head):

    Quote Originally Posted by printf
    *

    The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

    (source)

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    Quote Originally Posted by Matticus View Post
    This may or may not be important, but the horizontal lines in your table does not actually match the horizontal lines you described in your first post.

    Code:
    // first post table formatting:
    
    --------------
      xx  |  xx  |
    --------------
      xx  |  xx  |
    --------------
    
    // your table formatting
    
    ______________
      xx  |  xx  |
    ______________
      xx  |  xx  |
    ______________
    Also, you can break your "printf()" up into several "printf()" statements to make it a little bit easier to read.

    And - rather than manually adding whitespace before each floating-point number, you can use the width modifier in addition to the precision modifier. Not only does this eliminate the need for adding manual whitespace in those spots, you can also make the decimal points line up nicely. Here is a modified version of your "printf()" statement:

    Code:
    printf("\n%3.0f\t|%4.0f\t|%11.2f\t\t|%11.2f\t\t|%11.2f\t\t|\n________________________________________________________________________________________",y,i,i*y,(i*y*y*y)/12,(i*y)/6);
    If you wanted to get really fancy, you could write code that does not contain any hard-coded whitespace, but instead calculates how much whitespace is needed, and use the wild-card width specifier (though if you're still new to learning, this might be a bit over your head):
    "but the horizontal lines in your table does not actually match the horizontal lines you described in your first post."
    It's ok because I changed them


    and also thank you so much for clarifying how I should use width modifier, you're a life saver

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop trouble
    By musicman in forum C Programming
    Replies: 2
    Last Post: 04-27-2011, 03:23 AM
  2. Trouble with while loop
    By anything25 in forum C Programming
    Replies: 4
    Last Post: 05-23-2009, 02:59 PM
  3. even more loop trouble
    By mabufo in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 06:37 PM
  4. While Loop trouble.
    By Aterxerxes in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2005, 03:59 AM
  5. while loop trouble
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 09-19-2005, 02:59 AM