Solved

Code For * Triangle

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

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

0
Comment
Question by:sinha_anshul26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Jose Parrot
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
 
LVL 18

Accepted Solution

by:
Jose Parrot 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:Jose Parrot
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

635 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