Page 32, Exercise 4: PrintMatrix
a. Counts
void Printmatrix(int matrix[][MAX_SIZE], int rows, int cols)
{
int i,j;
int count = 0;
for (i = 0; i<rows; i++)
{
count ++; /*for i loop */
for (j = 0; j<cols; j++) {
printf("%5d",matrix[i][j]);
count++; /*for j loop */
}
count++; /* last time of j */
printf("\n");
}
count++; /* last time of i */
printf("Print Count: %d\n", count);
}
b. Simplified Counts
void Printmatrix(int matrix[][MAX_SIZE], int rows, int cols)
{
int i,j;
int count = 0;
for (i = 0; i<rows; i++)
{
count ++; /*for i loop */
for (j = 0; j<cols; j++) {
printf("%5d",matrix[i][j]);
count++; /*for j loop */
}
count++; /* last time of j */
printf("\n");
}
count++; /* last time of i */
printf("Print Count: %d\n", count);
}
c. Final Count for 5x5 matrix : 36
d. Step Count Table
| Statement | s/e | f | Total Steps |
void Printmatrix(int matrix[][MAX_SIZE], int rows, int cols) |
0 0 0 1 0 1 0 0 0 |
0 0 01 n+1 0 n+1 0 0 0 |
0 0 0 n+1 0 n+1 0 0 0 |
| Total | 2n+2 | ||