Algorithm Analysis
We draw a rectangular frame of as follows:
In the first row, we output asterisks;
We output rows. In each row, we output an asterisk, spaces, and another asterisk, but only if ;
In the last -th row, we output asterisks, but only if .
Algorithm Implementation
Read the input values and .
scanf("%d %d", &n, &m);
Output the first row – asterisks.
for (i = 0; i < m; i++) printf("*"); printf("\n");
Output rows. In each row, output an asterisk, spaces, and another asterisk, but only if .
for (i = 0; i < n - 2; i++) { printf("*"); for (j = 0; j < m - 2; j++) printf(" "); if (m > 1) printf("*"); printf("\n"); }
Output the last row – asterisks. Output it only if .
if (n > 1) { for (i = 0; i < m; i++) printf("*"); printf("\n"); }