Need help with program segments

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Need help with program segments

 
0
  #1
21 Days Ago
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

  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

  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)

  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

  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

  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

  1. p.DeleteQueue(v);
  2. p.DeleteQueue(e);
  3. p.AddQueue(v);
  4. p.AddQueue(e);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,350
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
21 Days Ago
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #3
21 Days Ago
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

  1. s.pop(v);
  2. s.pop(e);
  3. s.push(v);
  4. s.push(e);

Duplicate top element in a stack

  1. s.pop(v);
  2. s.push(v);
  3. s.push(v);

Print the elements in the stack from top to bottom

  1. while (!s.isempty())
  2. {
  3. s.pop(v);
  4. cout<<v;
  5. }
Last edited by NinjaLink; 21 Days Ago at 3:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #4
21 Days Ago
Did my above post help? =/
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC