TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: ELIJAH RICHARD
from: KURT KUZBA
date: 1998-04-21 11:30:00
subject: Nested loop

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)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.