Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Dimensions Of A Matrix


  • Please log in to reply
4 replies to this topic

#1 DarkLordCthulhu


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 422 posts
  • Location:The bash shell
  • Programming Language:C, JavaScript, Bash, Others
  • Learning:Ruby, Others

Posted 19 April 2012 - 01:01 PM

I'm writing a C program to convert a matrix into row echelon form. I have three functions for the row operations:

// Scalar multiplication
void multiply( int **matrix, int row, int scalar ){

// Row addition
void add( int **matrix, int row1, int row2, int scalar ){

// Row exchange
void exchange( int **matrix, int row1, int row2 ){

The problem is I need to get the dimensions of the matrix from within the function, and I've forgotten how to do it. How do you detect the end of an int array in C without having to resort to global variables?
  • 0
Programming is a journey, not a destination.

#2 papabear


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 19 April 2012 - 02:09 PM

you can just store the dimensions of the matrix in a variable for reference purposes, you must stick to global variables :)
and to get the size of the array you can use this

sizeof(myArray) / sizeof(int)

to get the last element as it returns the size of the array, or just wait for someone to post a function that gets the dimensions of a matrix, but I'll suggest you use the global variable for easy referencing of data
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#3 dargueta


    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 19 April 2012 - 03:15 PM

That'll work for statically declared arrays, not pointers to them.

int blah[5];
int *ptr_blah;

// Works
sizeof(blah) / sizeof(*blah) --> 5
sizeof(blah) / sizeof(int) --> 5

// Won't work:
sizeof(ptr_blah) / sizeof(int) --> 1
sizeof(ptr_blah) / sizeof(*ptr_blah) --> 1
sizeof(*ptr_blah) / sizeof(ptr_blah) --> 1

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'

#4 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 19 April 2012 - 08:14 PM

If you don't want to use global variables, pass the row_count, column_count as the argument to those functions.
  • 0

#5 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 April 2012 - 11:05 PM

Any particular reason you can't use structures?
  • 0

The roots of education are bitter, but the fruit is sweet.

Also tagged with one or more of these keywords: matrix

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download