Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can You help Me with this Stupid for Loop?

  1. Mar 3, 2009 #1
    Okay, so the object of this program is to write my name using an m x n array so that it prints each letter made up of that letter. That is, the first letter is B and thus is made up of a bunch of B's. Here is my code...I do not know what the problem is?

    Code (Text):
    #include <iostream>
    #include <cstring>
    using namespace std;

    int main ()

    {
        int i,j;
        char B[7][13]={
             {"BBBBBBBBBBBB"},
             {"BB    BBBBBB"},
             {"BB   BBB    "},
             {"BB BB       "},
             {"BB  BBBB    "},
             {"BB     BBBBB"},
             {"BBBBBBBBB   "}};
             
             for (i=0;i<7;i++)
             {
                 for (j=0;j<12;j++)
                 {
                     cout << B[i][j];
                 }
             }
             
             cout << endl;
             system ("PAUSE");
             return 0;
             
        }
    this is the output it is giving me
    Code (Text):
    BBBBBBBBBBBBBB    BBBBBBBB   BBB    BB BB       BB  BBBB    BB     BBBBBBBBBBBBB
    B
    Press any key to continue . . .

     
     
  2. jcsd
  3. Mar 3, 2009 #2
    Maybe you should first try to move the "cout<<endl;" inside the "for(i)" loop (I guess eventually you will need a "h" loop on the letters of your name). Also, is it correct to declare 13 elements and then put in only 12 ? Will this not mess up ?
     
  4. Mar 3, 2009 #3
    The 13th column is the "null character" so yes.

    And yes..the moving of endl; worked....I think I see why now! Thanks!

    What will the "h" loop be for? The next letter? Wouldn't I need to use two indices for each letter since I want them to print horizontally?
     
  5. Mar 3, 2009 #4
    Yes, you need to put a newline either just before or just after the second for loop.
     
  6. Mar 3, 2009 #5
    I put it after the 2nd loop; I am not sure that I see what putting it before the j loop would do?
     
  7. Mar 3, 2009 #6
    How can I get the E to print NEXT to the B?? Supposedly it can be done with for loops...


    Code (Text):
    #include <iostream>
    #include <cstring>
    using namespace std;

    int main ()

    {
        int i,j;
        char B[7][13]={
             {"BBBBBBBBBBBB"},
             {"BB    BBBBBB"},
             {"BB   BBB    "},
             {"BB BB       "},
             {"BB  BBBB    "},
             {"BB     BBBBB"},
             {"BBBBBBBBB   "}};

         char E[7][13]={
             {"EEEEEEEEEEEE"},
             {"EE          "},
             {"EE          "},
             {"EEEEEEEEE   "},
             {"EEEEEEEEE   "},
             {"EE          "},
             {"EEEEEEEEEEEE"}};
             
             
             
             for (i=0;i<7;i++)
             {
                 for (j=0;j<12;j++)
                 {
                     cout << B[i][j];
                 }
                  cout << endl;
             }
             
             
             
             
             
             system ("PAUSE");
             return 0;
             
     
  8. Mar 3, 2009 #7
    Ah sure, I have not used C-strings a lot. Thanks.
    I was thinking to make other tables for other letters, use the same indices for rows and columns, and a new index for the tables. That would in the end amount to one 3-indices table. It's only relevant if you want to re-use the letters. Eventually, you have one big alphabet table with 3 indices, and another table containing the index for each letter in your sentence. Do I make sense ? :uhh:

    Code (Text):

    char alphabet[26][7][13] = {
    {
    {"     A    "}
    { ...}
    ...
    }{
    {"     B    "}
    ...
    }{
    ...
    }{
    {"   Z   "}
    ...
    }
    };
    and for the printing of the sentence "BOB", one table
    Code (Text):

    int bob[3] = {1,12,1}
     
    It would also help for the handling of blank, null, coma... Well, that's ASCII :approve:
     
    Last edited: Mar 3, 2009
  9. Mar 3, 2009 #8
    I kind of follow you. But I am still lost on the h index. Using my last post, I have two arrays. One named B and one named E. How would I call them such that I can cout

    BE

    to the screen? as opposed to

    B
    E

    ?
     
  10. Mar 3, 2009 #9
    You need to do first the loop on the first row of B (the first letter), then the loop on the second row of E (the second letter),..., then the second row of the first letter, the second row of the second letter,...,...,..., then the last row of the first letter, then the last row of the second letter,...then the last row of the last letter. So either you have an explicit list of all letters with each letter has its own for loop for all chars in the row, or you need a big table. For the sentence "BOB"
    Code (Text):

    char alphabet[letter][row][column] = {{{AA..}{AA..}...}{{BB..}{BB..}...}...};
    char fixed_space[5] = "     ";
    int sentence[3] = {1,11,1};
    for(all rows){
      for(all letters){
        for(all chars){
          cout << alphabet[sentence[letter]][row][char];
        }
        cout << fixed_space;
      }
      cout << endl;
    }
     
     
  11. Mar 3, 2009 #10
    Somehow this works... why don't I need two indices now? Why does this even work?
    This is stupid.

    Code (Text):
    #include <iostream>
    using namespace std;

    int main ()

    {
        int i,j;
        char B[7][13]={
             {"BBBBBBBBBBBB"},
             {"BB    BBBBBB"},
             {"BB   BBB    "},
             {"BB BB       "},
             {"BB  BBBB    "},
             {"BB     BBBBB"},
             {"BBBBBBBBB   "}};
       
        char E[7][13]={
             {"EEEEEEEEEEEE"},
             {"EE          "},
             {"EE          "},
             {"EEEEEEEEE   "},
             {"EEEEEEEEE   "},
             {"EE          "},
             {"EEEEEEEEEEEE"}};
             
                 
             for (i=0;i<7;i++)
             {
       
                cout << B[i] << '\t'<<E[i];
                 
                cout << endl;
             }
             
             
             
             
             
             
             
             
             
             system ("PAUSE");
             return 0;
             
        }
     
     
  12. Mar 3, 2009 #11
    That's great ! :biggrin:
    That's why I love coding.
    I think B is interpreted as a string of chars, so cout << B outputs the entire ith row.
     
  13. Mar 3, 2009 #12
    Actually.. it occurs to me that I do not even need .... All I am doing is outputting each entire array with a tab in between.

    But my prof wants it done with for loops. This is so stupid. This teaches me nothing useful.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Can You help Me with this Stupid for Loop?
  1. Can you help me ? (Replies: 2)

Loading...