//flex table opened by JP

Click to See Complete Forum and Search --> : basic nested-for loop


rrcn
11-12-2003, 03:43 PM
I am having a little trouble creating an algorithm which prints to the screen an empty 5 X 5 square. I have already created one that prints to the screen a filled square:


FOR I <-- 1 TO 5
For J <-- 1 TO 5
OUTPUT '*', more
ENDFOR
OUTPUT
ENDFOR


The result should look like this:

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


Thanks for your time and help. :)
Peace.

suny
11-12-2003, 07:18 PM
you could set up a if statement in the second for loop checking for the value of I == 1 or I == 5 then you do the current output,

the else statement is where you could set up an additional
if statement to check if J is 1 or 5 and print *
else print the whitespace.

(hope that is clear enough)

rrcn
11-12-2003, 07:56 PM
I dont exactly understand. Maybe if you explain what you would do line by line if that isnt any trouble.

Thanks again suny.

nothing
11-12-2003, 08:16 PM
This is how I would to it in C++

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

int main()
{
int size = 5;

for( int x = 0; x < size; x++ )
cout << "*";

cout << endl;

for( int y = 2; y < size; y++ )
cout << "*" << setw( size ) << "*\n";

for( int z = 0; z < size; z++ )
cout << "*";

cout << endl;

return 0;
}


I guess the only thing you won't understand in this program is the setw function. This function prints whitespaces on the screen.


cout << "*" << setw( size ) << "*\n";


This line prints an asterisk, then it prints five whitespaces and then it prints another asterisk followed by a new line. I hope this helps :t

rrcn
11-12-2003, 08:21 PM
Now I see. Alright, thanks for your help.

rrcn
11-12-2003, 08:41 PM
how would you apply setw( size ) to assembly code?

suny
11-12-2003, 09:27 PM
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if( i == 0 || i == 4)
{
cout<<"*";
}
else // i is 1 or 2 or 3
{
if(j == 0 || j== 4)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<endl;
}
}


i was basically stating this

rrcn
11-12-2003, 09:36 PM
I now completely understand what you were saying suny. Thank you very much for your time.

Peace

rrcn
11-12-2003, 09:49 PM
by the way suny, i ran your code but something is wrong with it. i'm trying to figure it out. check it out...

nothing
11-12-2003, 11:00 PM
rrcn, the piece of code suny posted won't compile because it is not complete. He just wanted to help you understand what he meant in his previous post.

rrcn
11-12-2003, 11:04 PM
i didn't say it wasn't compiling. i was saying that there is some logical error in it. of course it doesn't compile, it's incomplete...i program in c++ too.

anyway, that's ok. thank you both suny and nothing for your help today.

nothing
11-13-2003, 07:16 AM
Ooops. My mistake. I thought you didn't know C/C++ :rolleyes:

DocEvi1
11-13-2003, 07:00 PM
in pseudo code you want (java comments)

for i == 1 to 5 do //outside loop, downwards
for j == 1 to 5 do //inside loop, across
if((i == 1 or 5) or (j == 1 or 5)) //print horizontal ***'s or print * *
print *
else if ((i <> 1 or 5 ) and (j <> 1 or 5))
print " "
od
od


what machine code are you wanting to produce it in? it looks a bit like BASIC used to look like, but I'm not sure.

rrcn
11-14-2003, 02:38 AM
Thanks docevi1 for the psudocode. This language is pretty much like basic, but we call it O'Hanlon code in class :D. We named it after the writer of the book.

That psudocode you wrote it in was exactly what I was looking for. Anyway, thanks again.

DocEvi1
11-14-2003, 02:19 PM
you're welcome.