diamond number

how to draw of diamond of the form

A
B-B
C---C
D-----D
C---C
B-B
A

the input data is D

can someone give me a rough idea on how to start the programe...i know its gt to do with for loop

its too simple.. at least give it a try.. write some code so that someone can suggest the errors in your code..
can u give me n idea abt how to start the code?

the correct form should be ..
A
B-B
C---C
D-----D
C---C
B-B
A
start writing program.cpp.

program.cpp will have a main() function in it. So write a main() function that does nothing.

now since you need to do input and output, you'll need to use cout. Which header file do you need to #include for cout? Add that #include to the top.

now, the first thing main() should do is output a message like "Enter a letter:" or something. Next, it should read in a character into a char. For simplicity's sake, convert the inputted character to uppercase. Use toupper() for this. You'll also need to figure out what header file declares toupper() and add it as an #include at the top.

Next, you need a loop that goes from 'A' to the (upper-cased) letter the user entered. Each time through the loop it should output 1 line, so the first time it should output A, the next time B-B, etc.

To make that happen generically, you should first output the necessary character. Then, if the letter you output is 'D' (for example), it looks like you should output a number of dashes equal to the number of characters between the one you outputted and 'A' times 2 minus 1. For example, 2 * ( 'D' - 'A' ) - 1 = 2 * 3 - 1 = 6 - 1 = 5.

Once that loop finishes, you've output half of the diamond. Next, you need a second loop that goes from one less than the (uppercase) letter the user entered back down to 'A', and you'll do something very similar in that loop.

Something like this does it but its pretty clunky. How do you apply say the 5 in above example to obtain the correct spacing?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
cout<<"    ";
char array[8][8]={0};
  for(int i=0;i<8;i++ )
  for(int j=0;j<8;j++)
   {if(i==0&&j==4)cout<<'A'<<endl;
   if(i==1&&j==3)cout<<"   "<<'B';
   if(i==1&&j==5)cout<<" "<<'B'<<endl;
   if(i==2&&j==2)cout<<"  "<<'C';
   if(i==2&&j==6)cout<<"   "<<'C'<<endl;
   if(i==3&&j==1)cout<<" "<<'D';
   if(i==3&&j==7)cout<<"     "<<'D'<<endl;
   if(i==4&&j==2)cout<<"  "<<'E';
   if(i==4&&j==6)cout<<"   "<<'E'<<endl;
  }
cout<<"...etc...";  
cout<<endl;  
system("PAUSE");
return 0;    
}

The letters print out without any dots.
.....A
...B.B
..C...C
.D.....D
..E...E
...etc...
Press any key to continue . .
Topic archived. No new replies allowed.