Thread: Help nested for loops

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Lightbulb Help nested for loops

    so i have run into another wall, there is this problem where it tells me to print dollard signs

    Using 2 For loops printout

    $$$$$
    $$$$
    $$$
    $$

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
       {
         int i,j;
         
         for(i=1;i<=5;i++){
            for(j=1;j<=5;j++){
               printf("i= %i and j= %i\n",i,j);
            }
         }        
    getchar();
    }
    ok so i can get the numbers to print out but how can i work it so it prints dollard signs instead of the numbers, im a bit lost

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dals2002 View Post
    so i have run into another wall, there is this problem where it tells me to print dollard signs

    Using 2 For loops printout

    $$$$$
    $$$$
    $$$
    $$

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
       {
         int i,j;
         
         for(i=1;i<=5;i++){
            for(j=1;j<=5;j++){
               printf("i= %i and j= %i\n",i,j);
            }
         }        
    getchar();
    }
    ok so i can get the numbers to print out but how can i work it so it prints dollard signs instead of the numbers, im a bit lost
    I have decided to take your question literally:
    Code:
    printf("$");

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Make a small mental note of the lengths of the sequences of the dollar signs to print.
    Write a for loop that it's control variable will take those values.
    Now that you have a variable that iterates over the lengths of the sequences of the
    dollar signs you want to print, write a second for loop that is controlled (stops) by the
    value of that variable, and simply print a '$'.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by tabstop View Post
    I have decided to take your question literally:
    Code:
    printf("$");
    what i mean to print out is

    $$$$$
    $$$$
    $$$
    $$

    but if i do it that way it all prints it out in a straight line down which is not really what im looking for

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by xuftugulus View Post
    Make a small mental note of the lengths of the sequences of the dollar signs to print.
    Write a for loop that it's control variable will take those values.
    Now that you have a variable that iterates over the lengths of the sequences of the
    dollar signs you want to print, write a second for loop that is controlled (stops) by the
    value of that variable, and simply print a '$'
    .
    the second loop is exactly what i don't get how can i change the values to printout $ signs, i get the part where i set the system to do it

    Code:
    for(i=1;i<=1;i++){
            for(j=1;j<=5;j++){
    what am i suppose to printout, so that it prints dollards instead of numbers?
    or am i doing it wrong?
    Last edited by dals2002; 03-14-2008 at 12:23 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    putchar('$'); for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Hm, tabstop and vart have already answered that question.
    My question is what is the sequence of the lengths of the sequences of dollar signs
    that you want to print, and how would you write a for loop that enumerates that sequence?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by vart View Post
    putchar('$'); for example
    i kind of got it now just need to break it up which i been trying to put j-- to see if it does remove the one but i guess it doesn't, also i have another question is there anyway to write putchar('$'), wanna make sure i know more than one just in case it presents itself in any future test thanks


    Code:
    int main()
       {
         int i,j;
         
         for(i=1;i<=4;i++){
            for(j=1;j<=5;j++){
               putchar('$');
            }
         printf("\n");
         }        
    getchar();
    }
    now it prints out, how can i make it

    $$$$$ ----> $$$$$
    $$$$$ ----> $$$$
    $$$$$ ----> $$$
    $$$$$ ----> $$

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    replace the j<=5 with the appropriate condition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by vart View Post
    replace the j<=5 with the appropriate condition
    wow i can't believe i missed that, i really missed it by a long shot

    here is the finished program

    Code:
    int main()
       {
         int i,j;
         
         for(i=1;i<=4;i++){
            for(j=1;j<=6-i;j++){
               putchar('$');
            }
            printf("\n");
         }        
    getchar();
    }
    can i ask, is there anyways to write putchar('$'), in other formats?

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    is there anyways to write putchar('$'), in other formats?
    What do you mean? I do not understand your question
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by vart View Post
    What do you mean? I do not understand your question
    like is there any other way to like re write it so that instead of putchar('$') i can use like printf or something else?

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    well all that is left for me to say its thanks to all that contributed and helped

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    yes you can.
    There is a lot of IO functions http://msdn2.microsoft.com/en-us/lib...xx(VS.71).aspx

    You can read their descrition and decide what of them could be used to output one char to file/stdout
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by vart View Post
    yes you can.
    There is a lot of IO functions http://msdn2.microsoft.com/en-us/lib...xx(VS.71).aspx

    You can read their descrition and decide what of them could be used to output one char to file/stdout
    thanks this is very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM