I have been trying for about 6 hours to get this program to work. It's a C++ program for for loops. I am trying to go through a book, but I don't have source code for how this should be. I need to make a program that looks like this below:

+ = are to show open space


*++++++++++**********+**********++++++++++*
**+++++++++*********+++*********+++++++++**
***++++++++********+++++********++++++++***
****+++++++*******+++++++*******+++++++****
*****++++++******+++++++++******++++++*****
******+++++*****+++++++++++*****+++++******
*******++++****+++++++++++++****++++*******
********+++***+++++++++++++++***+++********
*********++**+++++++++++++++++**++*********
**********+*+++++++++++++++++++*+**********


So far I got the code below, but have no clue what to do from here. Any help would be greatly appreciated. Thank You!!!!!

#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
	int i,j;

	for (i=1; i<=10; i++)
	{
		for (j=0; j<i; j++)
			cout << "*" ; 
		cout << endl; 
	}
		for (i=10; i>=1; i--)
	{
		for (j=0; j<i; j++)
			cout << "*" ; 
		cout << endl; 
	}


	cout << "any key..." << endl; 
	_getch();
	return 0;
}

Dani AI

Generated

The shape is fully symmetric; each line can be built from seven contiguous blocks (left stars, outer pluses, outer stars, middle pluses, outer stars, outer pluses, right stars). For a size n = 10 and a line index i (1..n) the counts are simple:

  • left/right stars = i
  • outer pluses = outer stars = (n + 1) - i
  • middle pluses = 2*i - 1

That gives the exact counts needed to reproduce the lines in the original post.

A concise, robust approach is to compute those three numbers per row and print the seven blocks in order. Using the standard library (std::string(count, ch)) avoids many nested loops and makes the intent clear.

#include <iostream>
#include <string>

int main() {
    const int n = 10;
    for (int i = 1; i <= n; ++i) {
        int outer = (n + 1) - i;
        int middle = 2 * i - 1;
        std::cout
            << std::string(i, '*')
            << std::string(outer, '+')
            << std::string(outer, '*')
            << std::string(middle, '+')
            << std::string(outer, '*')
            << std::string(outer, '+')
            << std::string(i, '*')
            << '\n';
    }
    return 0;
}

Notes and quick troubleshooting: change n to vary size; watch off-by-one errors (indices start at 1 here to match the formulas); prefer standard headers and std::cin.get() to pause rather than nonportable conio.h. — the original increasing/decreasing star loops were a good starting point; ’s counter idea points in the right direction; ’s triangle examples show other loop shapes but use nonstandard headers and functions.

Recommended Answers

All 3 Replies

Try looking at the code below..You should be able to finish it with this hint..

#include <iostream>

int main(int argc, char *argv[])
{
    int count_one = 1;
    int count_two = 10;
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < count_one; ++j)
        {
            std::cout << '*';
        }
        for (int j = 0; j < count_two; ++j)
        {
            std::cout << '+';
        }
        ++count_one;
        --count_two;
        std::cout << std::endl;
    }
    return 0;
}

Thank you so much. That helped a ton. Thanks!!!!!!! I hope you have a wonderful day and a great upcoming week. Thanks again!

// a programme that prints triangle of stars
# include<constrea.h>
void main()
{
clrscr();
for(int a=5;a>=1;a--)
{
for(int b=1;b<=a;b++)
{
cout<<" ";
}
for(int c=6;c>=b;c--)
{
cout<<"*";
}
cout<<endl;
}
getche();
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.