incorrect sort and search output

my sort and search code looks correct but when it runs it acts all weird, it sorts in a wrong way where it shows 0s if the elements aren't at the required max arrays and it also doesn't place the elements in the correct order and when it comes to search it shows the wrong index. i know in the sort void the for loops initializations and expressions are wrong cuz i was going through trial and error but i dont know how to set it up exactly to get the output i want(either ascending or descending order), but since both search and sort runs i guess i got the flow right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<iostream>
#include<cstdlib>
using namespace std;


const int Max = 10;
int arr[Max];
int rear=-1;
int front =-1;
int key;
int flag, index, i,j,k,n,exchange,temp;

bool isfull(){
    if (rear == Max-1){
        return true;
    }
    else {
        return false;
    }
}

bool isempty (){
    if (front ==-1 || front>rear){
        return true;
    }
    else{
        return false;
    }
}

void enqueue(){
    int ins_item;
    if (isfull()){
        cout<<"\nqueue overflow!\n";
        return;
    }
    else {
        if (front ==-1)
            front = 0;
        cout<<"\n enter an element : ";
        cin>>ins_item;
        cout<<"\n";
        rear = rear + 1;
        arr[rear]= ins_item;
    }
}

void dequeue(){
    if (isempty()){
        cout<<"queue is empty!\n";
        return;
    }
    else {
        cout<<"element removed from queue is : "<<arr[front];
        cout<<"\n";
        front++;
    }
}
void getdata ()
{
cout<<"\n enter the key to find the element in the array:";
cin>>key;
}
void linearsearch()
{
    flag = -1;
    for (int ctr = 0 ; ctr < 10; ctr++)
    {
        if (key == arr[ctr])
        {
            flag = 1;
            index = ctr;
        }
    }
}
void displaysearch()
{
    if (flag == -1)
    {
        cout<<"\n given key " << key <<" is not present in queue array"<<endl;
    }
    else
        cout << "\n given key "<<key<<" is position at index "<<index<<endl;
}
void displayq(){
    int i;
    if (isempty()){
        cout<<"queue is empty!\n";
        return;
    }
    else {
        cout<<"elements in queue :";
        for (i = front; i <=rear; i++)
            cout<<arr[i]<<"  ";
        cout<<"\n";
    }
}
void sort()
{
    cout<<"unsorted list : ";
    for (i=front;i<rear;i++)
    {
        cout<<arr[i]<<"  ";
    }

    for (i = front; i <=rear-1;i++)
    {
        exchange = 0;
        for (j=front; j<rear-1;j++)
        {
            if(arr[i]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]= temp;
                exchange++;
            }
        }
        if (exchange==0)
            break;
        for (k=0;k<i;k++)

            cout<<arr[k]<<"  ";
        cout<<"\n";

    }
    cout<<"\nsorted lists:  ";
    for (i=front;i<rear; i++)
    {
        cout<<arr[i]<<"  ";
    }

}

int main()
{

        
        cout<<"1. add\n";
        cout<<"2. delete\n";
        cout<<"3. sort\n";
        cout<<"4. display\n";
        cout<<"5. search\n";
        cout<<"6. exit\n";
        int ans_q;

        do {
            cout<<"enter your choice : ";
            cin>>ans_q;
        switch (ans_q){
        case 1: enqueue(); break;
        case 2: dequeue(); break;
        case 3: sort(); break;
        case 4: displayq(); break;
        case 5: getdata(); linearsearch(); displaysearch();
        case 6: return 0; break;
        default : cout<<"you have entered the wrong number please pick again\n\n";
        break;
            }
        }while(ans_q != 7);
}
Last edited on
It's not good practice to have global variables like this. What happened to using a class like previous? If not a class, pass variables as a parameter. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <limits>
#include <utility>
using namespace std;

const int badfind {numeric_limits<int>::min()};
const int Max {10};

int arr[Max] {};
int rear {-1};
int front {-1};

bool isfull() { return rear == Max - 1; }

bool isempty() { return front == -1 || front > rear; }

void enqueue() {
	if (isfull()) {
		cout << "\nqueue overflow!\n";
		return;
	}

	if (front == -1)
		front = 0;

	int ins_item {};

	cout << "\n enter an element : ";
	cin >> ins_item;
	cout << "\n";

	arr[++rear] = ins_item;
}

void dequeue() {
	if (isempty())
		cout << "queue is empty!\n";
	else
		cout << "element removed from queue is : " << arr[front++] << '\n';
}

int getdata()
{
	int key {};

	cout << "\n enter the key to find the element in the array: ";
	cin >> key;
	return key;
}

int linearsearch(int key)
{
	for (int ctr = front; ctr <= rear; ++ctr)
		if (key == arr[ctr])
			return ctr;

	return badfind;
}

void displaysearch(int key, int index)
{
	if (index == badfind)
		cout << "\n given key " << key << " is not present in queue array\n";
	else
		cout << "\n given key " << key << " is position at index " << index << '\n';
}

void displayq() {
	if (isempty())
		cout << "queue is empty!\n";
	else {
		cout << "elements in queue :";

		for (int i = front; i <= rear; ++i)
			cout << arr[i] << "  ";

		cout << "\n";
	}
}

void sort()
{
	if (isempty())
		return;

	cout << "unsorted list :\n";
	displayq();

	for (int i = front; i <= rear; ++i) {
		int exchange {};

		for (int j = front; j <= rear - 1; ++j)
			if (arr[j] > arr[j + 1]) {
				swap(arr[j], arr[j + 1]);
				++exchange;
			}

		if (exchange == 0)
			break;
	}

	cout << "\nsorted lists:\n";
	displayq();
}

int main()
{
	int ans_q {};

	do {
		cout << "1. add\n";
		cout << "2. delete\n";
		cout << "3. sort\n";
		cout << "4. display\n";
		cout << "5. search\n";
		cout << "6. exit\n";
		cout << "enter your choice :";
		cin >> ans_q;

		switch (ans_q) {
			case 1: enqueue(); break;
			case 2: dequeue(); break;
			case 3: sort(); break;
			case 4: displayq(); break;
			case 6: break;
			case 5:
			{
				const int key {getdata()};
				displaysearch(key, linearsearch(key));
			}
			break;

			default:
				cout << "you have entered the wrong number please pick again\n\n";
				break;
		}
	} while (ans_q != 6);
}

thank you, sorry for not presenting the code well. i was just testing it out before arranging it properly didn't think it would affect the entire thing.
First design, then code :)
Is this supposed to be a queue using a circular buffer? Right now it will only let you insert 10 items.

The code would be easier if you defined front and rear slightly differently:
1
2
3
int rear = 0;                   // next location to add an item
int front = 0;                  // next location to remove an item. If
                                // front == rear then queue is empty 


Then isfull, isempty, enqueue and dequeue become:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
bool
isfull()
{
    return (rear == Max - 1);
}

bool
isempty()
{
    return front == rear;
}

void
enqueue()
{
    int ins_item;
    if (isfull()) {
        cout << "\nqueue overflow!\n";
        return;
    } else {
        cout << "\n enter an element : ";
        cin >> ins_item;
        cout << "\n";
        arr[rear++] = ins_item;
    }
}

void
dequeue()
{
    if (isempty()) {
        cout << "queue is empty!\n";
        return;
    } else {
        cout << "element removed from queue is : " << arr[front++];
        cout << "\n";
    }
}
Topic archived. No new replies allowed.