I want to make a function to fill a matrix. The function receives the number of rows and columns and returns the matrix (User fills in the matrix). I have to use pointer?
Matrix
Started by Apprentice123, Jun 24 2009 01:02 PM
10 replies to this topic
#1
Posted 24 June 2009 - 01:02 PM
|
|
|
#2
Posted 24 June 2009 - 03:32 PM
In C++ you can use a vector of vectors.
#3
Posted 24 June 2009 - 03:41 PM
WingedPanther said:
In C++ you can use a vector of vectors.
I am learning C. Can you show me an example? Thanks
#4
Posted 24 June 2009 - 03:46 PM
With C, you'll have to use a pointer to an array of ints.
#5
Posted 24 June 2009 - 03:50 PM
WingedPanther said:
With C, you'll have to use a pointer to an array of ints.
I thought to use pointer to pointer and malloc function. But I do not know how
#6
Posted 24 June 2009 - 03:53 PM
A 2-dimensional array and a 1-dimensional array are basically stored the same way in memory. I'm a little rusty how the details of how you would have to do this, since I don't normally use C.
#7
Posted 24 June 2009 - 04:43 PM
WingedPanther said:
A 2-dimensional array and a 1-dimensional array are basically stored the same way in memory. I'm a little rusty how the details of how you would have to do this, since I don't normally use C.
I do not understand and pointers, this makes me. how time do you think of study to learn enough pointer?
#8
Posted 24 June 2009 - 04:55 PM
#9
Posted 25 June 2009 - 01:25 AM
This is how i would probably code it
int** createMatrix( unsigned int rows, unsigned int columns )
{
assert( 0 < rows );
assert( 0 < columns );
int element;
int** pArray;
int result;
element = 0;
element = sizeof( int );
pArray = 0;
pArray = ( int** )malloc( rows * element );
if ( 0 == pArray )
{
return 0;
} // if
memset( &result, 0, element );
memset( pArray, 0, rows * element );
for ( int it = 0; it < columns; ++it )
{
pArray[ it ] = ( int* )malloc( columns * element );
if ( 0 == pArray[ it ] )
{
result = -1;
break;
} // if
memset( pArray[ it ], 0, columns * element );
} // for
if ( 0 != result )
{
for ( int it = 0; it < columns; ++it )
{
free( pArray[ it ] );
} // for
free( pArray );
return 0;
} // if
return pArray;
}
#10
Posted 25 June 2009 - 07:20 AM
Thanks
#11
Posted 25 June 2009 - 01:41 PM
The most efficient way would be to store the matrix as a linear array of ints, or if you prefer, a pointer to a linear array of ints. See the following code as an example:
Avoid multi-dimensional implementations if you can! ;)
#include <stdio.h>
// We are going to set up a 2x2 matrix.
#define MATRIX_WIDTH 2
#define MATRIX_HEIGHT 2
typedef unsigned int uint;
uint matrix_2x2[MATRIX_WIDTH + MATRIX_HEIGHT];
int main()
{
// Initialise the array...
uint elementValue = 0;
for(int i = 0; i < MATRIX_WIDTH; i++)
for(int j = 0; j < MATRIX_HEIGHT; j++)
{
matrix_2x2[(i * MATRIX_WIDTH) + j] = elementValue;
elementValue++;
}
printf("|%d, %d|\n|%d, %d|\n", matrix_2x2[0], matrix_2x2[1], matrix_2x2[2], matrix_2x2[3]);
}
Avoid multi-dimensional implementations if you can! ;)


Sign In
Create Account


Back to top









