The query that i have is that it is pretty easy to print a hollow square using loop but is thee a way to do the same with out using any loops or functions.
Technically main() is a function, so it cannot be done without functions.
As far as variable-size squares, I don't know how you would add the middle rows without using loops. Somehow you have to count the middle rows to make sure the correct number are added to the square, and I don't see how that can be done without loops (assuming goto is considered a loop per @Ganado's question). A string could be allocated with the correct size, but still the characters in the string will need to be manipulated to transform into rows forming the square, and a loop of some type will be needed to do so.
So, I don't know of a way to do this without loops.
First, constructor the outer 2x rows using the constructor (2).
Then, you need to make one inner row using constructor (2).
Then, you need to repeat the constructed inner row height-2 times. This can be done with std::fill_n, I believe?
Obviously it's still looping under the hood.
Wait but fill_n is a function. Darn it!
But + operator is a "function" as well. I hate ill-defined questions.
If you set a maximum allowed size, you can unroll the loop uncleanly, with conditions and a counter...
counter++
if(counter < dimension)
spew out another line
... over and over until max allowed dimension,
then write the final bottom line...
if you get really keyed up about how loop is defined, the string allocations we are suggesting may contain a hidden loop. Again, it depends on the rules you want to play by.
it may be possible to make the string via macro expansions or something too. There are lots of hackery things c++ allows that we generally don't even consider doing until painted into a corner by some challenge.
sorry for the late reply but this is what i have done so far but i wana do this without using for and while loops
Write a program that reads in the size of the side of a square then prints a hollow square of that size out of asterisksand blanks.Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 5, it should print
*****
* *
* *
* *
*****
#include<iostream>
using namespace std;
int main()
{
cout << "Enter the size between 1 and 20 for the sides of a square : ";
int size, i, j;
cin >> size;
if (size >= 1 && size <= 20)
{
cout << "The size of the sides of your square is: "<<size<<endl;
cout << "This is your square : \n";
for (i = 0;i < size;i++)
{
for (j = 0;j < size;j++)
{
if (i == 0 || i == (size - 1))
{
cout << "*";
}
If you want it to be done as an iterative process, with each line being calculated as the program runs rather than hardcoded, and you can't use loops, then you should consider using recursion instead. That's the obvious alternative to loops, for an iterative process.
EDIT: Ignore that - recursion would involve writing a function, which you apparently are forbidden to do.
Has your teacher given you any other hints on what techniques to use?
While I would never, ever, recommend using goto, is it possible this is what your teacher is expecting you to use?
i know how to use loops and functions and we didnt went past if else the loops (for and while) will be covered in the next lecture but i have to solve this before that but i cant think of a way to keep it simple and not use anything complex. Thanks for haring ur input guys this is helping me learn but im going to move forward and go with what i know best
sorry agrito, you have sent us off into toyland ... your question has us playing with how to do it without the tools we are used to using and that yields exotic, humorous but unhelpful responses.
Let me see if I can do something less weird. Post what you come up with too.
lets try this. it does use something you likely havent seen, the ostringstream, but otherwise, its dead simple..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
int dim = 5; //example square dimension
string s(dim,'*'); //the top and bottom lines will be solid, the * letter dim times.
string h(dim,' '); //dim spaces.
h[0] = h[h.length()-1] = '*'; //fill in the first and last letter to * to make 'walls'
h+= '\n'; //add end of lines, not necessary, but handy.
s+= '\n';
ostringstream os;
fill_n(std::ostream_iterator<std::string>(os), dim-2, h); //fill dim-2 copies of the wall string complete with end of lines.
cout << s << os.str() << s << endl; //print the square. the top, the middle, and the bottom. the middle is, again, just dim-2 copies of the wall string, which is, again, first and last char * and dim-2 in the middle spaces.
}
I just realized this is virutally the same as Ganado's. A little shorter, but not much, and same idea. I think this is about as good as you are going to get if you want simple. And, fill_n is just hiding a function that contains a loop -- its exploiting built in tools to hide the things you said not to do.