Jump to content

Matrix

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In C++ you can use a vector of vectors.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

WingedPanther said:

In C++ you can use a vector of vectors.

I am learning C. Can you show me an example? Thanks

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
With C, you'll have to use a pointer to an array of ints.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
TWO-DIMENSIONAL ARRAYS
Chapter 23: Two-Dimensional (and Multidimensional) Arrays
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
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
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Thanks

#11
Mathematix

Mathematix

    Programmer

  • Members
  • PipPipPipPip
  • 112 posts
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:

#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! ;)