i don't have an answer key or anything for this textbook, but did i get this correct?
#7- Rearrange the square3.c program so that the FOR loop initializes 'i', tests 'i', and increments 'i'. Don't rewrite the program; in particular, don't use any multiplications.
Code:
* SEC6.3 EX7 */
/* DATE: 08-08-02 */
#include <stdio.h>
int main()
{
int i, n, odd, square;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf("%d", &n);
square = 1;
odd = 3;
for (i = 1; i <= n; odd+= 2) {
printf("%10d%10d\n", i, square);
++i;
square += odd;
}
return 0;
}
...and here's my code from the square3.c program:
Code:
/* Prints a table of squares using an old method */
/* DATE: 07-24-02 */
#include <stdio.h>
int main()
{
int i, n, odd, square;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf("%d", &n);
i = 1;
odd = 3;
for (square = 1; i <= n; odd+= 2) {
printf("%10d%10d\n", i, square);
++i;
square += odd;
}
return 0;
}