If I have a 1 dimensional array of data such as this:
1 2 3 4 5 6 7 8 9 1
and a corresponding data access table:
0 2 5 8
How would I print the 2D diagonalized array below with the zeros?
1 2 0 0
3 4 5 0
0 6 7 8
0 0 9 1
Code would be very helpful. Thanks
Here is what I tried...the if statement needs tweaking:
//table[] is the access table
//data[] is the 1D array
//grid[][] is the 2D array
for(row=0; row<sideSize; row++)
for(col=0; col<sideSize; col++){
if(col < table[col+1] - table[col]-1)
grid[row][col] = data[col+row];
else
grid[row][col] = 0;
}
Here is the entire function so far:
void printMatrix(int* table, int* data, int diagonals, int arraySize, int sideSize){
int** grid;
int row, col, i;
grid = (int**)malloc(sizeof(int)*sideSize);
for(i=0; i<sideSize; i++)
grid[i] = (int*)malloc(sizeof(int)*sideSize);
printf("\n");
for(row=0; row<sideSize; row++)
for(col=0; col<sideSize; col++){
if(col < table[col+1] - table[col]-1)
grid[row][col] = data[col+row];
else
grid[row][col] = 0;
}
for(row=0; row<sideSize; row++){
for(col=0; col<sideSize; col++)
printf("%d ", grid[row][col]);
printf("\n");
}
for(i=0; i<sideSize; i++)
free(grid[i]);
free(grid);
free(table);
free(data);
}
Alok your method of printing the middle rows won't work for this matrix for example:
9 7 5 0 0 0 0 0 0
8 9 7 5 0 0 0 0 0
5 8 9 7 3 0 0 0 0
0 5 8 9 7 3 0 0 0
0 0 3 8 9 7 5 0 0
0 0 0 3 8 9 7 5 0
0 0 0 0 5 8 9 7 8
0 0 0 0 0 5 8 9 9
0 0 0 0 0 0 8 9 9