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?