Commented exercises

From Applied Science
  • The two algorithms below attempt to calculate xn. However, both are wrong, explain the error in each one:
for (i = 1; i <= n; i++) x = x * i; It's successively multiplying by an increasing counter. For x = 4, we have 4 * 1 * 2 * ... * n
for (i = 0; i < n; i++) x = x * x; It's squaring the number over and over n times. For x = 4, we have 42, then 162, then 642, ...

Obs.: it might be silly, but that happens when the assignment operation isn't understood.


  • Write an algorithm that fills the following multiplication table (you don't have to worry about text alignment or declaration of variables)
1 2 3 ...
2 4 6 ...
3 6 9 ...
... ... ... ...

for (i = 1; i <= n; i++)

for (j = 1; j <= n; j++) value = i * j;

Obs.: it might be silly, but this exercise is the foundation for plotting the graph of functions on the computer's screen.


  • Write a function with this prototype int largest (int m[ ][max], int n) that returns the largest value of the row n of the matrix m. Consider #define max 10
int largest (int m[][max], int n) {

int i, largest_row = m[n][0];

for (i = 1; i < max; i++)

if (largest_row < m[n][i])

largest_row = m[n][i];

return largest_row;

}

The difference between asking to write a function that does something and to give the function's prototype is that, when a prototype is given, the function cannot have more or less parameters than those that are already there. We aren't asked to write a program, our only concern is with what the function is supposed to do.

Could 'largest_row' have been initialized with 0? Yes if the exercise did specify that the matrix doesn't contain negative values for example. Otherwise, we have to assume that some row's element is the largest, until an even larger one, in the same row, is found. The comparison is going to be made against all other elements, therefore it doesn't matter if the var is initialized with the first, last or any other element in between.

Notice that we used just one loop, not two, because the exercise only wants to know about one matrix's row, not all rows. The variable 'n' keeps the row constant. Could the loop begun at 0? It could, but as we are taking the first row's element as the largest one from the beginning, there is no need to compare it against itself.

Use the previous function, even if you didn't make it, to write a function with this prototype int equal (int m[ ][max], int n) that returns 1 if the largest value of all rows are equal or 0 if they are not

int equal (int m[][max], int n) {

int largest_value, i;
largest_value = largest(m, 0);

for (i = 1; i < n; i++) if (largest_value != largest(m, i)) return 0;

return 1;

}

We don't have to know how the previous function works to solve this problem. Even if we don't know the previous function's algorithm, we know its parameters and what result it outputs, which is enough to know how to call it in this problem.

The basic idea is rather simple, just stop the search in the first value that is different from the previous one, in which case the function returns 0. If the iteration isn't stopped, it scans all rows, then we know that the greatest value of each row was equal to each other and that the function should return 1.

What is left to discuss about the algorithm relates to the programming part. The counter 'i' begins with 1, because a variable is storing the row zero's largest value. The var 'largest_value' wouldn't be required if we'd place 'largest(m, 0)' directly in the 'if' conditional. Notice that both functions require a matrix as one of its parameters. If the function 'equal' doesn't have a matrix as one of its parameters, how the function 'largest' is going to work?