-2

I have been given the following homework question :

Given a set of 'n' elements and 'r', write a generic function to right shift the set of elements by 'r' position. If the elements are to moved to position greater than 'n' then wrap the shift process to the beginning of the collection. For example, if the set of five elements are 1,7,8,9,12 and value of 'r' is 3 then the set of elements would be 8, 9, 12, 1, 7.

I kind of felt a pull towards Circular Queue for this question so I wrote a code using it . Right now it doesn't contain any generic functions but that's not an issue , my problem is that my shift function isn't shifting the elements properly . Please give me a hand with this .

#include<iostream>
#include<string>
using namespace std;
#define max 10

int q[max];
int front,rear;

void init() {
    front = -1;
    rear = -1;
}

void enqueue(int x) {
    if(front == (rear+1)%max) {
        cout<<"Queue overflow";
    }

    if(front == -1) {
        front = 0;
        rear = 0;
    }
    else {
        rear = (rear+1)%max;
    }
    q[rear]=x;
    //cout<<"Front"<<front<<endl;
    //cout<<"Rear"<<rear<<endl;
}

int dequeue() {
    int x;
    if(front == -1) {
        cout<<"Queue Underflow";
    }
    else {
        x = q[rear];
        rear = (rear+1)%max;
    }
    return x;
}

void display() {
    int i;
    if(front == -1) {
        cout<<"Queue Underflow";
    }
    else {
        for(i = front; i != rear; i =( i+1)%max) {
            cout<<q[i]<<'\t';
        }
        cout<<q[rear]<<endl;
    }
}

void shift(int num) {
    //do the shifting business here
    int i,x,r;
    cout<<"Enter by how many positions should the elements be shifted towards the right"<<endl;
    cin>>r;
    for(i = 0; i < (num-r); ++i) {
        x = dequeue();
        enqueue(x);
    }
}

int main() {
    int ch,n,i,x,r;
    init();
    //cout<<"Enter your choice"<<endl;
    //cin>>ch;
    cout<<"Number of elements in the collection"<<endl;
    cin>>n;

    for(i = 0; i < n; ++i) {
        cout<<"Enter the value to be added"<<endl;
        cin>>x;
        enqueue(x);
    }
    cout<<"The elements to be shifted"<<endl;
    display();
    shift(n);
    display();
}

EDIT

The input I have given is :

Number of elements : 5

Elements to be shifted : 1 7 8 9 12

The value by which elements should be shifted : 3

Expected Output :

8 9 12 1 7

Output that I'm getting :

1 7 8 9 12 0 12 0 12

Ray Penber
  • 29
  • 8
  • Your `enqueue` and `dequeue` don't actually return anything .. your code is exhibiting undefined behavior. – txtechhelp Apr 26 '16 at 05:03
  • +txtchhelp I changed the return type of my enqueue function to void and returned my dequeued value but my code is still acting strange . I'm not able to understand what exactly is happening . Pls help – Ray Penber Apr 26 '16 at 05:08
  • 1
    *"my shift function isn't shifting the elements properly"* - for questions you post to S.O., it helps to supply input you've typed, what output you expected, what output you got, and whatever investigation or analysis you've done so far. – Tony Delroy Apr 26 '16 at 05:25
  • +Tony D I have made the edits . – Ray Penber Apr 26 '16 at 05:45
  • If it is not considered cheating you can use std::rotate, it makes left rotation. To make right rotation just pass reverse iterators. –  Apr 26 '16 at 06:06
  • +Dmitry Kuznetsov Thanks man . I'll try out your method . As much as I'm happy I found a new way to go about this question , I was more interested in finding out my mistake with this code . Pls help. – Ray Penber Apr 26 '16 at 06:16

1 Answers1

0

Your bug is in dequeue(). You need to advance the front pointer, not the rear:

int dequeue() {
    int x;
    if(front == -1) {
        cout<<"Queue Underflow";
    }
    else {
        x = q[front];
        front = (front+1)%max;
    }
    return x;
}
Cătălin Frâncu
  • 1,179
  • 8
  • 15
  • Thanks for the catch! I made the necessary changes now but still the output that I'm getting is : 8 9 12 12 12 and not : 8 9 12 1 7 Can you help me with this ? – Ray Penber Apr 26 '16 at 13:43
  • That's about the only thing I changed, and it works for me. I compiled the code with `g++ -Wall -o a a.cpp`. For speed, I put the input data in `a.in` and ran your program as `./a < a.in` -- but this shouldn't matter. You could try running `display()` after every enqueue / dequeue operation to figure out exactly where your code goes wrong. – Cătălin Frâncu Apr 26 '16 at 14:00
  • I'm still not able to figure out the fault with my code . I'll keep trying and if you are able to figure it out please let me know! Thanks again man – Ray Penber Apr 26 '16 at 14:24
  • I got my mistake . I wrote a wrong dequeue function . My logic was right alll along . – Ray Penber Apr 26 '16 at 14:29