Proportionally enlarging a 2D array

This is a discussion on Proportionally enlarging a 2D array within the C++ Programming forums, part of the General Programming Boards category; it goes something like this... Code: int main_grid[5][10] 1 0 1 0 1 1 0 1 0 1 1 0 ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Proportionally enlarging a 2D array

    it goes something like this...
    Code:
    
    
    int main_grid[5][10]
    
    1 0 1 0 1 1 0 1 0 1
    1 0 1 0 1 1 0 1 0 1
    1 0 1 0 1 1 0 1 0 1
    1 0 1 0 1 1 0 1 0 1
    1 0 1 0 1 1 0 1 0 1
    
    
    
    char display_grid[11][21]
    
    +-+-+-+-+-+-+-+-+-+-+
    |X| |X| |X|X| |X| |X|
    +-+-+-+-+-+-+-+-+-+-+
    |X| |X| |X|X| |X| |X|
    +-+-+-+-+-+-+-+-+-+-+
    |X| |X| |X|X| |X| |X|
    +-+-+-+-+-+-+-+-+-+-+
    |X| |X| |X|X| |X| |X|
    +-+-+-+-+-+-+-+-+-+-+
    |X| |X| |X|X| |X| |X|
    +-+-+-+-+-+-+-+-+-+-+
    
    
    



    so it's like transferring another array to another array with similar size and data type. I notice a pattern...

    Code:
    display main			display	main	
    row	row	difference	col	col	difference
    0	-	-		0	-	-
    1	0	1		1	0	1
    2	-	-		2	-	-
    3	1	2		3	1	2
    4	-	-		4	-	-
    5	2	3		5	2	3
    6	-	-		6	-	-
    7	3	4		7	3	4
    8	-	-		8	-	-
    .	.	.		.	.	.
    .	.	.		.	.	.
    In theory, you can get the display = main - difference, but I can't it t work correctly...

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    
    using namespace std;
    
    int main()
    {
        int i, j, , x, y, m, n;
        char d=32; //ascii code for white space " "
       char display_grid[11][21]={};
        
        int main_grid[5][10]={{1,0,1,0,1,1,0,1,0,1},
                               {1,0,1,0,1,1,0,1,0,1},
                               {1,0,1,0,1,1,0,1,0,1},
                               {1,0,1,0,1,1,0,1,0,1},
                               {1,0,1,0,1,1,0,1,0,1}}; //sample values
                               
    
        for ( i=0 ; i<5 ; i++ )
        {
            for ( j=0 ; j<5 ; j++ )
                cout<<main_grid[i][j]<<" ";
            cout<<endl;
        }
        
        cout<<"\n\n\n";
            
            for ( i=0 ; i<11 ; i++ )
            {
                
                n=0;
                for ( j=0 ; j<21 ; j++ )
                {
                    
                    if (( i%2==0 ) && ( j%2==0 ))
                        display_grid[i][j]='+';
                       
                    else if (( i%2==0 ) && ( j%2!=0 ))
                        display_grid[i][j]='-';
                            
                    else if (( i%2!=0 ) && ( j%2==0 ))
                        display_grid[i][j]='|';
    
                    else if (( i%2!=0 ) && ( j%2!=0 ))
                    {
                    for ( m=0 ; m<5 ; m++ )
                    {
                        for ( n=0 ; n<5 ; n++ )
                        {
                            if ( main_grid[i-m][j-m]!=0 )
                            display_grid[i][j]='X';
                            else
                            display_grid[i][j]=d;
                        }
                    }
                    
                }
                
            }
        }
              
               
        
        for ( i=0 ; i<11 ; i++ )
            {
                for ( j=0 ; j<21 ; j++ )
                {
                    
                cout<<display_grid[i][j];
                }
                cout<<endl;
            }
    
        getch();
        return 0;
    }
    What's wrong with the loops?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
       for (int i=0,r=0; i<11; i++)
       {
          for (int j=0,c=0; j<21; j++)
          {
             if (i%2 == 0)
             {
                if (j%2 == 0)
                   display_grid[i][j] = '+';
                else
                   display_grid[i][j] = '-';
             }
             else
             {
                if (j%2 == 0)
                   display_grid[i][j] = '|';
                else if (main_grid[r][c++] == 0)
                   display_grid[i][j] = ' ';
                else
                   display_grid[i][j] = 'X';
             }
          }
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 03:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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