• 08-08-2002
JohnMayer
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; }```
• 08-08-2002
Hammer
I'd say you need to have another go ;)

Look at this part of the question:
Quote:

... so that the FOR loop initializes 'i', tests 'i', and increments 'i'.
This says to me that you need a for statement that looks like this
Code:

`for (i = 1; i <= n; i++) {`
... and the appropriate code moved around to make this work.
• 08-08-2002
Prelude
>for (i = 1; i <= n; odd+= 2) {
Simple enough, look into the usage of the comma operator and throw the increment for i into the loop.

-Prelude
• 08-09-2002
JohnMayer
would this be correct? :
Code:

```/* SEC6.3 EX7      */ /* DATE: 08-09-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; i++, odd+= 2) {                 printf("%10d%10d\n", i, square);                 square += odd;         }         return 0; }```
• 08-10-2002
Unregistered
/* DATE: 08-09-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; i++, odd+= 2) {
printf("%10d%10d\n", i, square);
square += odd;
}

return 0;
}

+++++++++++++++++++++++++++++++++++++++
What did you get (answer)when you typed the program in like this???

Was is correct?? Let us know.

Mr. C.
• 08-11-2002
Unregistered
I got the right output... but I got the correct output when using the code that I thought was previously correct... so I'm hoping one of you guys will know the answer to this C prog Q!:)
• 08-11-2002
JohnMayer
...btw, forgot to sign in (the unregistered response before this one is mine's~!:)
• 08-11-2002
Hammer
Quote:

Originally posted by Unregistered
I got the right output... but I got the correct output when using the code that I thought was previously correct... so I'm hoping one of you guys will know the answer to this C prog Q!:)
So, in summary we started with this:
Code:

`for (i = 1; i <= n; odd+= 2)`
and the exercise was change it so....
Quote:

the FOR loop initializes 'i', tests 'i', and increments 'i'
`for (i = 1; i <= n; i++, odd+= 2) {`