Bill Cunningham said:
#include <stdio.h>
int
main (void)
{
int i;
for (i = 0; i < 7; i++)
{
printf ("%i\n", i);
}
}
I indented this program using indent. It looks more readable to
others than my usual indentaion.
It'll do. Whatever options you used to run indent, keep using them.
All this does is print vertically the number 0-7. I have no idea
how to even start working with the line numbers to obtain
exponenials.
OK, that's fine. Let's recap what you know:
0. A C program consists of a sequence of statements which are executed
one after the other.
A;
B;
C;
will run first A, then B, then C. Blocks of such sequences, wrapped
in {}, can usually each be considered equivalent to a single
statement.
1. You can print the value of an int variable named i with
printf("%i", i);
2. You can print a space with
printf(" ");
3. You can print a newline with
printf("\n");
4. You can do some sequence of things once for each value of i between
0 and 7 inclusive with
for (i = 0; i < 7; i++) {
E;
F;
G;
}
Now, the new bits:
5. You can calculate a new value by writing an arithmetic expression
involving variables, so for example to get the value of i plus 3,
i + 3
or, to get the value of i multiplied by i
i * i
6. You don't have to store such a value anywhere explicitly - you can
use it almost anywhere you could use the name of a variable, and then
instead of the value of the variable being used you'll use the new
calculated value. For example, you can print the value of an i plus 3
with
printf("%i", i+3);
(compare this with (1) above). You _can_ store a calculated value if
you want but we'll worry about that later.
So: since the variable i holds the line number that the question asks
for, how could you calculate the square of the line number? How could
you print that new value? What about the cube?
If you think you can answer the 3 questions above, try modifying the
program you posted here to complete the answer to q4a.
mlp