Page 32, Exercise 4
a. Counts
void transpose(int a[][MAX_SIZE])
{
int i, j;
int temp;
int count = 0;
for (i = 0; i & lt; MAX_SIZE-1; i++) {
count ++; /*for i loop */
for (j = i+1; j < MAX_SIZE; j++) {
count ++; /*for j loop */
SWAP(a[i][j],a[j][i],temp); count+=3; /*for swap */
}
count++; /* last timne of j */
}
count++; /* last time of i */
printf("Transpose Count: %d\n", count);
}
b. Simplified Counts
void transpose(int a[][MAX_SIZE])
{
int i, j;
int temp;
int count = 0;
for (i = 0; i < MAX_SIZE-1; i++) {
for (j = i+1; j < MAX_SIZE; j++) {
count ++; /*for j loop */
SWAP(a[i][j],a[j][i],temp); count+=3; /*for swap */
}
count+=2;
}
count++; /* last time of i */
printf("Transpose Count: %d\n", count);
}
c. Final Count for 5x5 matrix : 49
d. Step Count Table
| Statement | s/e | f | Total Steps |
void transpose(int a[][MAX_SIZE])
{
int i, j;
int temp;
for (i = 0; i =< MAX_SIZE-1; i++)
for (j = i+1; j < MAX_SIZE; j++)
SWAP(a[i][j],a[j][i],temp);
}
|
0 0 0 0 1 1 3 0 |
1 1 1 1 MAX_S MAX_S(MAX_S-1)+MAX_S MAX_S(MAX_S-1) 1 |
0 0 0 0 MAX_S MAX_S•MAX_S-1 + MAX_S 3(MAX_S(MAX_S-1)) 0 |
| Total : 4MAX_SIZE•MAX_S-1 + 2MAX_SIZE | |||