# Thread: Probably a Simple Solution.....

1. ## Probably a Simple Solution.....

hey guys,

im working on an extra credit assignment for my computer science class. what i need to do is ask the user for a word, then the word is output is the Shape of an hour glass, for example if the word FRED is input, the output would look like:

FFF
R
E
DDD

if Chicken is input, the output would look like:
CCCCCCC
HHHHH
III
C
KKK
EEEEE
NNNNNNN

my code now just subtracts 2 letters from the output everytime a new character is found, then when it reaches 1, it does it the opposite way. there in lays a problem with even numbers...
Any help would be greatly appreciated.

thanks,
Yubie

2. use the modulus operator, %, and write two seperate loops - one for if the number of chars is even and one for if the number of chars is odd. (oh and check the number of characters by using another loop and a counter)

3. This is a nice problem if you want an elegant solution. Here is my second attempt (the first was the most obvious solution using counters and all kind of mess):
Code:
```#include <iostream>
#include <string>

void ch_fill ( char ch, int n )
{
for ( int i = 0; i < n; ++i )
std::cout<< ch;
std::cout<<'\n';
}

void hourglass ( std::string word )
{
// Handle a single character string
int len = word.length();

if ( len == 1 ) {
ch_fill ( word[0], 1 );
return;
}

// Deal with even and odd length strings
int mid, mod;

if ( len % 2 == 0 ) {
mid = word.length() / 2;
mod = 1;
}
else {
mid = word.length() / 2 + 1;
mod = 2;
}

// Perform the magic
int i;

for ( i = 0; i < mid; ++i )
ch_fill ( word[i], mid - i );

for ( i = mid; i < len; ++i )
ch_fill ( word[i % len], i % mid + mod );
}

int main()
{
std::string word;

std::cout<<"Enter a word: ";
std::getline ( std::cin, word );

hourglass ( word );

return 0;
}```
That was fun, do you have any other problems?

-Prelude

4. thanks a ton prelude, it looks like i was on the right track to begin with, your example just took me that last step....

Nate