Monday, November 11
Diamond Printing Problem from Uxama Ahmad
The problem was diamond did not print odd rows with required spaces and stars.
#include <iostream>
using namespace std; int main() { int num,i=1,j=1,k=1,space,star; cout<<"Enter odd number of rows: "; cin>>num; if(num%2==0) num=num-1; space=num/2; star=1; cout<<endl; do { if(space!=0) do //prints spaces { cout<<" "; k++; }while(k<=space); k=1; do //prints stars { cout<<"*"; j++; }while(j<=star); j=1; cout<<endl; i++; //current row number for diamond if(i==(num/2)+1) { space=0; star=num; } if(i<=num/2) { star=star+2; space--; } if(i>1+num/2) { star=star-2; space++; } //star=2*i-1; }while(i<=num); return 0; }sample output
sorry it printed itself as is :P
ReplyDelete#include
using namespace std;
void main()
{
int num,i=1,j=1,k=1,space,star;
cout<<"Enter odd number of rows: ";
cin>>num;
if(num%2==0)
num=num-1;
space=num/2;
star=1;
do
{
do
{
cout<<"u";
k++;
}while(k<=space);
k=1;
do
{
cout<<"*";
j++;
}while(j<=star);
j=1;
cout<num/2)
{
star=star-2;
space++;
}
star=2*i-1;
}while(i<=num);
}
The actual code with problem was:
ReplyDelete#include <iostream> using namespace std; void main() { int num,i=1,j=1,k=1,space,star; cout<<"Enter odd number of rows: "; cin>>num; if(num%2==0) num=num-1; space=num/2; star=1; do { do { cout<<"u"; k++; }while(k<=space); k=1; do { cout<<"*"; j++; }while(j<=star); j=1; cout<<endl; i++; if(i==num/2) { space=0; star=num; } if(i<num/2) { star=star+2; space--; } if(i>num/2) { star=star-2; space++; } star=2*i-1; }while(i<=num); }
The problems were in concept that odd when divided by 2 is truncated to highest lower natural number and the do loop to print space it first executed and then checks the condition :)
ReplyDelete