c++ loop

Hi

using these variable

char ch = "*";
int max = 10;
int total = 150;

i want this result:

*
 **
  ***
   ****
    *****
     ******
      *******
       ********
        *********
         ********** (maximum 10 ch, then it start to decrese)
         **********
        *********
       ********
      *******
     ******
    *****
   ****
  ***
 **
*
*
 **
  ***
   ****
    *****
     ******
      *******
       ********
        **** (total 150 ch)


can anyone help with the codes plzzzzzzz
Here it is:

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
33
34
35
36
37
#include <iostream>
using namespace std;

int main()
{
    cout << "*                                                            " << endl;
    cout << " **                                                          " << endl;
    cout << "  ***                                                        " << endl;
    cout << "   ****                                                      " << endl;
    cout << "    *****                                                    " << endl;
    cout << "     ******                                                  " << endl;
     cout << "      *******                                                " << endl;
    cout << "	    ********                                              " << endl;
    cout << "        *********                                            " << endl;
    cout << "         ********** (maximum 10 ch, then it start to decrese)" << endl;
    cout << "         **********                                          " << endl;
    cout << "        *********                                            " << endl;
    cout << "       ********                                              " << endl;
    cout << "      *******                                                " << endl;
    cout << "     ******                                                  " << endl;
    cout << "    *****                                                    " << endl;
    cout << "   ****                                                      " << endl;
    cout << "  ***                                                        " << endl;
    cout << " **                                                          " << endl;
    cout << "*                                                            " << endl;
    cout << "*                                                            " << endl;
    cout << " **                                                          " << endl;
    cout << "  ***                                                        " << endl;
    cout << "   ****                                                      " << endl;
    cout << "    *****                                                    " << endl;
    cout << "     ******                                                  " << endl;
    cout << "      *******                                                " << endl;
    cout << "       ********                                              " << endl;
    cout << "        **** (total 150 ch)                                  " << endl;

	return 0;
}


If you want help with an algorithm to do this automatically, show us where you've gotten stuck and we'll point you in the right direction.
Last edited on
Wow , stewbond , i didnt knew that we can use cout to do that .

Can i get any more info about it anywhere. I didnt find anything in the tutorials.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
void main()
{
clrscr();
int i,c,j;
for(c=0,i=0;i<=10;i++)
{
cout<<setw(c);
for(j=0;j<=i;j++)
cout<<"*";
cout<<endl;
c++;
}

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

}
cout<<setw(13)<<"****";
getch();
}
Last edited on
void main()
This has never ever been correct C++ and if your compiler accepts it, throw it away and get a C++ compiler.

<conio.h> is non-standard, <iostream.h> and <iomanip.h> are pre-standard and over a decade old. A C++ compiler that actually is a C++ compiler (that is, conforms to the C++ language definition) will have no idea what they are.


Seriously, where does all this 15 year old bad code keep coming from?
Last edited on
Moschops: I can find old books with a lot of things in it, that pre-date the 98 standard of c++. If you don't know the differences you might have problems. I can get turbo c++ 2.0 for free out there so I have been told. Open Watcom currently still has these libraries and they are all c++ compilers which haven't conformed to anything past 2k. As for the C++ 11 standard none of the compilers have completely supported it at last check.
@Stewbond >> Seriously ??? :o

@Zu007 >> tnx a lot mate. I use DevC++ & i had to change ur code a little to work

here's what i did:
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
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
     int i,c,j;
     for(c=0,i=0;i<=10;i++)
     {
         cout<<setw(c);
         for(j=0;j<=i;j++)
         cout<<"*";
         cout<<endl;
         c++;
     }

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

     system("pause");
}


BUT actually u used 3 for loop & an extra cout at the end to show tht exact result (but tht result was an example only)

first the program shud ask the total number of charecter...lets say it stor it in int total variable

in tht example i used 150 as total thts why it showed exactly 150 * (if u count)

but lets say total 10
then it will show
*
**
***
****

see it doesnt need to decreas at all, coz it didnt reach max number 10 (at last line char number was 4 only)
it will be decreasing after it makes a line with max number, say 10
& again when it becomes 1 & still not reached given total...so increase again, until total number of * printed


can u plz help with this?
you want to show like
*
**
***
****
or
*
**
***
****
or
****
***
**
*

plz tell output first.
if you want like your 1st post result it's difficalt.Only expert sloved this.
If I set the width of my star to 10 for example:
 
int width = 10;


the part half of the shape would look like to me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(nCntWidth = 0; nCntWidth < width; nCntWidth++)
{
       
       if(nCntWidth != 0)
       { 
            for(int nSpcCnt = 0; nSpcCnt < nCntWidth; nSpcCnt++)
            {
               cout << " ";
            }
       }
       for(int nStarCnt = 0, nStarCnt < nCntWidth; nStarCnt++)
       {
            cout << "*";
       }
      cout << endl;
}


I hope that points you in a direction.
Last edited on
this is what i want...

there will be variable, lets say int n=10;

another int total=150;

so it will print total of 150 charecter...lets say the charecter is *
first line it prints only 1 *
2nd line 2 *
& so on...

but as the n=10 given....it cannot print more than 10 * in a line. so it stop increasing at line 10.
from 11th line, it will be decresing...& again when it will only 1 star...it start increasing again
but whatever the situation, when it reach the total=150 number...it will stop printing & pause. (doesnt matter tht time it was increasing or decreasing or how many line it took to print all 150 *)


something like this
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
*
 **
  ***
   ****
    *****
     ******
      *******
       ********
        *********
         ********** (maximum 10 ch, then it start to decrese)
         **********
        *********
       ********
      *******
     ******
    *****
   ****
  ***
 **
*
*
 **
  ***
   ****
    *****
     ******
      *******
       ********
        **** (total 150 ch)
Stewbond's response was brilliant.

The number of people asking for code handouts on this site is ridiculous at the minute. Knowing the answer is a million miles away from knowing how to get the answer.
Use vectors. Push a * onto the end and a " " onto the beginning. If you don't know how to do that, go read about it on this site.
sure i think this will work well .




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
33
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
 
  	int M_m = 10;
  	string str = "";
  	
  
		 for (int i=0; i<M_m; i++)
		 {
		  	 str.assign("",i);
 		 cout << str.append(i,'*')<< '\n';
		 };
		 
		 for (int i=M_m; i>0; i--)
		 {
		  	 str.assign("",i);
 		 cout << str.append(i,'*')<< '\n';
		 };
		 for (int i=0; i<M_m+1; i++)
		 {
		  	 str.assign("",i);
 		 cout << str.append(i,'*')<< '\n';
		 };
		 
		 

     cin.ignore(10,'/n');
	 return 0;
}
Last edited on
Topic archived. No new replies allowed.