Probably a Simple Solution.....

This is a discussion on Probably a Simple Solution..... within the C++ Programming forums, part of the General Programming Boards category; hey guys, im working on an extra credit assignment for my computer science class. what i need to do is ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    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. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 08:50 AM
  2. Solution to earlier quad-tree culling
    By VirtualAce in forum Game Programming
    Replies: 3
    Last Post: 08-10-2006, 08:04 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21