New to c++ creating functions

My task:
These three functions must be in a single file:
- Write a function named `square` that squares an int.
- Write a function named `square` that squares a double.
- Write a function named `square` that takes in an int and a char, and prints out a square of that char, with the int as the side length.
- Example: square(5, '*') will print out a 5x5 square of asterisks.
so far I have accomplised:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int square(int&x)
{
	return x*x;
}
double square(int&x)
{
	return x*x;
}


int main()
{


	return 0;
}


I have hit a wall and I need help asap! Much appreciated for the help!
You'll need two loops. for loop would be best (see http://www.cplusplus.com/doc/tutorial/control/).
You may want to print your characters onto your terminal. Use standard output stream cout (see http://www.cplusplus.com/doc/tutorial/files/ and the IO section of the reference pages).

double square(int&x) doesn't square a double. It squares an int.
Careless mistake on the double/int one on my part.
You don't really need to pass basic data types like int into functions as references.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

void square(int x, char ch)
{
	// add some error checking to make sure int is >0

	for(int i=0;i < x; ++i)
	{
		for(int j=0;j < x; ++j)
		{
			cout << ch;
		}
		cout << endl;
	}
}
Topic archived. No new replies allowed.