|
-
November 19th, 2010, 01:45 AM
#1
Need Help With This Code !!!!
hi
i write this code but i phase a problem :::
her the code
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,k,l=1,num;
cout<<"Enter the no:"<<endl;
cin>>num;
for(k=num;k>=1;k--)
{ for(i=k;i>=1;i--)
cout<<" ";
if(l<num)
{ for(j=1;j<=l;j++)
cout<<"*"<<" ";
}
l++;
cout<<endl;
}
for(k=num; k>=1; k--)
{
for(j=1; j<=l; j++)
{
cout<<" ";
}
if( l <= num)
{
for(i=k; i>=1; i--)
cout << "*" << " ";
}
l++;
cout<<endl;
}
getch();
return 0;
}
the out Put is
Code:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
the Output Should be
Code:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
where is the problem ....Please Help
Re grads
-
November 19th, 2010, 02:41 AM
#2
Re: Need Help With This Code !!!!
Try this one
Code:
int main()
{
int i,j,k,l=1,num;
cout<<"Enter the no:"<<endl;
cin>>num;
for(k=num;k>=1;k--)
{ for(i=k;i>=1;i--)
cout<<" ";
if(l<num)
{ for(j=1;j<=l;j++)
cout<<"*"<<" ";
}
l++;
if(num==l)
{
}
else
{
cout<<endl;
}
}
int kp=1;
for(k=num; k>=1; k--)
{
kp++;
for(j=1; j<=kp; j++)
{
cout<<" ";
}
if( l >= num)
{
for(i=k; i-1>=1; i--)
cout << "*" << " ";
}
l++;
cout<<endl;
}
getch();
return 0;
}
-
November 19th, 2010, 03:00 PM
#3
Re: Need Help With This Code !!!!
Msm: Unless I missed my guess your code I think it will actually print
Code:
*
* *
* * *
* * * *
* * * *
* * *
* *
*
when what he wants is
Code:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
mot1639: Try this
Code:
int main(int argc, char* argv[])
{
int i,j,k,l=1,num;
cout<<"Enter the no:"<<endl;
cin>>num;
for(k=num;k>=1;k--)
{
for(i=k;i>=1;i--)
cout<<" ";
if(l<num)
{
for(j=1;j<=l;j++)
cout<<"*"<<" ";
}
l++;
if (k > 1) // skip new line on last iteration
cout<<endl;
}
l = 1; // mot1639 forgot to init l which is why there were all blanks
for(k=num; k>=1; k--)
{
if(k!=num) // <--- don't want to add a space on the widest row which
for(j=1; j<=l; j++) // occurs the first time through this loop
cout<<" ";
if( l <= num)
{
for(i=k; i>=1; i--)
cout << "*" << " ";
}
l++;
cout<<endl;
}
getch();
return 0;
}
-
November 20th, 2010, 06:37 PM
#4
Re: Need Help With This Code !!!!
How about having the OP do his/her own homework?
The problem is easy enough for the OP to fix for themself without giving away answers. Since many teachers do look around the Web for copied answers, your posts could get the OP in a lot of trouble.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 20th, 2010 at 06:40 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|