Palindrome-kinda program with pointers

This is a discussion on Palindrome-kinda program with pointers within the C++ Programming forums, part of the General Programming Boards category; @anon, what do you mean by store the words in a vector. My friend told me to store it in ...

  1. #16
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    @anon, what do you mean by store the words in a vector. My friend told me to store it in a dynamic array, but then he just left...can you show me a dynamic array example please?! Thank you for your help, oh and concerning your last post, I totally changed my program and I fixed the portion. I ultimately used what you said. Thanks though. Please let me know about the dynamic array.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    std::vector is a container that provides a dynamic array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, you don't need to have two code branches. If the string has an even number of chars, each gets swapped, otherwise, all but the middle does, which is the correct behavior anyway.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #19
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    how do I store my values into a dynamic array?!

  5. #20
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Dynamic Array Question

    here is my code so far for those who would like some context to my previous post:

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>  
    void swap (char *front, char *rear); 
    using namespace std;
     
    int main()
    {
      string input, word;
      char *front, *rear; 
      char *p; // This will hold the individual words temporarily until they are replaced
    
      cout << "Enter a sentence that you would like to reverse: ";
      getline(cin, input);
     
      // put the sentence into a stream
      istringstream break_apart(input);
      
      while (break_apart >> word)
      {
        front = &word.at(0);
        rear = &word.at(word.size() - 1);
        while (front <= rear)
        {
              swap (*front, *rear);
              front++;
              rear--;
        }
        cout << word << " ";
      } 
     
      return 0;
    }
    void swap (char *front, char *rear)
    {
         char temp = *front;
         *front = *rear;
         *rear = temp;
    }
    If you didn't see my previous post, I would like to know how to create a dynamic array. Thank you to all who can help me, much appreciated!

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    As Laserlight already pointed out, you can simply use std::vector. Try looking up the documentation for using one - that will probably help you solve these sort of issues yourself, in the future.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #22
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    ok, so if it were a vector of strings, I would want to .push_back each word to store it at the end of the vector correct? essentially copying the components. so, I think I did that correctly, the code is posted below, but how would I display that vector of strings. the way I have obviously doesn't work. please let me know. Thank you for all of the help!!

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>  
    void swap (char *front, char *rear); 
    using namespace std;
     
    int main()
    {
      string input, word;
      char *front, *rear; 
      vector <string> array; 
    
      cout << "Enter a sentence that you would like to reverse: ";
      getline(cin, input);
     
      istringstream break_apart(input); 
      
      while (break_apart >> word)
      {
        front = &word.at(0);     
    rear = &word.at(word.size() - 1);     
    while (front <= rear)     {
              swap (*front, *rear);
              front++;
              rear--;
        }
        array.push_back(word);
        cout << word << " ";
        
      }
      return 0;
    }
    void swap (char *front, char *rear) {
         char temp = *front;
         *front = *rear;
         *rear = temp;
    }

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You could do:

    Code:
    for( vector< string >::const_iterator next = array.begin( ), end = array.end( ); next != end; ++next )
    	cout << *next << endl;
    Or perhaps:

    Code:
    for( size_t index = 0, size = array.size( ); index < size; ++index )
    	cout << array[ index ] << endl;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #24
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    is there a way though, that I can input all of the characters of the modified string above into a string, i.e., so that I can just do cout << mod_string, and I see "olleH"? Thanks again for the help.

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That's exactly what it does.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #26
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    ok, maybe im confused. so with the example you gave me, i'll use the second one because I understand that one, i don't see how that's storing the values into an array. I see that for loop shooting out characters at the user one by one, rather than modified word by modified word. What i am looking for (because I do not know how to do it and I can't find a good example on the internet over dynamic arrays) is a way for my modified words to be put into a vector <string> or something like that one by one as they are modified. Then after the entire original string is read from, I can just say "cout << mod_string" without having to go through a for loop to do it.

    Not to be rude or anything Sebastiani, maybe I'm wrong, it just doesn't look like that's what your post above does. Maybe I gave bad information so that you got the wrong idea, I don't know. Please let me know if I'm wrong though, because I do really want to understand this stuff. I appreciate your help, thanks again!

  12. #27
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i don't see how that's storing the values into an array.

    Either you're not paying attention or you're just plain confused. The examples I gave you output *strings* from a vector, which is what you had asked. Naturally, since the code you posted earlier already does the reading and storage of the words I assumed you didn't need yet another demonstration of how that was done.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #28
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    a little bit of both now, haha. I apologize for the confusion.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. Replies: 5
    Last Post: 05-25-2004, 04:36 PM
  3. Pointers program
    By Jamina in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 02:31 PM
  4. Replies: 5
    Last Post: 11-19-2002, 08:36 PM

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