Sep 24, 2015 at 10:30pm UTC
hey guys I am pretty new to the c++. I wrote this program which should ask me to input the diamond width and lateral offset and it should then produce a diamond shape.
its asking for the diamond width and lateral offset, but its not producing the diamond shape instead its giving me a whole bunch of lines. can someone please correct this code to help produce a diamond shape. thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
using namespace std;
void a( void );
void s( void );
void n( void );
void pyramid(int ,int );
void wedge(int ,int );
void a() {cout << "*" ;}
void s() {cout << " " ;}
void n() {cout << "\n" ;}
void pyramid (int width, int offset)
{
int count1, count2;
for (int j=1; j < width; j += 2)
{
for (int i = 0; i < offset; i++) a();
count2 = j;
count1 = (width - count2)/2;
{
for (int i = 0; i < count1; i++) a();
for (int i = 0; i < count2; i++) s();
for (int i = 0; i < count1; i++) n();
}
}
n();
}
void wedge(int width, int offset)
{
for (int j = 0; j < width; j++)
{
for (int i = 0; i < offset; i++) a();
{
for (int i = 0; i < 1; i++) a();
for (int i = 0; i < j; i++) s();
for (int i = 0; i < width - j - 1; i++) n();
}
}
cout << endl;
}
int main()
{
int colWidth, latOffset;
cout << "This is a program for outputting a diamond formation.\n" ;
cout << "First you must select an odd number diamond width.\n" ;
cout << "Then you must select a lateral offset even or odd.\n" ;
cout << " Input diamond width: " ;
cin >> colWidth;
if (colWidth % 2 == 0)
{
cout << "ERROR: You entered an even number.\nThe program is correcting your error\n" ;
colWidth +=1;
}
cout << "Input lateral offset: " ;
cin >> latOffset;
cout << "\n" ;
pyramid (colWidth, latOffset);
wedge (colWidth, latOffset);
cout << "\nAuthor: Amar" << endl;
cin.get();
cin.get();
return 0;
}
Last edited on Sep 24, 2015 at 10:32pm UTC
Sep 25, 2015 at 11:12am UTC
Hey pal!
Here is my code for diamond output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream.h>
#include <conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float col,row,side;
cout<<"Enter the size: " ;cin>>side;
for (col=side;col>=1;col--)
{ for (row=1;row<=col;row++)
cout<<" " ;
cout<<"*" ;
for (row=side-1;row>=col;row-=0.5)
cout<<" " ;
if (col==side)
cout<<'\n' ;
else
cout<<"*\n" ;
}
for (col=side;col>=1;col--)
{ for (row=side;row>col;row--)
{ if (row==0)
break ;
else
cout<<" " ;
}
cout<<"*" ;
for (row=1.5;row<=col+0.5;row+=0.5)
cout<<" " ;
if (col==1)
cout<<"*\n" <<setw(side+1)<<"*" ;
else
cout<<"*\n" ;
}
cout<<"\n\n" ;
getch();
}
Mine asks you one input but your's is designed to ask for two, I don't get why lateral offset is being asked. Please explain me it's working so that I can help......
Last edited on Sep 25, 2015 at 11:38am UTC
Sep 25, 2015 at 12:37pm UTC
Hey keskiverto,
That part has been the main problem for me, I wrote a part this code while my professor was explaining a code for triangle as an example for this homework he gave us. After that I tried to improvise it so that I can get a diamond, but the problem came where I have to input the diamond shape and lateral offset. He didn't explain that while he did that example problem. He explained only one input. I don't know how to explain those lines because i am still trying to figure out how to learn c++. Thankyou for your concern though.
Sep 25, 2015 at 12:58pm UTC
Here is your code pal!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream.h>
#include <conio.h>
#include<iomanip.h>
void space()
{cout<<' ' ;}
void asterisk()
{cout<<'*' ;}
void nline()
{cout<<'\n' ;}
void lastasterisk(int side)
{cout<<setw(side+1)<<"*" ;}
void main()
{
clrscr();
float col,row; int side;
cout<<"Enter the size: " ; cin>>side;
if (side%2==0)
{ cout<<"ERROR: You entered an even number." ;
nline();
cout<<"The program is correcting your error" ;
nline();
side+=1;
}
for (col=side;col>=1;col--)
{ for (row=1;row<=col;row++)
space();
asterisk();
for (row=side-1;row>=col;row-=0.5)
space();
if (col==side)
nline();
else
{asterisk();nline();}
}
for (col=side;col>=1;col--)
{ for (row=side;row>col;row--)
{ if (row==0)
break ;
else
space();
}
asterisk();
for (row=1.5;row<=col+0.5;row+=0.5)
space();
asterisk(); nline();
if (col==1)
lastasterisk(side);
}
nline();nline();
getch();
}
I have altered mine as you said............
Last edited on Sep 25, 2015 at 1:03pm UTC
Sep 25, 2015 at 1:08pm UTC
Thank you very much buddy for your help. but this is a shallow diamond and I need to have two inputs like the code I wrote. but this just has one. thanks anyway.
Last edited on Sep 25, 2015 at 4:31pm UTC
Sep 26, 2015 at 6:51am UTC
Please show us an example of what the correct output is. Say for width = 7 and offset = 2. Type blanks as '-' character.
Sep 26, 2015 at 4:08pm UTC
hi kemort
That isn't latest.
amar27 asked me in a PM to remodify the program.
So sent him the update but forgot to update it here in fourm:P
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream.h>
#include <conio.h>
#include<iomanip.h>
void space()
{cout<<' ' ;}
void asterisk()
{cout<<'*' ;}
void nline()
{cout<<'\n' ;}
void lastasterisk(int side,int offset)
{cout<<setw(side+offset+1)<<"*" ;}
void main()
{
clrscr();
float col,row; int side, offset;
cout<<"Enter the size of diamond: " ; cin>>side;
if (side%2==0)
{ cout<<"ERROR: You entered an even number." ;
nline();
cout<<"The program is correcting your error" ;
nline();
side+=1;
}
cout<<"Enter it's lateral offset: " ; cin>>offset;
for (col=side;col>=1;col--)
{ for (row=1;row<=col+offset;row++)
space();
asterisk();
for (row=side-1;row>=col;row-=0.5)
asterisk();
if (col==side)
nline();
else
{asterisk();nline();}
}
for (col=side;col>=1;col--)
{ for (row=side;row+offset>col;row--)
space();
asterisk();
for (row=1.5;row<=col+0.5;row+=0.5)
asterisk();
asterisk(); nline();
if (col==1)
lastasterisk(side,offset);
}
nline();nline();
getch();
}
so....copy the code and run it in your IDE for the output of all kinda inputs :D
Have a nice time!
[EDIT] Please browse this one too
http://www.cplusplus.com/forum/general/174405/
Last edited on Sep 26, 2015 at 4:23pm UTC
Sep 27, 2015 at 2:02am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
using namespace std;
void pyramid(int , int );
void wedge(int , int );
int main()
{
int colWidth, latOffset;
cout << "This is a program for outputting a diamond formation.\n" ;
cout << "First you must select an odd number diamond width.\n" ;
cout << "Then you must select a lateral offset even or odd.\n" ;
cout << " Input diamond width: " ;
cin >> colWidth;
if (colWidth % 2 == 0)
{
cout << "ERROR: You entered an even number.\nThe program is correcting your error\n" ;
colWidth += 1;
}
cout << "Input lateral offset: " ;
cin >> latOffset;
cout << "\n" ;
pyramid (colWidth, latOffset);
wedge (colWidth, latOffset);
cout << "\nAuthor: Amar" << endl;
return 0;
}
void pyramid (int width, int offset)
{
int count = 1;
for (int i = 1; i < width; i++)
{
for (int j = 0; j < offset; j++)
cout << ' ' ;
for (int k = 0; k < width - i; k++)
cout << ' ' ;
for ( int w = 0; w < count; w++)
cout << "*" ;
count +=2;
cout << endl;
}
}
void wedge(int width, int offset)
{
// Similar to pyramid
}
Last edited on Sep 27, 2015 at 2:03am UTC
Sep 28, 2015 at 4:03am UTC
LOL :]
Last edited on Sep 28, 2015 at 4:03am UTC