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?
#include<iostream>
#include<cstdlib>
usingnamespace std;
constint 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){
returntrue;
}
else {
returnfalse;
}
}
bool isempty (){
if (front ==-1 || front>rear){
returntrue;
}
else{
returnfalse;
}
}
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);
}
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:
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.