Functions, arrays and matrices

From Applied Science

After learning how to use functions, arrays and matrices, it's time to learn how to use arrays and matrices as function's parameters. A matrix with 10 columns and 10 rows can store up to 100 values, but a function doesn't make 100 calls when we use that matrix in that function. We can have a function with 100 parameters, but if they are all part of a matrix it's much simpler to just use the matrix and then reference to it by a single name.

To explain why matrices and arrays have the first pair of brackets left empty, an explanation about how arrays are stored in memory and about pointers is required. For the moment we can learn how to use arrays and functions without having to worry about that.

Errors of logic:

  • The same with arrays and matrices when used without functions;
  • Errors related to the operations with matrices and arrays can often cause your program to crash;
  • It's not possible to return a matrix or array, that is going to be explained when we study pointers.


What comes below requires knowledge about arrays, matrices and functions


  • Function that counts how many times a number occurs in an array

/* receives the array, the max number of elements to compare and the value x to be compared against all array's values */

int count (int array[], int ind_max, int x) {

int i, found = 0;

for (i = 0; i < ind_max; i++)
if (array[i] == x)
found++;

return found;

}

Pretty simple. It's just one comparison operation and a counter.


  • Functions that counts how many lines of a matrix have all elements with the same value

include <stdio.h>

/* Counts how many matri'x rows have the same number in all columns. If the element of one row is different from the element of the same row and in the next column, stop. If the loop wasn't broken, then all elements of that row are the same, then count one row found */

int count (int matrix[][max], int imax, jmax) {

int i, row_count = 0;

for (i = 0; i < imax; i++) {

for (j = 0; j < jmax - 1; j++) if (matrix[i][j] != matrix[i][j + 1]) break;

if (j == jmax) row_count++;

}
return row_count;
}

The algorithm is pretty simple, but be careful with the counter! Pay a lot of attention to the indexes' counters! As an exercise, try to modify it to the following problems: count how many matrix's rows are equal to a number x; count columns rather than rows.


  • Function that does the product of two matrices

/* Receives the matrix a, of n rows and max columns and the matrix b of n rows and m colums. Multiply a and b, storing the result in matrix c, of n rows and mm columns. If m columns doesn't match nn rows, return 0 without multiplying the matrices */
int multmat (int a[][max], int b[][max], int c[][max], int n, int m, int nn, int mm) {
int i, j, k = 0;
if (m != nn) return 0;

for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {

c[i][j] = 0;
for (k = 0; k < mm; k++) c[i][j] += a[i][k] * b[k][j];

}

}

The algorithm obeys to the rule of matrix multiplication. It's a bit hard to track it because there are three loops in cascade. In this case the performance has been traded for a sequence of operations that clearly matches the way matrix multiplication would be done on paper.

Return 0 is used to tell the program that the multiplication cannot be done. This is a classical example of a function which includes some check to prevent some incorrect operation to be done if the input is invalid.