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. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    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. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Show an attempt to solve the problem and we'll nudge you in the right direction...

    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    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..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    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.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    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. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    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. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I was lazy, and used the no math approach:
    cout << setw(2) << "1" << " ";

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I wrote a version using 1 c library function & 2 for loops.
    Mine was 3 loops by the way.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Owe this is hurting my brain....ack
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    Has anybody done it using only loops & cout?

    400!

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    3 loops and 4 couts for me.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 04-20-2004 at 09:59 PM.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Great, Bubba ruins the whole thing by posting a complete solution ...
    Oh well, it was fun while it lasted.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    atleast it doesn't compile

    gg

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21