-4

From word puzzle collect data in array 2 dimensions and display the results in the following figure.

example :

T  TH  THI  THIS
H  HI  HIS
I  IS
S

W  WA  WAT  WATS
A  AT  ATS
T  TS
S 

I want to solve word puzzle and I can't to solve this because I don't know for loop work and I don't know how to set range of this loop so I want to know how can I set for loop for run word puzzle out like example and this question a[5] (array 2 dimensions type char) and this my code

int main() 
   { int i,j,k;

       char a[5] = "THIS";
       for(k=0;k<4;k++)
       {
         for(i=0;i<4-k;i++)
         {
          for(j=0;j<=i;j++)
              cout << a[j] << " ";
         cout << " ";
         }
       cout << endl;
       }
    }

but this code run is
T TH THI THIS
T TH THI
T TH
T

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
R.TURK
  • 1
  • 3
  • What counts as a result? Are all the words four letters? – doctorlove Aug 19 '16 at 17:24
  • Could you please provide more information on the sample inputs, and the desired outputs? It seems like it is the only way we can understand what you are exactly trying to achieve. – Khalil Khalaf Aug 19 '16 at 17:34
  • 1
    What is the problem, exactly? Also, your example seems incomplete since I don't see the definition of `a`. Please provide [mcve]. – Algirdas Preidžius Aug 19 '16 at 17:35
  • In addition to my previous comment: I don't understand what, you think, is wrong, with your code. If I include all of the relevant headers, and define `a` (in addition to reading it via `cin`), I get [this](https://ideone.com/RqDrpn). Now that I see that additional space gets added after each letter, I removed the output of the space symbol in the innermost `for` to get [this](https://ideone.com/yftaO9), which closely matches your examples. So, once again, what is the problem? – Algirdas Preidžius Aug 19 '16 at 17:44
  • Your `j` variable is always starting at zero. In the examples, it should start at different offsets. Use a debugger and see. – Thomas Matthews Aug 19 '16 at 20:45

2 Answers2

0

Results obtained from above program, just to prove it works for different sizes, not just strings of length 4.

T TH THI THIS 
T TH THI 
T TH 
T 

W WA WAT WATS 
W WA WAT 
W WA 
W 

H HE HEL HELL HELLO 
H HE HEL HELL 
H HE HEL 
H HE 
H 
Eamonn Kenny
  • 1,926
  • 18
  • 20
0

Your restriction on your inner nested for loop are off here is the correct code.

int main() 
   { int i,j,k;

       char a[5] = "THIS";
       for(k=0;k<4;k++)
       {
         //here you replace the 0 with k and take out the -k
         for(i=k;i<4;i++)
         {
          //right here replace 0 with k 
          for(j=k;j<=i;j++)
              cout << a[j] << " ";
         cout << " ";
         }
       cout << endl;
       }
    }
NNaylor
  • 11
  • 4