3

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

12
  • what did you try? show you code using loop... Commented Oct 6, 2019 at 10:49
  • a program that uses while loop with increment and decrement operators but loops are not allowed Commented Oct 6, 2019 at 10:50
  • You should have a look at the constructors for std::string, there is one that allows to get n times a given character Commented Oct 6, 2019 at 10:53
  • 1
    Please post your code. Posting an image is not enough because it doesn't let people test your code easily. Commented Oct 6, 2019 at 11:00
  • 2
    I have down voted for posting a link to an image of your code. Please edit the question to include the actual code. Commented Oct 6, 2019 at 11:01

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

thanks your answer is really comprehensive ... it also gave me an idea to how to use recursive operators.
0

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++.

5 Comments

This is the answer i was looking for ... thanks mate you gave a really simple and basic answer which i can implement easily. I understood the logic behind it & it wont be difficult now to create the above pattern.
@Gingitsune however this would 'suffice' and is simple, this would not help you further in creating 'nice' code through solve by just 'linear programming'. Just a mention to keep in mind =)
@Sliver2009 I know but i already mentioned in the question that i am being restricted by my instructor to use the only methods he taught. We are new to programming and we will learn about the advanced methods of c++ in future.
@Sliver2009 I completely agree. There are other options that would be more interesting, but they might be over the top when producing a small pattern. It could be a good exercise to try doing recursion as well.
@Gingitsune No problem
0

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).

1 Comment

Thanks for the answer but i am not familiar with recursions. The logic was very simple as pointed out by @Chris Pearce in his answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.