question that is challenging

wanna get output for the following structure using c and c++ language

*
***
*****
*******
*****
***
*
Last edited on
Consider using std::cout for C++. It shouldn't be too hard :)
http://www.cplusplus.com/reference/iostream/cout/
Last edited on
You want this?

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

Or this?

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

What have you done so far?
Can we see your code?
#include <iostream>
using namespace std;
//*
//***
//*****
//*******
//*****
//***
//*
/*
The pattern is 1,3,5,7,5,3,1
i.e. increases to 7 and decreases to 1
*/
void printastriek(int count)
{
int i;
for (i=1;i<=count;i++)
cout << '*'; //For C just replace this line with putchar('*');
cout <<'\n'; //putchar('\n')
return;
}

int main()
{
const int max=7;
int i=1,sign = 1;
while (1)
{
printastriek(i);
i = i+2*sign;
if (i>max) sign = -1;
if (i<1) break;
}
return 0;
}

Last edited on
Basically there are 2 characters needed the * and space.
You could print so many spaces, then print so many *.
cout << '___*';
cout << ' __***';
cout << ' _*****';
cout << ' *******';
cout << ' _*****';
cout << ' __***';
cout << ' ___*';
but that would make 100 * a very long code. Rule of thumb, if the code is less than the amount you need, then use code, if code is longer use the simple approach.

or you could run the loop, which says how many spaces to print before the * starts, then how many * to actually print. With this you can change the max, and it will easily make more.

int ast = 1; // start with 1 on top
int max = 7; // max amount of * in a line
int space = max/2 - 1; // start with 3 for 1st line (max sets this value)
bool dir = true; // adding more *

// adding 2 * per line will make lines tall = * wide
for(int i=0; i<max; i++) // max (7) lines tall
{
for(int j=0; j<space; j++) // for the amount of spaces
cout << ' '; // print them
for(int k=0; k<ast; k++) // for each *
cout << '*'; // print them

cout << '\n'; // done with that line
if(ast == max)
dir = false; // set to reverse

if (dir== true) // check direction (add or sub)
{
space-=1; // 1 less space
ast+=2; // 2 more *
}
else // start reducing amount
{
space+=1; // 1 more space
ast-=2; // 2 less *
}

}
Another homework.
Topic archived. No new replies allowed.