Designing a Pattern using C++

Viewed 111

I was given a task by my professor to create a pattern in c++ output using a string input by user.

The program should run in the following way:

Enter a string: ***

       ***
        ***
         ***
          ***     
           ***
***************
           ***
          ***
         ***
        ***
       ***

Note : The input string from user can have any number of length or characters

Following are the restrictions for the program:

  • No string literals or white spaces in cout statement allowed.
  • Use of loops is also forbidden (This is the reason i am stuck ... i successfully created the above program but by using loops)
  • No advanced concept of c++ are allowed (at college we just started with basic concepts of how the language works. so please keep this in mind while giving the answer)

I have tried multiple ways to create the above program but due to given restrictions i don't think its possible anymore, so that's why i came here to ask help from the community.

Here is my code below using loops:

string userInput;
int m = 1, n = 9;
cout<<"\nEnter a three character string: ";
cin>>userInput; 
cout<<endl;
while (m <= 6)
      {
         if (m == 6)
           {                
        cout<<userInput<<userInput<<userInput<<userInput<<userInput<<endl; 
             m = 1;
             n = 13;
             while (m <= 5)
              {
                cout<<setw(n)<<userInput<<endl;
                m++;
                n--;
            }
            return 0; //this will finish the execution of the program
        }
        cout<<setw(n)<<userInput<<endl;
        n++;
        m++;
    }

The above program works if user enters 3 character string only

Help will be highly appreciated!

Sorry for my bad English, feel free to edit and correct it if you find any errors or mistakes

3 Answers

You can use something called recursive functions. This way you do not use loops, but recursion which you only have to call once.

#include <iostream>
#include <string>

void coutLine(std::string output) {
    std::cout << output << '\n';
}
void recursiveWriter(std::string recursiveInput, int number, int iterator) {
    //Correct for even number of lines below and above
    number = number - (number % 2);

    //You should split this logic in another function, to keep it neat
    if (iterator < (number / 2)) {
        recursiveInput = std::string(1, ' ') + recursiveInput;
        coutLine(recursiveInput);
    } else if (iterator > (number / 2)) {
        //dividable by 2 triggers the middle line
        //iterator should be -1 because one time it has ran by the 'else'
        if (iterator - 1 > number / 2) {
            recursiveInput = recursiveInput.erase(0, 1);
        }

        coutLine(recursiveInput);
    } else {
        //Create the middle line
        coutLine(std::string(recursiveInput.length() + 1, '*'));
    }

    if (iterator < number) {
        iterator++;
        recursiveWriter(recursiveInput, number, iterator);
    }
}

Of course I do not know all the specific requirements, but the following happends:

int main() {
    int lines = 11;
    int iterator = 0;
    recursiveWriter("***", lines, iterator);
}

//lines 10 and 11 generates:
 ***
  ***
   ***
    ***
     ***
*********
     ***
    ***
   ***
  ***
 ***

//lines 12 and 13 generates with input text *****:
 *****
  *****
   *****
    *****
     *****
      *****
************
      *****
     *****
    *****
   *****
  *****
 *****

So this way the number of lines is always equally on top and below. This could be improved however. Like mentioned, might not follow the requirements (You were not very specific about them).

Since the number of times that you need to repeat the input string or whitespace is fixed, you could manually print each repetition of the input manually. You can produce whitespace which matches the length of the input string with the setw function from iomanip (see http://www.cplusplus.com/reference/iomanip/setw/).

Here's an example:

#include <iostream>
#include <iomanip>

int main() {
    std::string input = "test";
    std::cout << std::setw(input.length() * 3) << input << "\n";
    std::cout << input << input << input << "\n"; 
    std::cout << std::setw(input.length() * 3) << input << "\n";
    std::cout << "\n";
    return 0;
}

Which produces the output:

        test
testtesttest
        test

To avoid repeating yourself you could try to use recursion. This may not be a concept you've covered yet, in which case it would be best to stick to the first suggestion. It might be worth looking up if you're interested, as it doesn't involve advanced features of c++.

You can always replace an iteration with a recursion. Take your existing code, and transform the loops into recursive functions. I would start by replacing the innermost loop, and work your way out.

In my view, this is a bad use of recursion. Recursion is useful for processing a naturally recursive structure (like a tree), and when you can solve a problem by splitting it in two (like sorting).

Related