Hey I am very new to programming. I am taking my first C++ class. I am a little embarrassed to ask a simple question but I am stuck.
I want to code a pattern that can vary by size etc. It looks like
*****
5****
55***
555**
5555*

or
********
9*******
99******
999*****
You get the idea. size can vary say from 5 to 100 (that doesnt matter) what matters is the *&&^%$## logic to get there.

So I definitely want to use a loop, or more accurately a nest of loops. Where the number falls changes depending on the size of the pattern but in a predictable fashion so here are some thoughts:

row 1 is size of the pattern minus (size - 1) so when row equals one I want "*" size times. For now just assume size is obtained from the user.

for (row = 1; row <= size; row++)
   for (col = 1; col <= size; col++)
   {
      if (row == 1 && col <= size)  
         cout << "*";
      else if (row == 2 && col == 1)
              cout << size;
            else
              cout << "*";
   )

That doesnt work.
Next thought:

for (row = 1; row <= size; row++)
   for (col = 1; col <= size; col++)
   {
      if (row == (size - (size - row) && row != 1)
        {
         do
         cout << "*";
         while (col <= size)
         }
      else if (row == (size - (size - row)) && col <= (size - row))
         cout << size;
         else << "*";
      cout << endl;
    }

But this falls short of the requirement as well, I think.
Well if anyone has a push in the right direction I would appreciate it.
Thank you
Mai

Recommended Answers

All 4 Replies

Please use code Tagging next time.
And please complete the Introduction, most likely this post will be moved

here is the code

System.Console.WriteLine("Enter Number");
            int size = int.Parse(System.Console.ReadLine());

            for (int row = 0; row < size; row++)
            {
                for (int col = 0; col < size; col++)
                {
                    if (col < row)
                        System.Console.Write("" + size);
                    else
                        System.Console.Write("*");
                }
                Console.WriteLine("");
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);

This is in C# cause that's the only complier I have handy atm.

But you should get the idea

Here is what I got :

#include <iostream>

using namespace std;

template<typename T>
void printN(T arg, int n){
 while(n--)cout << arg;
}

int main(){
 const int ROW = 9;

 for(int r = 0; r != ROW; ++r){
      printN(ROW, r);		
      printN('*',ROW - r);
      cout << endl;
 }

  return 0;
}

Please use code Tagging next time.
And please complete the Introduction, most likely this post will be moved

here is the code

System.Console.WriteLine("Enter Number");
            int size = int.Parse(System.Console.ReadLine());

            for (int row = 0; row < size; row++)
            {
                for (int col = 0; col < size; col++)
                {
                    if (col < row)
                        System.Console.Write("" + size);
                    else
                        System.Console.Write("*");
                }
                Console.WriteLine("");
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);

This is in C# cause that's the only complier I have handy atm.

But you should get the idea

You are AWESOME. That is essentially what I finally discovered. The "Hint" I needed was assign each of the positions in the pattern a matrix location assignment and a pattern will emerge. Thank you for your assistance!
Mai

no problem anytime

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.