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; Guess we may as well start posting them Code: void box(int n) { for(int i=0; i < n; ++i) { ...

  1. #16
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    Guess we may as well start posting them

    Code:
    void box(int n)
    {
        for(int i=0; i < n; ++i)
        {
            for(int j=n; j > 0; --j)
            {
                cout.width(2);
                cout << abs(j-i-1) / ((j-i-1) ? (j-i-1) : 1) << " ";
            }
            cout << endl;
        }
    }
    Last edited by cozman; 04-20-2004 at 11:06 PM. Reason: code tags

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Codeplug
    atleast it doesn't compile

    gg
    Or work.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That's clever Cozman. All you needed was basically one cout.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, it's actually two cout's, but it looks like one with the ternary operator, or whatever it's called. Clever nontheless.

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I coded it while sitting here...of course it probably doesn't work...but I could get it to.

    Not a very difficult problem.

  6. #21
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    Talking

    Hi there... sorry about not posting what I had gotten so far. I posted the question then I had to run off to class and I forgot about this topic until I recieved an e-mail saying there was a reply... looking back it almost looks like I was asking for someone to do all the work for me... my apologies....

    now...what I had when I posted that was nothing but garbage... This morning I figured I'd take it step by step...


    I figured first thing I'd do is that the FIRST loop is one that creates each line

    Then I would have the program subtract 1 from the initial size each time the loop goes through (decreasing the 1's all the way down)...

    Then I'd have it print a "0" to seperate the 1 and the -1

    then the program would take the number of 1's in the program subtract that from the size that was wanted...and have that loop that many times while printing -1's

    ok now that i've probably confused everyone trying to explain...

    I came up with the results below...

    Code:
    #include<iostream.h>
    
    int main ()
    
    {
    
    	int s,p,n,z,g,a;
    
    	z=0;
    
    	cout<<"How big do you want your box? ";
    	cin>>s;
    	cout<<endl;
    
    	p=s;
    
    	for (a=0;a<s;++a)
    
    	{
    		p=--p;
    
    		for (n=p; n>0; --n)
    		{
    			cout<<" 1"<<" ";
    		}
    
    		cout<<" "<<z<<" ";
    
    		for (g=s-1-p;g>0; --g)
    		{
    			cout<<"-1"<<" ";
    		}
    
    		cout<<endl;
    
    	}
    
    	return 0;
    
    }

  7. #22
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    #include <stdio.h>
    #include <dos.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <mem.h>
    
    
    
    void CreateMatrix(int maxrow,int maxcol);
    
    
    int main(void)
    {
      clrscr();
      CreateMatrix(10,10);
      getch();
      return(0);
    }
    
    void CreateMatrix(int maxrow,int maxcol)
    {
    
      int size=maxrow*maxcol;
      int *Matrix=(int *)malloc(size);
      int offset=maxcol-1;
    
      int value=0;
    
      for (int i=0;i<maxrow;i++)
      {
        for (int j=0;j<maxcol;j++)
        {
          if (j<offset) value=1;
          if (j==offset) value=0;
          if (j>offset) value=-1;
          Matrix[i*maxcol+j]=value;
        }
        offset--;
       }
    
    }
    This does work.

    If you want it in a single loop then set offset to maxcol the first time. Save maxcol-1 in a temp variable. Then test against temp variable -> decrement temp variable -> test against temp variable.

    The idea is if you add maxcol to the offset you simply move down one row in the matrix -> just like in graphics 320x200 8 bit color when you add 320 you move down one pixel. But if you move 319, 318, 317, 316, etc., etc you will create a diagonal line from top left to bottom left.


    This code works with any size matrix. To test it you will probably need to set the other value to 2 since -1 actually takes up 2 character cells and screws the whole printout up. But it works.

    My apologies for my first post...my logic was a bit off.


    Here is the correct printout code using printf() - change to cout if you wish - I use both.

    Code:
    for (int j=0;j<maxrow;j++)
    {
      for (int k=0;k<maxcol;k++)
      {
        int value=Matrix[j*maxcol+k];
        if (value==0 || value==1) printf("  %d",value);
        if (value==-1) printf(" %d",value);
      }
      printf("\n");
    }
    Sorry it's not pure C++, but you don't need it to solve this problem.
    Last edited by VirtualAce; 04-21-2004 at 01:57 PM.

  8. #23
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    sorry for some confusion but the program was also supposed to ask the user how big they wanted the box...

    the size of the box was not supposed to be programmed into it. But to be a variable that depends on the size the user requests.... sorry if that wasn't clear...

  9. #24
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Ok....so get the user input and then send the correct values in maxrow and maxcol.

    It is already setup to handle any values

    ..Get user input and store in numrows and numcols
    CreateMatrix(numrows,numcols);

  10. #25
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void box(int n) {
       int j = -1, k = -1;
       for (int i = 0; i < n * n; ++i) {
          if (j++ == n - 1) {
             k = -1;
             j = 0;
             cout << endl;
          } else if (j == i / n) {
             k = 0;
          } else if (j > i / n) {
             k = 1;
          }
          cout << setw(2) << k << ' ';
       }
    }
    
    int main() {
       int n;
       cout << "sides = " << flush;
       cin >> n;
    
       box(n);
       return 0;
    }
    Prints:
    Code:
    sides = 5
     0  1  1  1  1
    -1  0  1  1  1
    -1 -1  0  1  1
    -1 -1 -1  0  1
    -1 -1 -1 -1  0
    Except its the other way around! One loop.

    EDIT: Darn I wanted to not give him the correct code so I did it the opposite way. Oh well. Its an easy fix to make it the other way.
    Last edited by Speedy5; 04-21-2004 at 05:34 PM.

  11. #26
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by wiresite
    sorry for some confusion but the program was also supposed to ask the user how big they wanted the box...

    Quote Originally Posted by wiresite
    Code:
    #include<iostream.h>
    
    int main ()
    
    {
    
    	int s,p,n,z,g,a;
    
    	z=0;
    
    	cout<<"How big do you want your box? ";
    	cin>>s;
    	cout<<endl;
    
    	p=s;
    
    	for (a=0;a<s;++a)
    
    	{
    		p=--p;
    
    		for (n=p; n>0; --n)
    		{
    			cout<<" 1"<<" ";
    		}
    
    		cout<<" "<<z<<" ";
    
    		for (g=s-1-p;g>0; --g)
    		{
    			cout<<"-1"<<" ";
    		}
    
    		cout<<endl;
    
    	}
    
    	return 0;
    
    }
    didnt u post ur own solution already?

  12. #27
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>void CreateMatrix(int maxrow,int maxcol);
    Ever since he got into 3d graphics, Bubba has been really enthusiastic about the matrices

    My solution (not tested):
    Code:
    for(int i = 0; i < width; ++i)
    {
        for(int j = 0; j < (height - (i + 1)); ++j)
        {
            array[i][j] = 1;
        }
        array[i][j] = 0;
        for(j = j + 1; j < height; ++j)
        {
            array[i][j] = -1;
        }
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #28
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >This does work.
    Not exactly Bubba.

    > int *Matrix=(int *)malloc(size);
    This should be:
    int *Matrix=(int *)malloc(size* sizeof(*Matrix));

    And you never freed your memory.

  14. #29
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I changed that - not sure why it didn't show up here. Perhaps I changed it in my source but didn't paste it here.

    Anyways in my code I freed the memory.

    I stand corrected.

    [snipped here for a stupid oversight on my part]
    Last edited by VirtualAce; 04-22-2004 at 03:38 PM.

  15. #30
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> malloc allocates a block of size bytes from the memory heap...

    An integer is 2 or 4 bytes, depending on the compiler.

    >> I am allocating size bytes which is the amount of storage I need for the array

    It's an integer array so you need size*sizeof(int) bytes.
    sizeof(*Matrix) is the same as sizeof(int), which is correct in this case.

    gg

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

Similar Threads

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

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