944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 416
  • C++ RSS
Nov 4th, 2009
0

Need help with program segments

Expand Post »
I need help with the programming segments below, so I can study for them...I showed my effort on trying to do them. However, I don't believe they are right. If someone can assist me with this, I will definitely appreciate it.


1. Determine if a word is a palindrome or not using queues

C++ Syntax (Toggle Plain Text)
  1. q.addQueue(v);
  2.  
  3. for (int i = q.front(); i < q.front(); i--)
  4. {
  5. for (int j = q.back(); i<q.back(); i++
  6. {
  7. if ( q.front() == q.back() )
  8. {
  9. cout<<"It is a palindrome"<<endl;
  10. }
  11. else
  12. {
  13. cout<<"It is not a palindrome"<<endl;
  14. }
  15. }
  16. }

2. Return the last item of a queue leaving the queue unchanged

C++ Syntax (Toggle Plain Text)
  1. if (!p.QueueIsEmpty() )
  2. {
  3. p.deleteQueue(v);
  4. }
  5. p.addQueue(v);
  6. cout<<v;


3. Replace every occurence of every (e) in a queue with an (x)

C++ Syntax (Toggle Plain Text)
  1. if (!s.QueueIsEmpty)
  2. {
  3. for (int i=q.front(); i<q.front(); i--)
  4. {
  5. for (int j=q.back(); i<q.back(); i++)
  6. {
  7. if (q.front() == 'e') || (q.back() == 'e')
  8. {
  9. q.addQueue(x);
  10. }
  11. }
  12. }



4. Give the sum of the 1st 3 elements in a queue leaving the queue unchanged

C++ Syntax (Toggle Plain Text)
  1. if (q.QueueIsEmpty())
  2. {
  3. for (i = q.front(); i<q.front()-3; i--)
  4. {
  5. q.DeleteQueue(v)
  6. s.AddQueue(v)
  7. sum+=val;
  8. }
  9. for (int i =0; i<3; i++)
  10. {
  11. s.DeleteQueue(v);
  12. q.AddQueue(v);
  13. }
  14. else
  15. {
  16. cout<<"It is empty"<<endl;
  17. }
  18. cout<<"Sum is "<<sum;


5. Reverse the elements in a queue

C++ Syntax (Toggle Plain Text)
  1. while (!s.QueueIsEmpty())
  2. {
  3. p.DeleteQueue(v);
  4. }
  5. p.AddQueue(v);
  6. cout<<v<<" ";

6. Exchange the front 2 elements of a queue

C++ Syntax (Toggle Plain Text)
  1. p.DeleteQueue(v);
  2. p.DeleteQueue(e);
  3. p.AddQueue(v);
  4. p.AddQueue(e);
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 4th, 2009
-7
Re: Need help with program segments
We don't have the definition of your queue class so can't really tell if some of those are right or wrong.

2. Probably wrong. You are not supposed to delete the item from the queue. Just retrieve it. There is probably another way to get the item out of the queue without deleting it.

3. Definitely wrong. Just think a minute about what you have done with those loops. You first initialize variable i to be q.front() and then test to see if i is less and q.front() ??? Most likely should be this? for(int i = q.front(); i < q.back(); i++) What I don't know if what kind of data is in the queue. Is it a queue of strings (most likely answer considering the question), a queue of ints or what??

4. Similar answer as given for #2, above.

5 & 6. I have no idea since I don't know the definition of the queue class.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,960 posts
since Aug 2005
Nov 4th, 2009
0
Re: Need help with program segments
I am not using a queue class for these program segments. I apologize for any confusion. They are just programming segments made up to help me understand how to do them. On my test tomorrow, I have to write program segments "similar" to those that I put, and I just wanted to make sure I am prepared. They are basically just used for practice, I guess like pseudo code. Sorry for any confusion

An example of a program segment that I did that were correct:

Swap top 2 elements in a stack

C++ Syntax (Toggle Plain Text)
  1. s.pop(v);
  2. s.pop(e);
  3. s.push(v);
  4. s.push(e);

Duplicate top element in a stack

C++ Syntax (Toggle Plain Text)
  1. s.pop(v);
  2. s.push(v);
  3. s.push(v);

Print the elements in the stack from top to bottom

C++ Syntax (Toggle Plain Text)
  1. while (!s.isempty())
  2. {
  3. s.pop(v);
  4. cout<<v;
  5. }
Last edited by NinjaLink; Nov 4th, 2009 at 3:09 pm.
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 4th, 2009
0
Re: Need help with program segments
Did my above post help? =/
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Solving quadratics from input file
Next Thread in C++ Forum Timeline: C++ and saveFileDialog





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC