# Stuck on a friend's challange

This is a discussion on Stuck on a friend's challange within the C++ Programming forums, part of the General Programming Boards category; Hi there. A friend and I always are throwing simple challanges at each other and I'd hate to say it ...

1. ## Stuck on a friend's challange

Hi there.

A friend and I always are throwing simple challanges at each other and I'd hate to say it but I think my friend has gotten me stumped.

Heres his instructions:

"Devin it took me a while to think of this but this one will get you. Using only loops create me a program that creats boxes to look like the one below.

Code:
1  1  1  1  0
1  1  1  0 -1
1  1  0 -1 -1
1  0 -1 -1 -1
0 -1 -1 -1 -1
The basic idea is to create a program where if I want a box thats 10x10 it will create it to look similar to that one above. The top left corner has '1' the lower right corner has '-1' and there is a line of '0' that go from top right corner to the lower left corner.

If you get stumped I want my pizza with pineapple and canadian bacon."

If there's anyone that can help ANY help would be appreciated.

even if its the recommendation of a good pizza place.

Thank You

2. Show an attempt to solve the problem and we'll nudge you in the right direction...

gg

3. I'll give you a couple of hints

1. Theres only two possibilities so any incrementation you do will have to loop 2 times.
2. Look at the setup, what normally represents columns and rows? Arrays.
3. Negative numbers, so you've gotta think how you would do that..obviously you can't just decide how many 1's are in one spot and how many 0's are in the other spot. You've gotta increase/decrease at x amount.
4. Notice whats similiar about them. There are 3 numbers, and all of them rely in a certain area in the matrix. So you don't have to do anything funky really, it's all predictable so it isn't that complicated.
5. Something that I Just noticed actually =) Notice how it's incremented.. In the first row, there are 4 1's, in the second row there are 3 1's, in the third row there are 2 1's, and in the fourth row there is 1 1.
-Ontop of that, in the first row there is one -1, in the second 2 -1, in the third 3 -1, in the 4th 4 -1's.
-The only thing that stays the same are the 0's which go at a diagonal.
=From this we can gather, that you will have to decrease something _while_ you are increasing something.
6. There are a set number of times this will loop yes? So basically you don't need a dowhile loop or any sort of a while loop. You will need, however a for loop with a break condition..maybe nested as there are two things to consider? I dunoo..

4. Some hints to solving problems...
1)Try and find out _how_ you would develop the situation without any sort of limitations.
2)Figure out the similarities and the differences in the data, and write them out. See if you can see some sort of a pattern to them.
3)All c++ is is a _way_ to express something. If you know c++ and english well enough, well you'll be able to interchange them, and express lets say..english with c++ or c++ with english perhaps? It's all about seeing the pattern.
-For instance, in this problem, there is a matrix..how do you express matrice's in c++? Arrays.

-I think i've given you enough help, it's not that hard =) Just review your array looping mathematics/perceptions.

5. There is no need for an array.. simple output sufficed fine.

btw, when he says just loops.. what is he excluding? I wrote a version using 1 c library function & 2 for loops.

6. >simple output sufficed fine
That's what I did. Just a couple of loops, and either outputting a 1 or -1. Then the 0 outputted between.

I suppose you could make one loop, but then you'd need if()'s or similar.

7. Are you using math to determine if a -1, 0, or 1 should be output, or are you using if statements. I took no-loops to mean no-if statements, and wrote it using abs().

8. I was lazy, and used the no math approach:
cout << setw(2) << "1" << " ";

9. >I wrote a version using 1 c library function & 2 for loops.
Mine was 3 loops by the way.

10. Owe this is hurting my brain....ack

11. Has anybody done it using only loops & cout?

400!

12. 3 loops and 4 couts for me.

13. Very simple.

Code:
void CreateMatrix(int maxrow,int maxcol)
{
int halfx=maxcol>>1;
int halfy=maxrow>>1;

int Matrix[row][col];
memset(matrix,row*col,0);

for (int row=0;row<maxrow;row++)
{
for (int col=0;col<maxcol;col++)
{
if (row<halfy || col<halfx)
{
Matrix[row][col]=1;
}
if (row>halfx || col>halfy)
{
Matrix[row][col]=-1;
}
}
}

..print out however you want
}
Might be just the opposite result...but not hard to fix.
This should leave one of the matrix axes untouched leaving all zeros since it has been zeroed.

If you want to do it in a while loop...you should be able to figure that out as well.

For a single loop unroll the inner loop and access the matrix by using (row*maxcol)+col.

14. Great, Bubba ruins the whole thing by posting a complete solution ...
Oh well, it was fun while it lasted.

15. atleast it doesn't compile

gg

Page 1 of 3 123 Last