6 Replies - 388 Views - Last Post: 04 November 2009 - 01:40 PM Rate Topic: -----

#1 NinjaLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-October 09

Need help with program segments to study

Posted 04 November 2009 - 10:21 AM

I need help with these program segments, so I can study for an exam. Please assist me


Example of program segment:

Return the last item of a queue leaving the queue unchanged

while (!q.isempty())
{
int e;
q.deleteQueue(e);
p.addQueue(e);
}
cout<<e<<endl;
while (!p.isempty())
{
p.deleteQueue(e);
q.addQueue(e);
}



I have attempted these questions on my own, but failed horribly. I ask anyone who has knowledge of C++ to help assist me. I really appreciate it. I do not want to fail. I want to use the variables and stuff that I used in the example above. If anyone wants to help me please reply back!


1. Determine if a word is a palindrome using queues

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

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

4. Reverse the elements in a queue (using recursion)

5. Exchange the front 2 elements of a queue

*** MOD EDIT: Added code tags. Please :code: ***

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with program segments to study

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Need help with program segments to study

Posted 04 November 2009 - 10:24 AM

You're going to have to show some effort. You've been here for a month, you know the rules. Start with the first one...show what you've "attempted ... and failed."
Was This Post Helpful? 0
  • +
  • -

#3 NinjaLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-October 09

Re: Need help with program segments to study

Posted 04 November 2009 - 10:58 AM

Here is my attempt...


Determine if a word is a palindrome or not using queues



q.addQueue(v);

for (int i = q.front(); i < q.front(); i--)
{
   for (int j = q.back(); i<q.back(); i++
  {
   if ( q.front() == q.back() )
    {
     cout<<"It is a palindrome"<<endl;
     }
       else 
       {
         cout<<"It is not a palindrome"<<endl;
        }
      }
    }





Return the last item of a queue leaving the queue unchanged

if (!p.QueueIsEmpty() )
{
p.deleteQueue(v);
}
p.addQueue(v);
cout<<v;





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

if (!s.QueueIsEmpty)
{
for (int i=q.front(); i<q.front(); i--)
  {
     for (int j=q.back(); i<q.back(); i++)
        {
        if (q.front() == 'e') || (q.back() == 'e')
         {
         q.addQueue(x);
         }
         }
   }





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

if (q.QueueIsEmpty())
{
for (i = q.front(); i<q.front()-3; i--)
  {
   q.DeleteQueue(v)
   s.AddQueue(v)
   sum+=val;
   }
for (int i =0; i<3; i++)
{
  s.DeleteQueue(v);
  q.AddQueue(v);
}
else
{
 cout<<"It is empty"<<endl;
}
cout<<"Sum is "<<sum;





Reverse the elements in a queue

while (!s.QueueIsEmpty())
{
p.DeleteQueue(v);
}
p.AddQueue(v);
cout<<v<<" ";




Exchange the front 2 elements of a queue

p.DeleteQueue(v);
p.DeleteQueue(e);
p.AddQueue(v);
p.AddQueue(e);


Was This Post Helpful? 0
  • +
  • -

#4 NinjaLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-October 09

Re: Need help with program segments to study

Posted 04 November 2009 - 12:28 PM

Is anyone available to assist me? I am not using any classes. I am basically using pseudo code. Please help!

This post has been edited by NinjaLink: 04 November 2009 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Need help with program segments to study

Posted 04 November 2009 - 01:10 PM

You haven't asked a question. Telling us your assignment is not a question.
Was This Post Helpful? 0
  • +
  • -

#6 NinjaLink  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-October 09

Re: Need help with program segments to study

Posted 04 November 2009 - 01:11 PM

I just need to know if my answers are correct and if not, what do I need to do to correct them
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Need help with program segments to study

Posted 04 November 2009 - 01:40 PM

There's no way to verify the correctness of what you wrote. We don't know what the types of the variables are, the functions, their behavior, etc.

If you want to verify correctness by yourself, you can write small programs making use of the logic in these snippets. Encode your snippets as a function, and call the functions appropriately. Your program(s) serve as unit tests, basically.

If your program functions as you would expect it to, that's a sign you are correct.

EDIT: Although I can't formally verify anything you have, a quick look suggests your logic makes no sense. Look at the way you determine if a word is a palindrome or not. You have two for loops, but all you compare is q.front() and q.back(). Does that make sense?

This post has been edited by Oler1s: 04 November 2009 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1