473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Write a C program to create identity matrix or unit matrix

16
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 !
May 18 '14 #1
8 5395
linda7
16
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 !



Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main () {
  4.  
  5.  
  6.     int i,j,m,n;
  7.     int a;
  8.  
  9.     printf(" Please enter the order of the matrix m x n \n");
  10.     scanf("%d%d",&m,&n);
  11.  
  12.     a = (int *) malloc(m*n*sizeof(int *)); /* Row pointers */
  13.  
  14.     if (m==n){
  15.     for(i=0;i<m;i++){
  16.  
  17.         for(j=0;j<m;j++) {
  18.  
  19.             if (i==j) {
  20.             a[i][j]=1; 
  21.             printf("%d ",a[i][j]);   }
  22.             else{
  23.             a[i][j]=0;
  24.             printf("%d ",a[i][j]);   }    
  25.         }
  26.     }
  27.     printf("Matrix A is\n");
  28.     for(i=0;i<m;i++)
  29.     {
  30.         for(j=0;j<m;j++)
  31.         printf("%d\t",a[i][j]);
  32.         printf("\n");
  33.     }
  34.  
  35.     }// end if
  36.  
  37.     return 0;
  38. }
May 18 '14 #2
stdq
94 64KB
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:

Expand|Select|Wrap|Line Numbers
  1. int **a;
Allocate the rows with the following statement:

Expand|Select|Wrap|Line Numbers
  1. a = ( int ** )malloc( m * sizeof( int * ) );
Then, use a for statement to allocate memory for the columns of each row:

Expand|Select|Wrap|Line Numbers
  1. for ( i = 0 ; i < m ; ++i )
  2. {
  3.     *( a + i ) = ( int * )malloc( n * sizeof( int ) );
  4. }
Now, loop through the whole matrix and assign 1 or 0 to each element:

Expand|Select|Wrap|Line Numbers
  1. for ( i = 0 ; i < m ; ++i )
  2. {
  3.     for ( j = 0 ; j < n ; ++j )
  4.     {
  5.         if ( i != j )
  6.         {
  7.             a[ i ][ j ] = 0;
  8.         }
  9.         else
  10.         {
  11.             a[ i ][ j ] = 1;
  12.         }
  13.     }
  14. }
Then, print the matrix to check that everything is correct:

Expand|Select|Wrap|Line Numbers
  1. printf( "The matrix is:\n\n" );
  2.  
  3. for ( i = 0 ; i < m ; ++i )
  4. {
  5.     for ( j = 0 ; j < n ; ++j )
  6.     {
  7.         printf( "%3d", a[ i ][ j ] );
  8.     }
  9.  
  10.     putchar( '\n' );
  11. }
May 18 '14 #3
linda7
16
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 !!
May 18 '14 #4
stdq
94 64KB
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:

Expand|Select|Wrap|Line Numbers
  1. 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.
May 18 '14 #5
linda7
16
Yes it gives error: "incompatible type", it is fine I will consider the above solution.

Thank you so much
May 18 '14 #6
stdq
94 64KB
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:
Expand|Select|Wrap|Line Numbers
  1. free( a );
  2. a = NULL;
  3.  
  4. for ( i = 0 ; i < m ; ++i )
  5. {
  6.     free( *( a + i ) );
  7.     *( a + i ) = NULL;
  8. }
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main( void )
  5. {
  6.     int **a;
  7.     int i;
  8.     int j;
  9.     int m = 4;
  10.     int n = 4;
  11.  
  12.     a = ( int ** )malloc( m * sizeof( int * ) );
  13.  
  14.     if ( a == NULL )
  15.     {
  16.         printf( "Memory error...\n" );
  17.  
  18.         /* To indicate failure: */
  19.         return 1;
  20.     }
  21.  
  22.     for ( i = 0 ; i < m ; ++i )
  23.     {
  24.         *( a + i ) = ( int * )malloc( n * sizeof( int ) );
  25.  
  26.         if ( *( a + i ) == NULL )
  27.         {
  28.             printf( "Memory error...\n" );
  29.  
  30.             /* To indicate failure: */
  31.             return 1;
  32.         }
  33.     }
  34.  
  35.     for ( i = 0 ; i < m ; ++i )
  36.     {
  37.         for ( j = 0 ; j < n ; ++j )
  38.         {
  39.             if ( i != j )
  40.             {
  41.                 a[ i ][ j ] = 0;
  42.             }
  43.             else
  44.             {
  45.                 a[ i ][ j ] = 1;
  46.             }
  47.         }
  48.     }
  49.  
  50.     printf( "The matrix is:\n\n" );
  51.  
  52.     for ( i = 0 ; i < m ; ++i )
  53.     {
  54.         for ( j = 0 ; j < n ; ++j )
  55.         {
  56.             printf( "%3d", a[ i ][ j ] );
  57.         }
  58.  
  59.         putchar( '\n' );
  60.     }
  61.  
  62.     /******** I'm not sure my deallocation is correct: /*********/
  63.     free( a );
  64.     a = NULL;
  65.     for ( i = 0 ; i < m ; ++i )
  66.     {
  67.         free( *( a + i ) );
  68.         *( a + i ) = NULL;
  69.     }
  70.  
  71.     /* Successful program termination: */
  72.     return 0;
  73. }
May 18 '14 #7
linda7
16
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 ??
May 18 '14 #8
stdq
94 64KB
If you use malloc, the memory should be freed. At least I think so.
May 18 '14 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Job Lot | last post by:
How can I make my setup program create folders with specific name while installing application? I want to create these folders in application folder. Thanks
0
by: snacampbell | last post by:
My project is at a standstill because I cannot get the SQL code below to run without error. I'm using Visual Basic 6.0, referencing dao360.dll (not referencing dao351.dll), and referencing MDAC 2.8...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.