Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,501 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,388 people online right now. Registration is fast and FREE... Join Now!




Function

 
Reply to this topicStart new topic

Function, Write a function with 4 parameters to create a rectangle

street3
20 Mar, 2007 - 12:38 PM
Post #1

New D.I.C Head
*

Joined: 1 Mar, 2007
Posts: 10


My Contributions
I am trying to write a function with 4 parameter: length and width of a rectangle, character for the border(default is a *) and a character to fill the inside(default should be a space).

It should look like this:

#####
#/////#
#/////#
#####


*****
* *
*****
this should form a rectangle




This is as far as I can get.

CODE


#include<iostream>
using std::cout;
using std::cin;
#include<cstdlib>

double lengthRect (int length, int width, char='*', char='/');




I'm lost from here. Not sure if the return-type should be double. Any sugesstion?

This post has been edited by street3: 20 Mar, 2007 - 12:40 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Function
20 Mar, 2007 - 07:46 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
why would it return a double? I would think if anything it would return a pointer to a string containing the box.

The declaration should look more like:
void rect(int iHeight, int iWidth, char cBorder='*', char cFill = 32);
(32 is the ascii char code for a space).

the inner workings of the program are two loops, one for the height, one for the width, and maybe a few if-statements to determine if you are on the first line, the first char of a middle line, the last character of a middle line, or the very last line, else just cout << cFill;.
User is offlineProfile CardPM
+Quote Post

GannMan
RE: Function
20 Mar, 2007 - 11:24 PM
Post #3

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 5


My Contributions
Hey I randomly saw this post and registered to help out. It says here no cheating allowed but um im not really sure what exactly that would be. anyways i went to my last semester program folder and took out the draw function out, and made changes to fit what you wanted. maybe it can help you a bit.

CODE

void rect(int Length, int Width)
{
    char Border='*';
    char Fill=' ';
    for (int L=0;L<Length;L++)
    {
        cout << Border;
        if(L==0 || L==Length-1)
        {
            for (int W=0;W<Width-2;W++)
                cout << Border;
        }
        if(L>0 && L<Length-1)
        {
            for (int W=0;W<Width-2;W++)
                cout << Fill;
        }
        cout << Border;
        cout << endl;
    }
}


Thats the function you asked for. The code below is the rest of the program, it asks for the user to input a length and width. If you need you can also make it so it asks for the border character and the filling character but you'll have to do that.

CODE

#include<iostream>
using namespace std;
void rect(int, int, char, char);

int main()
{
    char border='*';
    char fill=' ';
    int length;
    int width;
    cout << "Length: ";
    cin >> length;
    cout << "Width: ";
    cin >> width;
    rect(length, width, border, fill);
    return 0;
}


Just take that and put it before the function and compile, should all work.
User is offlineProfile CardPM
+Quote Post

rissvann
RE: Function
21 Mar, 2007 - 12:57 AM
Post #4

New D.I.C Head
*

Joined: 19 Sep, 2006
Posts: 8


My Contributions
dont forget to use .h in headers file if you are writing code in C language

QUOTE(GannMan @ 21 Mar, 2007 - 12:24 AM) *

Hey I randomly saw this post and registered to help out. It says here no cheating allowed but um im not really sure what exactly that would be. anyways i went to my last semester program folder and took out the draw function out, and made changes to fit what you wanted. maybe it can help you a bit.

CODE

void rect(int Length, int Width)
{
    char Border='*';
    char Fill=' ';
    for (int L=0;L<Length;L++)
    {
        cout << Border;
        if(L==0 || L==Length-1)
        {
            for (int W=0;W<Width-2;W++)
                cout << Border;
        }
        if(L>0 && L<Length-1)
        {
            for (int W=0;W<Width-2;W++)
                cout << Fill;
        }
        cout << Border;
        cout << endl;
    }
}


Thats the function you asked for. The code below is the rest of the program, it asks for the user to input a length and width. If you need you can also make it so it asks for the border character and the filling character but you'll have to do that.

CODE

#include<iostream>
using namespace std;
void rect(int, int, char, char);

int main()
{
    char border='*';
    char fill=' ';
    int length;
    int width;
    cout << "Length: ";
    cin >> length;
    cout << "Width: ";
    cin >> width;
    rect(length, width, border, fill);
    return 0;
}


Just take that and put it before the function and compile, should all work.


This post has been edited by rissvann: 21 Mar, 2007 - 01:04 AM
User is offlineProfile CardPM
+Quote Post

GannMan
RE: Function
21 Mar, 2007 - 03:04 AM
Post #5

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 5


My Contributions
you mean for #include <iostream>?
User is offlineProfile CardPM
+Quote Post

street3
RE: Function
22 Mar, 2007 - 06:20 PM
Post #6

New D.I.C Head
*

Joined: 1 Mar, 2007
Posts: 10


My Contributions
QUOTE(GannMan @ 21 Mar, 2007 - 04:04 AM) *

you mean for #include <iostream>?



Thank you so much. This helped me.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month