Code For * Triangle

what will be the code in c/c++ to generate this sequence--

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

sinha_anshul26Asked:
Who is Participating?
 
Jose ParrotConnect With a Mentor Graphics ExpertCommented:
Or, to be concise:

// printStarsTrick.cpp : print diamond with asteriscs
//

#include "stdafx.h"
#include <stdio.h>

void printStarsTrick(int n)
{
      int i,p1,p2;
      char c[200]; // arbitrary, max 199 '*'s

      for (i=0; i<n; i++) c[i]=' '; c[i]='\0';
      for (p1=p2=n/2;p1>0;c[p1--]=c[p2++]='*',printf("%s\n",c));
      c[p1++]=c[p2--]=' ';
      for (;p2>n/2;c[p1++]=c[p2--]=' ',printf("%s\n",c));
}

void main(int argc, char* argv[])
{
      printStarsTrick(45);
      
}
0
 
DrAskeCommented:
homeWork??
0
 
AlexFMCommented:
printf("                                    *\n");
printf("                                  ***\n");
printf("                                *****\n");
printf("                              *******\n");
printf("                                *****\n");
printf("                                  ***\n");
printf("                                    *\n");
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
DrAskeCommented:
C++ ..
      cout<<"   *   "<<endl;
      cout<<"  ***  "<<endl;
      cout<<" ***** "<<endl;
      cout<<"*******"<<endl;
      cout<<" ***** "<<endl;
      cout<<"  ***  "<<endl;
      cout<<"   *   "<<endl;

cheers;
0
 
bpmurrayCommented:
Try this:

#include <stdio.h>

main()
{
   char XXX[] = { 0xBB, 0x90, 0xDF, 0x86, 0x90, 0x8A, 0x8D, 0xDF, 0x90,
                  0x88, 0x91, 0xDF, 0x97, 0x90, 0x92, 0x9A, 0x88, 0x90,
                  0x8D, 0x94, 0xD3, 0xDF, 0x86, 0x90, 0x8A, 0xDF, 0x93,
                  0x9E, 0x85, 0x86, 0xDF, 0x8C, 0x90, 0x9B, 0xDE, 0x00 };
   char *pStr;

   for (pStr=XXX; *pStr; pStr++)
     printf("%c", ~(*pStr));
   printf("\n");
}
0
 
DrAskeCommented:
:o)
0
 
d343sevenCommented:
#include <cmath.h>
#include <iostream>

        int check=0;
        int stars=0;
        int spaces=0;
        int n=0;
        cout << "Enter the number of stars in the center line (odd natural number):";
       
        cin >> n;

        for (int line = 1; line <= n; line++)
        {
            spaces = abs((n+1)/2-line);
            stars = n-2*spaces;
            for (int x = 1; x <= spaces; x++)
                cout << endl;
            for (int y = 1; y <= stars; y++)
                cout << "*";
            cout << endl;        
           
        }
       
       
   
0
 
Jose ParrotGraphics ExpertCommented:
// printDiamond.cpp : print diamond with asteriscs
// traditional programming...

#include <stdio.h>

void printStars(int n)
{
   int i,j,k;

   if (n % 2 != 1)
   {
      n = n-1;
      printf("\nOnly odd numbers allowed. Assuming n = %d\n\n",n);
   }
   for (i=1; i<=n; i+=2)
   {
      printf("\t");      // minimum left margin
      for (j=n-1; j>i; j-=2)
         printf(" ");
      for (k=1; k<=i; k++)
         printf("*");
      printf("\n");
   }
   for (i=n-2; i>0; i-=2)
   {
      printf("\t");      // minimum left margin
      for (j=n-1; j>i; j-=2)
         printf(" ");
      for (k=1; k<=i; k++)
         printf("*");
      printf("\n");
   }
}

void main(int argc, char* argv[])
{
      printStars(7); // maximum depends on console number of collumns
}
0
 
DrAskeCommented:
In d343seven's code ..
>>#include <cmath.h>
must be #include<cmath>

>> for (int x = 1; x <= spaces; x++)
>>          cout << endl;
must be cout<<" ";

and don't forget to add :
using namespace std; after the header files ..
0
 
Jose ParrotGraphics ExpertCommented:
bpmurray wins.
0
 
bpmurrayCommented:
Bows.

I think Jose's is really cool: if that's handed in as homework, the lecturer will give anshul26 the marks he deserves.
0
 
sinha_anshul26Author Commented:
************************************
This comment is NOT for  JOSE PERROT and d343seven
************************************
Hi there,
I am 26 yrs old and a graduate( not in computer science), I am not in school.  and I am not trying to submit my homework.
I was just tring to do some programming and at a point I was stuck, thats why I posted this question here.....but I have got beautifull responce. specially from  bpmurray
I see that  there are many sarcastic, pompus so called "EXPERTS !!".....its highly shameful
Intelligent people are always humble, Its only empty vessels that make sound.







0
 
DrAskeCommented:
So Sorry ..
0
 
DrAskeCommented:
This is another one ..

#include<iostream>
#include<cmath>
using namespace std;


void main()
{
      int y= 30;
      for(int i = 0; i<ceil(y/2); i++)
      {
            for(int x = 0; x<floor(y/2)-i; x++)
                  cout<<" ";
            for(int j = ceil(y/2)-i; j<ceil(y/2)+i+1; j++)
                  cout<<"*";
            cout<<endl;
      }

      for(i=ceil(y/2); i>=0; i--)
      {
            for(int x = floor(y/2)-i; x>0; --x)
                  cout<<" ";
            for(int j = ceil(y/2)-i; j<ceil(y/2)+i+1; j++)
                  cout<<"*";
            cout<<endl;
      }
}

have fun ;o)

regards, Ahmad;
0
 
bpmurrayCommented:
Well Sinha, I'm delighted that you are so interested in computers that you have set yourself a task that pops up here (and all other similar boards) fairly regularly. If you had presented us with your attempt to solve this problem, you would have received a lot of good advice, ranging from resolving any bugs to suggestions on style, etc. The fact that you just asked for the solution to what to us looks like homework means that you get the response such a request deserves, some of which are extraordinarily humorous (I love AlexFM's).

Just to explain why homework requests are frowned upon and, in fact any "gimme a solution" request is treated this way, is because the asker is supposed to be trying to learn how to program. If we give the answer, you will never learn anything. Actually, to learn how to use some interesting mechanisms in C, you should carefully disect Jose's reply. It's actually a very slick and terse way to solve the problem.

I still think this is homework.
0
 
sinha_anshul26Author Commented:
Thanks for the tip. I will keep that in mind for my future doubts and querries.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.