473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Recursion Square - HELP!

Hello everybody. I'm having a little trouble using recursion.

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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void input(char charSq[]);
  5. void draw(char charSq[], int size);
  6.  
  7. int main()
  8. {
  9.         int i;
  10.         char charSq[5];
  11.         char charSqCopy[5];
  12.  
  13.  
  14.         cout << "\nThis program accepts a sequence of characters (up to ";
  15.         cout << "5) consisting of one word and then produces a square using ";
  16.         cout << "those characters with the first character in \nthe middle ";
  17.         cout << "of the square and so on until the last character surrounds ";
  18.         cout << "the \noutside of the square.\n\n";
  19.  
  20.         input(charSq);
  21.  
  22.         int size = strlen(charSq);
  23.  
  24. //      for(i = 0; i < size + (size - 1); i++)
  25. //      {
  26. //              if(
  27. //              char charSq[size];
  28.  
  29. //      for(i = 0; i < size; i++)
  30. //              charSqCopy[i] = charSq[i];
  31.  
  32.         cout << "\ncharSq " << charSq << "." << endl;
  33.         cout << "charSqCopy " << charSqCopy << "." << endl;
  34.  
  35.         for(i = 0; i < size; i++)
  36.         {
  37.                 cout << "charSq " << charSq << endl;
  38.                 cout << "charSqCopy " << charSqCopy << endl;
  39.         }
  40.  
  41.         cout << charSq[0] << charSq[2] << endl;
  42.         cout << charSqCopy[size] << endl;
  43.  
  44.         cout << strlen(charSq) << endl;
  45.  
  46.         cout << "test" << size << endl;
  47.  
  48.         draw(charSq, size);
  49.  
  50.         for(i = 0; i < size; i++)
  51.         {
  52.                 charSqCopy[size-size] = charSq[size];
  53.                 charSqCopy[size-(size-1)] = charSq[size-1];
  54.                 charSqCopy[size-(size-2)] = charSq[size-1];
  55.                 //not finished
  56.         }
  57.         return (0);
  58. }
  59.  
  60. void input(char charSq[])
  61. {
  62.         cout << "Enter a one word sequence of up to 5 characters: ";
  63.         cin >> charSq;
  64.  
  65.         if(strlen(charSq) > 5)
  66.         {
  67.                 cout << "\nYour sequence exceeded the allowed number of characters. ";
  68.                 cout << "Try again!\n\n";
  69.                 input(charSq);   
  70.         }
  71.         else
  72.                 return;
  73. }
  74.  
  75. void draw(char charSq[], int size)
  76. {
  77.         int i, j;
  78.         int count = 0;
  79.  
  80. //      for(i = 0; i < size; i++)
  81. //              charSqCopy[i] = charSq[i];
  82.  
  83. //      cout << "size = " << size << endl;
  84. //      cout << "count = " << count << endl;
  85.  
  86.         for(i = 0; i < size + (size - 1); i++)
  87.                 cout << charSq[size-1] << " ";
  88.  
  89.         if(count == 0)
  90.                 cout << endl << charSq[size-1] << " ";
  91.  
  92.         for(j = 0; j < size + (size - 1); j++)
  93.         {       
  94.                 if(size == (size - j))
  95.                 {
  96. //                      count++;
  97.  
  98. //                      cout << endl << charSq[size-count] << " ";
  99.                         cout << charSq[size-j] << " ";
  100.  
  101.                         charSq[size-1] = charSq[size-2];
  102.                         size = strlen(charSq) - 1;
  103. //                      count++;
  104.  
  105.                         draw(charSq, size);   
  106.                 }
  107.                 else
  108.                         return;
  109.         }
  110.  
  111.         cout << endl;
  112.  
  113.         cout << endl;
  114.         cout << strlen(charSq) + (strlen(charSq) - 1) << endl;
  115.         cout << size << endl;   
  116.         cout << "Size:" << charSq[size-2] << endl;
  117. }
  118.  

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...
Mar 21 '08 #1
3 2007
oler1s
671 Expert 512MB
What's with the forum cross post?
Mar 21 '08 #2
What's with the forum cross post?
To get other opinions on a different forum. Can I not do that?
Mar 21 '08 #3
Ganon11
3,652 Expert 2GB
I would think about the base case, and how you will reduce the problem to the base case at any other case.

For this example, the base case seems to be the middle row of the square, where you finally print the first letter of the word, in the middle of the square.

The other base cases seem to be thus: You print the last N characters of the word once, then repeat the Nth-to-last letter until you then print the N+1th letter, then the N+2th letter, etc...

So each row seems like it will require a loop to print the proper characters. Then you have the basic algorithm:

Expand|Select|Wrap|Line Numbers
  1. if (this isn't the base case) {
  2.    print_row(theString, theLetterNum);
  3.    draw(theString, theLetterNum-1);
  4.    print_row(theString, theLetterNum);
  5. } else {
  6.    print_row(theString, theLetterNum);
  7. }
Mar 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
2
by: Alexander | last post by:
Hello All, I have array 5 x 5 with 'x' and spaises '-'. If I give coordinates of point on the array and i on the 'x' i must count any 'x' caracters that have shared sides (right, left,down,...
5
by: J. Wesolowski | last post by:
Hello, This is my first approach to comp.lang.c, so hello to everyone. I learn c programming from "Teach yourself c " by Aitken & Jones. So far everything is going well and I'm quite excited about...
11
by: Josiah Manson | last post by:
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
5
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.