Printing the Alphabet. Shape

I've gotten this down to where it prints the start of the diamond with the letter 'A' but it doesn't finish it. I can't seem to figure it out. please help.

this is what its suppose to look like when done.

For example, if the user enters 5, then the following diamond
comprised of 5 rows of uppercase letters is printed:
__A (2 spaces preceding first letter)
_ABC (1 space preceding first letter)
ABCDE (0 spaces preceding first letter)
_ABC (1 space preceding first letter)
__A (2 spaces preceding first letter)

*_* are spaces.


This is my code so far.


#include <cstdlib>
#include <iostream>
using namespace std ;

// ------------------------------------------------------------------------- //

// Function getUserInput obtains an integer input value from the user.
// This function performs no error checking of user input.
int getUserInput ( )
{
int N ;
cout << endl << "Please enter a positive, odd integer value: " ;
cin >> N ;
cout << endl ;
return N ;
} // end getUserInput function

// ------------------------------------------------------------------------- //

// Function printDiamond prints a diamond comprised of N rows of asterisks.
// This function assumes that N is a positive, odd integer.
void printDiamond ( int N )
{
char i=65;
// print top ~half of the diamond ...
for ( int row = 1 ; row <= (N/2 + 1) ; row ++ )
{
for ( int spaceCount = 1 ; spaceCount <= (N/2 + 1 - row) ; spaceCount ++ )
cout << ' ' ;
for ( int column = 1 ; column <= (2*row - 1) ; column ++ )
for ( char i=65; (i=i+N); i++)
cout << i;
cout << endl ;
} // end for loop

// print bottom ~half of the diamond ...
for ( int row = (N/2) ; row >= 1 ; row -- )
{
for ( int spaceCount = 1 ; spaceCount <= (N/2 + 1 - row) ; spaceCount ++ )
cout << ' ' ;
for ( int column = 1 ; column <= (2*row - 1) ; column ++ )
for ( char i=65; (i=i+N); i++)
cout << i;
cout << endl ;
} // end for loop

return ;
} // end printDiamond function

// ------------------------------------------------------------------------- //

int main ( )
{
printDiamond ( getUserInput() ) ;
} // end main function

// ------------------------------------------------------------------------- //



What if the user enters an even number?

Here's my implementation: http://ideone.com/OhhhR7
Last edited on
this would work and all but if I'm using things I haven't learned then how will I actually learn how to use it? Thank you for trying though :)
Oh, well.

I modified your printDiamond function. Here you go:

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
void printDiamond (int N)
{
    for (int i = 1; i <= N; i += 2)
    {
        for (int j = 0; j < (N - i) / 2; j++)
        {
            cout << ' ';
        }

        for (int j = 0; j < i; j++)
        {
            cout << char(j + 'A');
        }

        cout << endl;
    }

    for (int i = N - 2; i >= 1; i -= 2)
    {
        for (int j = 0; j < (N - i) / 2; j++)
        {
            cout << ' ';
        }

        for (int j = 0; j < i; j++)
        {
            cout << char(j + 'A');
        }

        cout << endl;
    }
}
Topic archived. No new replies allowed.