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!
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.
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?
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.
#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();
}