I need to accept input from the user and display the input in a square. It has to be done recursively. I would be able to do it without using recursion (although it may not be the best way and sloppy), but I cannot for the life of me figure out how to do it using recursion.
Here is how it's supposed to work:
Enter characters: lucky //ask for user input
Display user input in square:
y y y y y y y y y
y k k k k k k k y
y k c c c c c k y
y k c u u u c k y
y k c u l u c k y
y k c u u u c k y
y k c c c c c k y
y k k k k k k k y
y y y y y y y y y
Here's my code (I have a lot of cout statements for testing. Please disregard those.):
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std;
- void input(char charSq[]);
- void draw(char charSq[], int size);
- int main()
- {
- int i;
- char charSq[5];
- char charSqCopy[5];
- cout << "\nThis program accepts a sequence of characters (up to ";
- cout << "5) consisting of one word and then produces a square using ";
- cout << "those characters with the first character in \nthe middle ";
- cout << "of the square and so on until the last character surrounds ";
- cout << "the \noutside of the square.\n\n";
- input(charSq);
- int size = strlen(charSq);
- // for(i = 0; i < size + (size - 1); i++)
- // {
- // if(
- // char charSq[size];
- // for(i = 0; i < size; i++)
- // charSqCopy[i] = charSq[i];
- cout << "\ncharSq " << charSq << "." << endl;
- cout << "charSqCopy " << charSqCopy << "." << endl;
- for(i = 0; i < size; i++)
- {
- cout << "charSq " << charSq << endl;
- cout << "charSqCopy " << charSqCopy << endl;
- }
- cout << charSq[0] << charSq[2] << endl;
- cout << charSqCopy[size] << endl;
- cout << strlen(charSq) << endl;
- cout << "test" << size << endl;
- draw(charSq, size);
- for(i = 0; i < size; i++)
- {
- charSqCopy[size-size] = charSq[size];
- charSqCopy[size-(size-1)] = charSq[size-1];
- charSqCopy[size-(size-2)] = charSq[size-1];
- //not finished
- }
- return (0);
- }
- void input(char charSq[])
- {
- cout << "Enter a one word sequence of up to 5 characters: ";
- cin >> charSq;
- if(strlen(charSq) > 5)
- {
- cout << "\nYour sequence exceeded the allowed number of characters. ";
- cout << "Try again!\n\n";
- input(charSq);
- }
- else
- return;
- }
- void draw(char charSq[], int size)
- {
- int i, j;
- int count = 0;
- // for(i = 0; i < size; i++)
- // charSqCopy[i] = charSq[i];
- // cout << "size = " << size << endl;
- // cout << "count = " << count << endl;
- for(i = 0; i < size + (size - 1); i++)
- cout << charSq[size-1] << " ";
- if(count == 0)
- cout << endl << charSq[size-1] << " ";
- for(j = 0; j < size + (size - 1); j++)
- {
- if(size == (size - j))
- {
- // count++;
- // cout << endl << charSq[size-count] << " ";
- cout << charSq[size-j] << " ";
- charSq[size-1] = charSq[size-2];
- size = strlen(charSq) - 1;
- // count++;
- draw(charSq, size);
- }
- else
- return;
- }
- cout << endl;
- cout << endl;
- cout << strlen(charSq) + (strlen(charSq) - 1) << endl;
- cout << size << endl;
- cout << "Size:" << charSq[size-2] << endl;
- }
Executed code:
This program accepts a sequence of characters (up to 5) consisting of one word and then produces a square using those characters with the first character in
the middle of the square and so on until the last character surrounds the
outside of the square.
Enter a one word sequence of up to 5 characters: hello
charSq hello.
charSqCopy Đh8ężello.
charSq hello
charSqCopy Đh8ężello
charSq hello
charSqCopy Đh8ężello
charSq hello
charSqCopy Đh8ężello
charSq hello
charSqCopy Đh8ężello
charSq hello
charSqCopy Đh8ężello
hl
¦
5
test5
o o o o o o o o o
o l l l l l l l
l l l l l l l l l
l l l l l l l l l
l l l l l l l l l
l l l l l l l l l
l l l l l l l l l
l l l l l l l l l
l l l l l l l l l
The l just keeps looping infinitely...