Program segments

Nov 4, 2009 at 7:40pm
I need help with the programming segments below...I showed my effort on trying to do them. However, I don't believe they are right. I am not using any queue classes, I am using pseudo code to try to help me understand each example. This is urgent! Please help. I will appreciate it.

If you can, please use the variable names that I used below.


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}
}
}





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


1
2
3
4
5
6
if (!p.QueueIsEmpty() )
{
p.deleteQueue(v);
}
p.addQueue(v);
cout<<v;





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


1
2
3
4
5
6
7
8
9
10
11
12
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);
}
}
}





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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;



5. Reverse the elements in a queue


1
2
3
4
5
6
while (!s.QueueIsEmpty())
{
p.DeleteQueue(v);
}
p.AddQueue(v);
cout<<v<<" ";



6. Exchange the front 2 elements of a queue


1
2
3
4
p.DeleteQueue(v);
p.DeleteQueue(e);
p.AddQueue(v);
p.AddQueue(e);
Nov 4, 2009 at 7:54pm
1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
q.addQueue(v);

for (int i = q.front(); i < q.front(); i--) // i = q.front(); i < q.front  <- this loop would never get executed
{
  for (int j = q.back(); i<q.back(); i++) // here you initialize j and then use i
  {
    if ( q.front() == q.back() ) // you should compare i and j
    {
      cout<<"It is a palindrome"<<endl; // outputting at the first match, it is a palindrome when all the elements are specular
    }
    else 
    {
      cout<<"It is not a palindrome"<<endl; // OK, this happens whenever i != j
    }
  }
}


2. You are adding an element (which you aren't suppose to do), you should return q.back()

3. Loops and condition have the same issues as #1

4. Similar to #2

5. The code is completely wrong: You should swap the fist element with the last etc. using a loop similar to #1

6. You need a temporary variable to hold the contents of one of the elements or you will lose it
Nov 4, 2009 at 8:21pm
thank you for your reply, I will see if I can correct these problems and then post them again! I REALLY appreciate it because no one else cared to help me in other places!~
Topic archived. No new replies allowed.