Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

help with recursion

Name: Anonymous 2011-02-03 17:31

im making a program using recursion to try and make a rectangle.
it .
heres the instructions
1.    Rectangle Printer: Write a recursive function that draws a rectangle to the screen.  Ask the user to enter positive integers for the height and width of the rectangle (error trap for bad input).  Then ask the user for a character to use to print the rectangle (no error trap required).  Then print the rectangle to the screen.  No loops are allowed when printing the rectangle.


so if it said 5 * 5 with the filer a it would look like this
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa

heres my code point me in the right direction



#include <iostream>
using namespace std;




void Rectangle(int height, int width, char filler);


void main()
{
    int width;
    int height;
    char filler;

   

    cout << "Printing a rectangle:\n\n";
    cout << "Enter the height:";
    cin >> height;
    while (height <= 0)
    {
        cout << "The height should be greater than zero\n\n";
        cout << "Enter the height:";
        cin >> height;
    }

    cout << "Enter the width";
    cin >> width;
    while (width <= 0)
    {
        cout << "The width should be greater than zero\n\n";
        cout << "Enter the width:";
        cin >> width;
    }

    cout << "Enter a fill character:";
    cin >> filler;

   
        Rectangle(height, width, filler);
       


}




void Rectangle(int height, int width, char fill)
{
   
   
   
   




    if(width > 0)
    {
        cout<<fill;
        Rectangle(height, width - 1, fill);
    }
    if(height > 0)
    {
        cout << "\n";
        Rectangle(height - 1, width, fill);
    }
       
}

Name: Anonymous 2011-02-03 17:41

NO EXCEPTIONS

Name: Anonymous 2011-02-03 17:43

>>2

that doesnt really help me

Name: Samurai Jack !JNS//AKUUU 2011-02-03 17:49

void Rectangle(int height, int width, char fill){
    cout << fill;
    if(width >= 0)
        Rectangle(0, width-1, fill);
    else
        cout << "\n";
    if(height >= 0)
        Rectangle(height-1, width, fill);
}

Name: Samurai Jack !JNS//AKUUU 2011-02-03 17:52

Or something like that. I made changes without recompiling and it fucked it up. It works with > 0 but prints an extra column and row, it breaks with >= 0. I dunno.

Name: Anonymous 2011-02-03 17:53

i just tried it it and i got this  so idk

5
55
5
5555
5
55
5
555
5
55
5
555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
5555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
5555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
5555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
5555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
555555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
55555
5
55
5
555
5
55
5
5555
5
55
5
555
5
55
5
Press any key to continue . .

Name: Samurai Jack !JNS//AKUUU 2011-02-03 17:54

void Rectangle(int height, int width, char fill){
    cout << fill;
    if(width > 1)
        Rectangle(0, width-1, fill);
    else
        cout << "\n";
    if(height > 1)
        Rectangle(height-1, width, fill);
}

Durr hurr I'm a retard.
Also I don't know C or C++ or whatever this is, I'm writing it in Go and translating it.

Name: use [code] tags 2011-02-03 17:54


#include <iostream>
using namespace std;
void Rectangle(int height, int width, char filler);
void main()
{
    int width;
    int height;
    char filler;
    cout << "Printing a rectangle:\n\n";
    cout << "Enter the height:";
    cin >> height;
    while (height <= 0)
    {
        cout << "The height should be greater than zero\n\n";
        cout << "Enter the height:";
        cin >> height;
    }

    cout << "Enter the width";
    cin >> width;
    while (width <= 0)
    {
        cout << "The width should be greater than zero\n\n";
        cout << "Enter the width:";
        cin >> width;
    }

    cout << "Enter a fill character:";
    cin >> filler;
    Rectangle(height, width, filler);
}
void Rectangle(int height, int width, char fill)
{
    cout << fill;
    if(width >= 0)
        Rectangle(0, width-1, fill);
    else
        cout << "\n";
    if(height >= 0)
        Rectangle(height-1, width, fill);
}

Name: Doctor Racket !RACKET/.HY 2011-02-03 17:54

>>7
We don't need you, really.

Name: Samurai Jack !JNS//AKUUU 2011-02-03 17:57

>>9
Well deal with it, you're not my boss.

Name: Anonymous 2011-02-03 17:59

>>9 he helped me though so i dont mind him calling me a retard
>>10 thanks man im gonna try and figure out how you did it so i can try to learn this recursion shit

Name: Anonymous 2011-02-03 18:00

>>11
have you read your SICP today?

Name: Anonymous 2011-02-03 18:01

>>12

What is SICP?

Name: Anonymous 2011-02-03 18:02

>>13
Structure and Interpretation of Computer Programs

Name: Anonymous 2011-02-03 18:03

>>14
no i sure haven't does it help a lot?
because i might just need to look at it if it does

Name: VIPPER 2011-02-03 18:06

Something went very wrong with this thread.

Lets fix it.

JEWS

Name: Anonymous 2011-02-03 18:10

●█████▄▄▄▄▄▄▄▄
▄▅███████▅▄▃▂
███sage tank███████►
◥☼▲⊙▲⊙▲⊙▲⊙▲⊙▲☼◤

Name: Anonymous 2011-02-03 19:11

Use a source code style that makes the code readable and consistent. Unless you have a group code style or a style of your own, you could use a style similar to the Kernighan and Ritchie style used by a vast majority of C programmers. Taken to an extreme, however, it's possible to end up with something like this:
    int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);

Name: Anonymous 2011-02-03 19:11

It is common to see the main routine defined as main(). The ANSI way of writing this is int main(void) (if there are is no interest in the command line arguments) or as int main( int argc, char **argv ). Pre-ANSI compilers would omit the void declaration, or list the variable names and follow with their declarations.

Name: Anonymous 2011-02-03 19:16

# Treat functions with care

Functions are the most general structuring concept in C. They should be used to implement "top-down" problem solving - namely breaking up a problem into smaller and smaller subproblems until each piece is readily expressed in code. This aids modularity and documentation of programs. Moreover, programs composed of many small functions are easier to debug.

Cast all function arguments to the expected type if they are not of that type already, even when you are convinced that this is unnecessary since they may hurt you when you least expect it. In other words, the compiler will often promote and convert data types to conform to the declaration of the function parameters. But doing so manually in the code clearly explains the intent of the programmer, and may ensure correct results if the code is ever ported to another platform.

If the header files fail to declare the return types of the library functions, declare them yourself. Surround your declarations with #ifdef/#endif statements in case the code is ever ported to another platform.

Function prototypes should be used to make code more robust and to make it run faster.

Name: Anonymous 2011-02-03 19:16

Name: Anonymous 2011-02-03 19:38


    if (width > 0) {
        cout << fill;
        Rectangle(0, width - 1, fill);
    } else
        cout << "\n";
    if (height > 0)
        Rectangle(height - 1, width, fill);

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List