Hi I have this code with a function that outputs a square filled with the given char but for the

square(4);

I want it to just output a square like this

****
****
****
****

How would I do this without changing the function definition or using another function definition?

#include<iostream>
using namespace std;

void square(unsigned a, char b);

int main(){
	square(4, 'W');
	square(4);
	system("pause");
}

void square(unsigned a, char b){
		for(unsigned i = 0; i< a; ++i){
				for(unsigned j = 0; j< a; ++j)
						cout << b;
						cout << endl; }
	}

Recommended Answers

All 8 Replies

Here is what I made up in a few min, hope it helps, probably easier way to do it.

// prototype with default char b
void square(unsigned a, char b = '*');

// function definition
void square(unsigned a, char b)
{
	int oneLessThanA = a-1;
	for (int i = 0; i < a; i++)
	{
		if (i == 0 || i == oneLessThanA)
		{
			for (int j = 0; j < a; j++)
				cout << b;
			cout << endl;
		}
		else
		{
			for (int k = 0; k < a; k++)
				if (k == 0 || k == oneLessThanA)
					cout << b;
				else
					cout << " ";
			cout << endl;
		}
	}
}

What would you suggest? I've given you hints on the other problems, so I'll leave the burden on you for this one.

Here is what I made up in a few min, hope it helps, probably easier way to do it.

There is, but I want the OP to try and come up with it first. I'm not sure I understand what you did there, actually. The exercise is trying to get output like this:

square(4,w);
wwww
wwww
wwww
wwww

square(4);
****
****
****
****

at least that's my understanding of it.

I'm not sure but I think I have to change something on the char arg.

Okay, you're on the right track. MasterG has edited his post (please don't give the answer away, it doesn't help the OP at all), so see if you can figure out what you need to do with the parameter.

Ahh. I need to define the arg. That's what I lost points on my test for.
Thanks for helping me.

No problem. Take a glance back through your textbook on these areas. Don't be afraid to try things out in your programs to test whether you are right.

Whoops, i made a hollow square xD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.