Thread: transpose of a 2D array seg faults on non-square arrays

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    transpose of a 2D array seg faults on non-square arrays

    Hi i'm having trouble with a function that simply computes the transpose of a 2D array. It works fine for square arrays, but then seg faults on non-square arrays. Any Suggestions on what might be wrong?


    Code:
    #include "stdio.h"
    #include "stdlib.h"
    
    void  transpose(int rows, int cols, double** orig_arr, double** transpose){
      //transpose[cols][rows] = orig_arr[rows][cols];
        int i, j;
    
        for(i=0;i<rows; i++) { 
          for (j=0; j<cols; j++){
    	transpose[j][i]=orig_arr[i][j];
          }
        }
    }
    
    int main(){
    
      int row;
      int col;
      int i=0; //counter for for loops
      int j=0; //counter for for loops
      float x;
    
      printf("How man rows would you like to imput? :");
      scanf("%i", &row);
      printf("How man cols would you like to imput? :");
      scanf("%i", &col);
    
      /* Allocate space for the imput array:*/
      double** arr=(double**) malloc(row * sizeof(double*));
      for(i=0;i<col;i++) {
        arr[i]=(double*) malloc(col * sizeof(double));
       }
       
      /* Allocate space for the transpose array:*/
      double** transpose_arr=(double**) malloc(col* sizeof(double*));
      for(i=0;i<row;i++) {
        transpose_arr[i]=(double*) malloc(row * sizeof(double));
        }
    
      /*Read in the elements of the array*/
      for(i=0;i<row;i++) {
        printf("\nEnter your value for row %i:\n arr[%i]", i, i);
        for(j=0;j<col;j++) {
          if (j==0)                                  //If statemet just makes the  
    	{printf("arr[%i]:", j);            //printing on the screen look
    	}                                           //orderly. 
          else {printf("       arr[%i]:", j);
          }
          scanf("%f", &x);
          arr[i][j]=x;
        }
      }
    
      /*Print back the array to the user*/
      printf("Your array is:\n");
      for(i=0;i<row;i++) { 
        for (j=0; j<col; j++){
          printf("%f ", arr[i][j]);
          }
        printf("\n");
        }
      printf("\n");
    
      /*Call function to compute the transpose of the imput array*/
      printf("The transpose of that array is:\n");  
      transpose(row, col, arr, transpose_arr);
     
         for(i=0;i<row;i++) { 
          for (j=0; j<col; j++){
    	printf("%f ", transpose_arr[i][j]);
          }
          printf("\n");
        }
      printf("\n");
    
      free(arr);
      free(transpose_arr);
      arr=NULL;
      transpose_arr=NULL;
      return 0;
    }
    Thanks so much!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's breaking when you print it. You're trying to print the dimensions of the original array, not the transposed one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    16
    Ok I changed the row and col in the printing loop, but it is still giving me a seg fault. Is that what I needed to change?
    Code:
    for(i=0;i<col;i++) { 
          for (j=0; j<row; j++){
    	printf("%f ", transpose_arr[i][j]);
          }
          printf("\n");
        }
      printf("\n");

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your malloc's are still wrong. If you initially allocate room for row * (sizeof double *), then your for loop after that had better go from 0 to row, not from 0 to col.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    Oh wow! Thank you so much, that fixed it!

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, 03:45 AM
  2. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM