Q:How to find average in queue by c plus plus code?
Ans:This code example easily explain how to find average in queue by simple c plus plus code.
Code Is:
#include<iostream>
using namespace std;
class queue{
int F,R;
int n[5];
int i;
public:
queue(){
F=R=-1;
}
void insert(){
if(R>=5){
cout<<"Queue is full";
F=R=-1;
return;
}
else
cout<<"Enter the 5 numbers:";
for( i=0;i<5;i++)
cin>>n[i];
n[++R]=i;
}
void average(){
if(R==F)
{
cout <<" queue empty";
return;
}
int avg,sum;
for(int i=0;i<5;i++)
sum+=n[i];
avg=sum/5;
cout<<"\nAverage is:"<<avg<<endl;
}
void dele(){
if(F==R){
cout<<"\n Queue Underflow"<<endl;
return;
}
cout <<"deleted" <<n[++F];
}
};
int main(){
int ch;
queue a;
while(1)
{
cout <<"\n1.insert 2.average 3.delete 4.exit\nEnter ur choice";
cin >> ch;
switch(ch){
case 1:
a.insert();
break;
case 2:
a.average();
break;
case 3:
a.dele();
break;
case 4:
exit(0);
}
}
return 0;
}
0 comments:
Post a Comment