ER> I'm using a book called "C by Examples" and on lesson 14
ER> it ask me to write a program that would print this by
ER> using nested loop:
ER> A
ER> AB
ER> ABC
ER> ABCD
ER> ABCDE
ER> This is what I have:
ER> void main() {
ER> int count, Ascii;
ER> for(count = 1; count <= 5; count++) {
ER> for(Ascii = 65; Ascii ........
ER> This is where I got stuck. I don't know what the test
ER> expression should be the second one. Also, I really don't
ER> know if I should use the ++ or --.
There are several ways to do this.
Here are two simple methods. You will notice that neither
will depend on using the numeric constants for the standard
ASCII character set. This allows this code to be valid on a
wider range of platforms, meaning it is more portable.
int main(void)
{
int iterate, iterate_2;
char output[6] = "ABCDE";
for(iterate = 1; iterate < 6; iterate++)
{
char output[6] = "ABCDE";
output[iterate] = (char)'\0';
puts(output);
}
/* note that the char array 'output' in this code block is not
the same as the one created above. The first one is hidden
by the second one, which is visible to, and has scope
within, only this code block.
*/
for(iterate = 0; iterate < 5; iterate++)
{
for(iterate_2 = 0; iterate_2 <= iterate; iterate_2++)
putchar(output[iterate_2]);
putchar('\n');
}
return 0;
}
> ] Within every pearl there lies a tiny grain of truth.........
---
---------------
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
|