Nested Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • durini ali
    New Member
    • Sep 2006
    • 1

    Nested Loop

    Hi everyone,

    I'm having problem to do the programming about this question for my assignment.

    Question:

    Write a program that uses for statement to print the following patterns separately, one below the other. Display the pattern on the screen, as well as on the output file. If you input 6, you should display all four patterns as below:

    *
    **
    ***
    ****
    *****
    ******

    ******
    *****
    ****
    ***
    **
    *

    ******
    *****
    ****
    ***
    **
    *

    *
    **
    ***
    ****
    *****
    ******
  • Rakesh Mutharaju
    New Member
    • Sep 2006
    • 14

    #2
    Did you give a try?

    that is a simrple task..

    steps:

    int Line_number =1,maxnumber_of stars=6,StarsPe rLine=1;

    for(;Line_numbe r <= maxnumber_ofsta rs;Line_number+ +)
    {
    for (;StarsPerLine <= Line_number; StarsPerLine++)
    {
    printf("*");
    }
    printf("\n");
    }

    for the next sequence.. change the first for loop
    for(Line_number = 6; Line_number >0; Line_number--)
    and the rest same.. will do..

    similarly try the next part..

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by Rakesh Mutharaju
      Did you give a try?

      that is a simrple task..

      steps:

      int Line_number =1,maxnumber_of stars=6,StarsPe rLine=1;

      for(;Line_numbe r <= maxnumber_ofsta rs;Line_number+ +)
      {
      for (;StarsPerLine <= Line_number; StarsPerLine++)
      {
      printf("*");
      }
      printf("\n");
      }

      for the next sequence.. change the first for loop
      for(Line_number = 6; Line_number >0; Line_number--)
      and the rest same.. will do..

      similarly try the next part..
      Did you try your code?

      it outputs

      *
      *
      *
      *
      *
      *

      It works with this minor modification

      Code:
      #include "stdio.h"
      
      
      int main(int argc, char* argv[])
      {
      	int Line_number,maxnumber_ofstars=6,StarsPerLine;
      
      	for(Line_number=1;Line_number <= maxnumber_ofstars;Line_number++)
      	{
      		for(StarsPerLine=1;StarsPerLine <= Line_number; StarsPerLine++)
      		{
      			printf("*");
      		}
      		printf("\n");
      	}
      
      	return 0;
      }
      And the moral of the tale is the best place to initialise a loop variable for a for loop is in the initialiser expression.

      Comment

      • suresh_punniyakkodi
        New Member
        • Aug 2006
        • 20

        #4
        Hi,
        Please try this,
        #include<stdio. h>
        #include<conio. h>
        void main()
        {

        Comment

        • suresh_punniyakkodi
          New Member
          • Aug 2006
          • 20

          #5
          Sorry, i am mistakely pressed enter key, Please follow this
          Please try this,

          #include<stdio. h>
          #include<conio. h>
          int i,j;
          void first(int n)
          {
          for(i=1;i<=6;i+ +)
          {
          for(j=1;j<=i;j+ +)
          printf("*");
          printf("\n");
          }
          }
          void second(int n)
          {
          for(i=n;i>=1;i--)
          {
          for(j=1;j<=i;j+ +)
          printf("*");
          printf("\n");
          }
          }
          void main()
          {
          int n;
          clrscr();
          printf("Enter N Value : ");
          scanf("%d",&n);
          first(n);
          second(n);
          second(n);
          first(n);
          getch();
          }


          Regards,
          Suresh

          Comment

          • Rakesh Mutharaju
            New Member
            • Sep 2006
            • 14

            #6
            Originally posted by Banfa
            Did you try your code?

            it outputs

            *
            *
            *
            *
            *
            *

            It works with this minor modification

            Code:
            #include "stdio.h"
            
            
            int main(int argc, char* argv[])
            {
            	int Line_number,maxnumber_ofstars=6,StarsPerLine;
            
            	for(Line_number=1;Line_number <= maxnumber_ofstars;Line_number++)
            	{
            		for(StarsPerLine=1;StarsPerLine <= Line_number; StarsPerLine++)
            		{
            			printf("*");
            		}
            		printf("\n");
            	}
            
            	return 0;
            }
            And the moral of the tale is the best place to initialise a loop variable for a for loop is in the initialiser expression.


            sorry. But the initialization need not be in place [
            for(initializat ion;condition;i nc/decre) ] if the variable is not altered or need to be reset.



            #include<stdio. h>
            #include<conio. h>
            main()
            {
            int Line_number =1,maxnumber_of stars=6,StarsPe rLine=1;
            clrscr();
            for(;Line_numbe r <= maxnumber_ofsta rs;Line_number+ +)
            {
            for (;StarsPerLine <= Line_number; StarsPerLine++)
            {
            printf("*");
            }
            StarsPerLine =1;
            printf("\n");
            }
            }

            will also work.. The concept here is StarsPerLine variable used for INNER FOR LOOP has to be reset to the intial value.

            Comment

            • risby
              New Member
              • Sep 2006
              • 30

              #7
              Originally posted by durini ali
              Write a program that uses for statement to print the following patterns separately, one below the other.
              Please do not write code like this if you work in the nuclear power industry ... (particularly the one line limit swap with which I am inordinately pleased)

              Code:
              #include <stdio.h>
              #include <stdlib.h>
              
              #define MAX 100
              
              int main(int argc, char ** argv)
              {
              	int r = 0;
              	int i;
              	int batch;
              	int last = (argc > 1) ? atoi(argv[1]) : 6;
              	int first=1;
              	int delta=1;
              	char star[MAX+1];
              
              /* initialize print data */
              	for (i=0; i<MAX; i++)
              	{
              		star[i] = '*';
              	}
              	star[MAX]='\0';
              
              /* four batches of lines of stars */
              	for (batch = 0; batch < 4; batch++)
              	{
              	/* batch of lines of increasing or decreasing width of stars */
              		for (i = first; i != last + delta; i += delta)
              		{
              			printf("%*.*s\n", i, i, star);
              		}
              		printf("\n");
              
              	/* if necessary juggle parameters */
              		if (batch != 1)
              		{
              			first = last - first + (last = first);
              			delta = -delta;
              		}
              	}
              
              	return r;
              }

              Comment

              Working...