Solved

Code For * Triangle

Posted on 2006-06-23
16
333 Views
Last Modified: 2010-05-19
what will be the code in c/c++ to generate this sequence--

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

0
Comment
Question by:sinha_anshul26
  • 6
  • 3
  • 3
  • +3
16 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 16968869
homeWork??
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16968992
printf("                                    *\n");
printf("                                  ***\n");
printf("                                *****\n");
printf("                              *******\n");
printf("                                *****\n");
printf("                                  ***\n");
printf("                                    *\n");
0
 
LVL 9

Expert Comment

by:DrAske
ID: 16969032
C++ ..
      cout<<"   *   "<<endl;
      cout<<"  ***  "<<endl;
      cout<<" ***** "<<endl;
      cout<<"*******"<<endl;
      cout<<" ***** "<<endl;
      cout<<"  ***  "<<endl;
      cout<<"   *   "<<endl;

cheers;
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16969142
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
 
LVL 9

Expert Comment

by:DrAske
ID: 16969171
:o)
0
 

Expert Comment

by:d343seven
ID: 16970083
#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
 
LVL 18

Expert Comment

by:JoseParrot
ID: 16970427
// 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
 
LVL 9

Expert Comment

by:DrAske
ID: 16970606
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Accepted Solution

by:
JoseParrot earned 500 total points
ID: 16970804
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
 
LVL 18

Expert Comment

by:JoseParrot
ID: 16970829
bpmurray wins.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16970895
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
 

Author Comment

by:sinha_anshul26
ID: 16971107
************************************
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
 
LVL 9

Expert Comment

by:DrAske
ID: 16971138
So Sorry ..
0
 
LVL 9

Expert Comment

by:DrAske
ID: 16971211
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
 
LVL 15

Expert Comment

by:bpmurray
ID: 16971221
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
 

Author Comment

by:sinha_anshul26
ID: 16974917
Thanks for the tip. I will keep that in mind for my future doubts and querries.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now