Write a C program to create identity matrix or unit matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • linda7
    New Member
    • Apr 2014
    • 16

    Write a C program to create identity matrix or unit matrix

    Hi

    I'm writing a program that creates an identity matrix. when I run this program it gives me an error message.

    Any help please !
  • linda7
    New Member
    • Apr 2014
    • 16

    #2
    Hi

    I'm writing a program that creates an identity matrix. when I run this program it gives me an error message.

    Any help please !



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main () {
    	
    	
    	int i,j,m,n;
    	int a;
    	
    	printf(" Please enter the order of the matrix m x n \n");
    	scanf("%d%d",&m,&n);
    
    	a = (int *) malloc(m*n*sizeof(int *)); /* Row pointers */
     
        if (m==n){
    	for(i=0;i<m;i++){
    		
    		for(j=0;j<m;j++) {
    		
    			if (i==j) {
    			a[i][j]=1; 
    	        printf("%d ",a[i][j]);   }
    			else{
    			a[i][j]=0;
    			printf("%d ",a[i][j]);   }	
    		}
    	}
    	printf("Matrix A is\n");
        for(i=0;i<m;i++)
        {
            for(j=0;j<m;j++)
            printf("%d\t",a[i][j]);
            printf("\n");
        }
        
    	}// end if
    	
    	return 0;
    }

    Comment

    • stdq
      New Member
      • Apr 2013
      • 94

      #3
      What are the lines that you're getting errors at? You need a pointer to pointer to hold the address of the first element of the matrix. Assuming you use a, this variable should be declared as:

      Code:
      int **a;
      Allocate the rows with the following statement:

      Code:
      a = ( int ** )malloc( m * sizeof( int * ) );
      Then, use a for statement to allocate memory for the columns of each row:

      Code:
      for ( i = 0 ; i < m ; ++i )
      {
          *( a + i ) = ( int * )malloc( n * sizeof( int ) );
      }
      Now, loop through the whole matrix and assign 1 or 0 to each element:

      Code:
      for ( i = 0 ; i < m ; ++i )
      {
          for ( j = 0 ; j < n ; ++j )
          {
              if ( i != j )
              {
                  a[ i ][ j ] = 0;
              }
              else
              {
                  a[ i ][ j ] = 1;
              }
          }
      }
      Then, print the matrix to check that everything is correct:

      Code:
      printf( "The matrix is:\n\n" );
      
      for ( i = 0 ; i < m ; ++i )
      {
          for ( j = 0 ; j < n ; ++j )
          {
              printf( "%3d", a[ i ][ j ] );
          }
      
          putchar( '\n' );
      }

      Comment

      • linda7
        New Member
        • Apr 2014
        • 16

        #4
        error message: subscripted value is neither array nor pointer ( lines 20, 21, 23, 24, 31).

        Do I have to use double pointer ( int **a ), I mean is there another way to do this ??
        And why it gives these errors !!

        Comment

        • stdq
          New Member
          • Apr 2013
          • 94

          #5
          You're probably getting these errors because a is simply an integer, and not a pointer to pointer. I'm not really sure you can do the following, which also ends up using pointer indirectly:

          Code:
          int a[ m ][ n ];
          If this is correct, the memory is allocated at compile time (I think). Other than that, I can't think of other solution. And like I said, the above line might be incorrect - and maybe that depends on the compiler you're using.

          Comment

          • linda7
            New Member
            • Apr 2014
            • 16

            #6
            Yes it gives error: "incompatib le type", it is fine I will consider the above solution.

            Thank you so much

            Comment

            • stdq
              New Member
              • Apr 2013
              • 94

              #7
              You're welcome. And I forgot: check whether each allocation returns NULL. Also, free all the memory allocated. I think you could do that like this:
              Code:
              free( a );
              a = NULL;
              
              for ( i = 0 ; i < m ; ++i )
              {
                  free( *( a + i ) );
                  *( a + i ) = NULL;
              }
              Someone please correct me if I'm wrong. Here is a complete program, for a matrix that is 4x4, although it might not be perfect:
              Code:
              #include <stdio.h>
              #include <stdlib.h>
              
              int main( void )
              {
                  int **a;
                  int i;
                  int j;
                  int m = 4;
                  int n = 4;
                  
                  a = ( int ** )malloc( m * sizeof( int * ) );
                  
                  if ( a == NULL )
                  {
                      printf( "Memory error...\n" );
                      
                      /* To indicate failure: */
                      return 1;
                  }
                  
                  for ( i = 0 ; i < m ; ++i )
                  {
                      *( a + i ) = ( int * )malloc( n * sizeof( int ) );
                      
                      if ( *( a + i ) == NULL )
                      {
                          printf( "Memory error...\n" );
                      
                          /* To indicate failure: */
                          return 1;
                      }
                  }
              
                  for ( i = 0 ; i < m ; ++i )
                  {
                      for ( j = 0 ; j < n ; ++j )
                      {
                          if ( i != j )
                          {
                              a[ i ][ j ] = 0;
                          }
                          else
                          {
                              a[ i ][ j ] = 1;
                          }
                      }
                  }
              
                  printf( "The matrix is:\n\n" );
                   
                  for ( i = 0 ; i < m ; ++i )
                  {
                      for ( j = 0 ; j < n ; ++j )
                      {
                          printf( "%3d", a[ i ][ j ] );
                      }
                   
                      putchar( '\n' );
                  }
                  
                  /******** I'm not sure my deallocation is correct: /*********/
                  free( a );
                  a = NULL;
                  for ( i = 0 ; i < m ; ++i )
                  {
                      free( *( a + i ) );
                      *( a + i ) = NULL;
                  }
                  
                  /* Successful program termination: */
                  return 0;
              }

              Comment

              • linda7
                New Member
                • Apr 2014
                • 16

                #8
                But I don't need a dynamic array, I just need to make it optional for the user to enter any size of array and then this size become static ( I mean the array become static ), so I don't need to free the memory because no extra memory is available, am I right ??

                Comment

                • stdq
                  New Member
                  • Apr 2013
                  • 94

                  #9
                  If you use malloc, the memory should be freed. At least I think so.

                  Comment

                  Working...