473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursion Square - HELP!

74 New Member
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 2033
oler1s
671 Recognized Expert Contributor
What's with the forum cross post?
Mar 21 '08 #2
JWest46088
74 New Member
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 Recognized Expert Specialist
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
2580
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 procedure is not following the recursion in serial order, but in parallel. In other words, after one instance of the procedure calls itself, it continues executing lines below the recursion before the recursion is done. Is that possible? I...
2
1295
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, up,... 8 directions) I make that, but program not work and i can't find mistake: ///////////////////////////////////////////////////
5
4604
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 being able to write my own simple programs, but I'm stuck at one point. I can't figure out recursion of a function in a simple way. Do you have any system that when traing to follow recursion everthing is clear? When recursion is at one level...
11
3927
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 attempting to make a list of polynomial functions such that poly(3) = 1, poly(3) = 3, poly(3) = 9, etc. Could someone point me in the right direction? Thanks. def make_polys(n): """Make a list of polynomial functions up to order n. """
18
3723
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 python is generally more time-efficient than recursion. Is that true? Here is my algorithm as it stands. Any suggestions appreciated!
13
2914
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
8307
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? Surprisingly, deep recursion actually isn't that slow for what I'm doing. Programming this task recursively is so much more straightforward to me, but currently I'm forced to use an ugly hack to avoid going over the 1000 level limit. Of course, it could...
5
3139
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
35
4738
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
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.