c++ for loop

  • ultimate11
  • Student
  • Student
  • ultimate11
  • Posts: 86

Post 3+ Months Ago

how to make this an output like this..help
*****A
****BB
***AAA
**BBBB
*AAAAA

Code: [ Select ]
#include <iostream>
using namespace std;

int main()
{
  
  int a,b,w;
  for(int q=0;q<=10;q++)
  {
      for(w=1;w<=10-q;w++)
      {
          cout<<"*";
          }

        for(a=1;a<=q-w;a++)
          {
              cout<<"A";  
              }
cout<<endl;
              for(b=1;b<=q-w+1;b++)
          {
              cout<<"B";
              }

        
          cout<<endl;
}
 
  system ("pause");
  return 0;
  }
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   
  6.   int a,b,w;
  7.   for(int q=0;q<=10;q++)
  8.   {
  9.       for(w=1;w<=10-q;w++)
  10.       {
  11.           cout<<"*";
  12.           }
  13.         for(a=1;a<=q-w;a++)
  14.           {
  15.               cout<<"A";  
  16.               }
  17. cout<<endl;
  18.               for(b=1;b<=q-w+1;b++)
  19.           {
  20.               cout<<"B";
  21.               }
  22.         
  23.           cout<<endl;
  24. }
  25.  
  26.   system ("pause");
  27.   return 0;
  28.   }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Is this homework?
  • ultimate11
  • Student
  • Student
  • ultimate11
  • Posts: 86

Post 3+ Months Ago

yes..before when i studying..but now its just a refresh. :lol:
  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

I guess the point with this exercise is to only use for loops and for loops only? No things such as IF, ELSE, while etc?

I failed to do so during my initial attempt but I will probably try again tomorrow if I have the time since this was somewhat interesting. Here is a silly first shot where I "cheated" anyhow:

Code: [ Select ]
#include <iostream>
using namespace std;

int main()
{
  int stars, letters, temp;
  bool print_A = true;
  for(letters = 1; letters <= 5; letters++)
  {
    for(stars = 6 - letters; stars > 0; stars--)
    {
      cout<<"*";
    }
    for(temp = letters; temp > 0; temp--)
    {
      if(print_A)
      {
        cout<<"A";
      }
      else
      {
        cout<<"B";
      }
    }
    cout<<endl;
    print_A = !print_A;
  }
  while(1);
  return 0;
}
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.   int stars, letters, temp;
  6.   bool print_A = true;
  7.   for(letters = 1; letters <= 5; letters++)
  8.   {
  9.     for(stars = 6 - letters; stars > 0; stars--)
  10.     {
  11.       cout<<"*";
  12.     }
  13.     for(temp = letters; temp > 0; temp--)
  14.     {
  15.       if(print_A)
  16.       {
  17.         cout<<"A";
  18.       }
  19.       else
  20.       {
  21.         cout<<"B";
  22.       }
  23.     }
  24.     cout<<endl;
  25.     print_A = !print_A;
  26.   }
  27.   while(1);
  28.   return 0;
  29. }


I'm not sure that code make that much sense - I can hardly read it, feeling a bit tired and for loops inside for loops hasn't been a strong side of mine.

=)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 70 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.