sinha_anshul26
asked on
Code For * Triangle
what will be the code in c/c++ to generate this sequence--
*
***
*****
*******
*****
***
*
*
***
*****
*******
*****
***
*
homeWork??
printf(" *\n");
printf(" ***\n");
printf(" *****\n");
printf(" *******\n");
printf(" *****\n");
printf(" ***\n");
printf(" *\n");
printf(" ***\n");
printf(" *****\n");
printf(" *******\n");
printf(" *****\n");
printf(" ***\n");
printf(" *\n");
C++ ..
cout<<" * "<<endl;
cout<<" *** "<<endl;
cout<<" ***** "<<endl;
cout<<"*******"<<endl;
cout<<" ***** "<<endl;
cout<<" *** "<<endl;
cout<<" * "<<endl;
cheers;
cout<<" * "<<endl;
cout<<" *** "<<endl;
cout<<" ***** "<<endl;
cout<<"*******"<<endl;
cout<<" ***** "<<endl;
cout<<" *** "<<endl;
cout<<" * "<<endl;
cheers;
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");
}
#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");
}
:o)
#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;
}
#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;
}
// 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
}
// 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
}
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 ..
>>#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 ..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bpmurray wins.
Bows.
I think Jose's is really cool: if that's handed in as homework, the lecturer will give anshul26 the marks he deserves.
I think Jose's is really cool: if that's handed in as homework, the lecturer will give anshul26 the marks he deserves.
ASKER
************************** **********
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.
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.
So Sorry ..
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;
#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;
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.
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.
ASKER
Thanks for the tip. I will keep that in mind for my future doubts and querries.