tags:

views:

316

answers:

3

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
A: 

The data access table just tells you where each row of data is put in the 1D array. So the 1 2 in the first row starts at index 0. The 3 4 5 in the second row starts at index 2. The 6 7 8 in the third row starts at index 5 and so on...

This sounds good but your example:

1 2 0 0

3 4 5 0

0 6 7 8

0 0 9 1

I don't see the 3 4 5 starting at index two. Please explain.

Hogan
The 3 4 5 starts at index 2 in the 1D array not the matrix.
Andrew
ok, how do you know where to put it in the matrix? or is always these spots in the matrix that get filled?
Hogan
That's what I'm trying to figure out. I have the 1D array and the data access table (don't know how this table is really useful in making the matrix) and I need to make the matrix with the 0's in it.
Andrew
hmm... what does the homework assignment say?
Hogan
Write a program that will read a diagonalized matrix froma file and store it in a 1-d array with a corresponding1-d array access table. You will need to read on the firstline to discover the number of diagonals with data. Theprogram should have a functionthat loads the arrays and then a function that writes thematrix out on standard output in matrix form (ie, it shouldlook the same as it does in the file) This program shouldwork for any size matrix, so you need to figure the sizeof the matrix before you allocate the space for the arrays.
Andrew
A: 
Alok
Yes, that is what the assignment is except the file contains only the matrix. No N number on the first line. I have already done everything you have just described except for printing the 1D array back to matrix form. Any ideas on how to do that also remembering that there can be more than 1 diagonal. Thanks.
Andrew
If there is no `n`, you have to read the first line to figure out the value of `n`. Just count the number of numbers.
Alok
See my edit about printing.
Alok
Your algorithm to print the middle rows isn't quite right for every matrix. See the addition to my original post.
Andrew
A: 

Ok, I found a way to print the matrix using the data access table. You can do it by starting from the middle row(s) (for even matrices there are 2 middle rows) of the matrix (since it is symmetric) and calculating the padding of zeros which will have the same number on both sides. Then you just decrement that value for the rows above the middle row and increment for each row below it (on the left side). Then you use the access table to insert the data elements for that row. Finally add zeros to the right side of the data for each row.

Thanks for your suggestions!

Andrew