473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

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

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

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

*
**
***
****
*****
******
Sep 6 '06 #1
6 4895
Did you give a try?

that is a simrple task..

steps:

int Line_number =1,maxnumber_ofstars=6,StarsPerLine=1;

for(;Line_number <= maxnumber_ofstars;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..
Sep 6 '06 #2
Banfa
9,065 Expert Mod 8TB
Did you give a try?

that is a simrple task..

steps:

int Line_number =1,maxnumber_ofstars=6,StarsPerLine=1;

for(;Line_number <= maxnumber_ofstars;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

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     int Line_number,maxnumber_ofstars=6,StarsPerLine;
  7.  
  8.     for(Line_number=1;Line_number <= maxnumber_ofstars;Line_number++)
  9.     {
  10.         for(StarsPerLine=1;StarsPerLine <= Line_number; StarsPerLine++)
  11.         {
  12.             printf("*");
  13.         }
  14.         printf("\n");
  15.     }
  16.  
  17.     return 0;
  18. }
  19.  
And the moral of the tale is the best place to initialise a loop variable for a for loop is in the initialiser expression.
Sep 6 '06 #3
Hi,
Please try this,
#include<stdio.h>
#include<conio.h>
void main()
{
Sep 6 '06 #4
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
Sep 6 '06 #5
Did you try your code?

it outputs

*
*
*
*
*
*

It works with this minor modification

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     int Line_number,maxnumber_ofstars=6,StarsPerLine;
  7.  
  8.     for(Line_number=1;Line_number <= maxnumber_ofstars;Line_number++)
  9.     {
  10.         for(StarsPerLine=1;StarsPerLine <= Line_number; StarsPerLine++)
  11.         {
  12.             printf("*");
  13.         }
  14.         printf("\n");
  15.     }
  16.  
  17.     return 0;
  18. }
  19.  
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(initialization;condition;inc/decre) ] if the variable is not altered or need to be reset.



#include<stdio.h>
#include<conio.h>
main()
{
int Line_number =1,maxnumber_ofstars=6,StarsPerLine=1;
clrscr();
for(;Line_number <= maxnumber_ofstars;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.
Sep 7 '06 #6
risby
30
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)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 100
  5.  
  6. int main(int argc, char ** argv)
  7. {
  8.     int r = 0;
  9.     int i;
  10.     int batch;
  11.     int last = (argc > 1) ? atoi(argv[1]) : 6;
  12.     int first=1;
  13.     int delta=1;
  14.     char star[MAX+1];
  15.  
  16. /* initialize print data */
  17.     for (i=0; i<MAX; i++)
  18.     {
  19.         star[i] = '*';
  20.     }
  21.     star[MAX]='\0';
  22.  
  23. /* four batches of lines of stars */
  24.     for (batch = 0; batch < 4; batch++)
  25.     {
  26.     /* batch of lines of increasing or decreasing width of stars */
  27.         for (i = first; i != last + delta; i += delta)
  28.         {
  29.             printf("%*.*s\n", i, i, star);
  30.         }
  31.         printf("\n");
  32.  
  33.     /* if necessary juggle parameters */
  34.         if (batch != 1)
  35.         {
  36.             first = last - first + (last = first);
  37.             delta = -delta;
  38.         }
  39.     }
  40.  
  41.     return r;
  42. }
  43.  
Sep 7 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: mark | last post by:
(not sure if this is the correct group) My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.