loop c++ : whats missing in this code?

Feb 16, 2011 at 12:56pm
the problem is :

write a program that would generate the output below :

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

note : use only 1 printf("*") and place this inside a loop.


here is the code :

#include<stdio.h>
#include<conio.h>



main()
{
int x,y,a=6,b=1,z=5;

for(x=b;x<=a;x++)
{
for(y=b;y<=x;y++)
{
if(y<=z)
z--;
printf("*");
}
printf("\n");
}

getch();

}

i am really confuse about this inner loop here of what condition i will put or another loop, the only output of that is :

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

really need your help guys, what hard of this is that i must use only 1 printf("*"); and place this inside a loop. hope you all could help me.. thank you and have a nice day!

Feb 16, 2011 at 2:12pm
You need 1 loop that executes 11 times (since there is 11 lines of output). The first 6 times through the loop, you output that number of stars. The last 5 times through, ... you figure that part out. The number of stars to output is a simple function of the for loop counter.
Feb 16, 2011 at 2:21pm
Hmm your code seems a bit complicated.

When faced with problems like these, I try to figure the logic pattern and then apply the simplest solution I get for it.

So, you need a program that writes a number of *s, increasing it by one every line until it gets to the half way, and then starts decreasing it by one until the end.
How about setting a function that will just count the number of *s that you need to write, and only then print it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void writeSymbols(int numberOfLines)
{
  int currentLine = 0; //we begin at one
  int maxLines = numberOfLines; //and will move to this
  int howManySymbols = 0; //this is how many we have to write each line

  while(currentLine < maxLines) //until we move to the end of the line...
  {
    if(currentLine < maxLines/2) 
    {
      howManySymbols++;  //we increase the number of symbols while our currentLine is lower than half the totalLines
    }
    else
    {
      howManySymbols--; //else, we decrease it
    }

    for(int i = 0; i < howManySymbols; i++) //this is the loop which only does the writing, leaving the logic somewhere else
    {
      printf("*");
    }
    printf("\n");
    currentLine++; //increase so we get out of this loop
  }
}

int _tmain(int argc, _TCHAR* argv[])
{
  writeSymbols(12);
  printf("Press a key to continue...");
  cin.get();
}


When things get complicated, separate the code by it's functions (some code does the logic, some other does the writing for example).
I find this way easier to think and to rework the code if you need to change something later on.
Last edited on Feb 16, 2011 at 2:25pm
Feb 18, 2011 at 1:51am
A simpler way:
1
2
3
4
5
6
7
8
#include "stdio.h"
#include "stdlib.h"
int main(){
	int i;
	for(i=0;i<11;i++)
		printf("%.*s\n",6-abs(i-5),"******");// the first argument is the maximum field width
	getchar();
}
Feb 18, 2011 at 9:55am
Haha that is very nice rocketboy!
Topic archived. No new replies allowed.