Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am struggling in how to solve a problem. I need to make my C program be able t

ID: 3688119 • Letter: I

Question

I am struggling in how to solve a problem. I need to make my C program be able to calculate averages, as well as enqueue, dequeue, display, and exit the application. I have everything working, but I have no clue in how to make the application display averages properly.

Here is my code: (note, I do not have a void created for averages because I was unable to get it to function properly. Everything in the code below functions properly and provides zero errors)

#include<conio.h>
#include<stdio.h>
#define max 5
int queue[max],front=0,rear=0;
int menu();
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
printf(" Queues using Arrays ");
do
{
ch=menu();
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4:
return;
default:printf(" Please enter a valid choice!!");
}
}while(1);
}

int menu()
{
int ch;
printf(" 1.ENQUEUE 2.DEQUEUE 3.DISPLAY 4.EXIT");
printf(" Enter your Choice: ");
scanf("%d",&ch);
return ch;
}

void enqueue()
{
int element;
if(rear==max)
{
printf(" Overflow!!");
}
else
{
printf(" Enter Element: ");
scanf("%d",&element);
queue[rear++]=element;
printf(" %d Enqueued at %d ",element,rear);
}

}

void dequeue()
{
if(rear==front)
{
printf(" Underflow!!");
}
else
{
front++;
printf(" Element is Dequeued from %d ",front);
}
}
void display()
{
int i;
if(front==rear)
{
printf(" Queue is Empty!!!");
}
else
{
printf(" ");
for(i=front;i<max;i++)
{
printf(" | %d ",queue[i]);
}
printf("| ");
}
}

Can you please show me how to make it display averages in the array and tell me why you decided to do it the way you did.

Explanation / Answer

we will add an average function to calculate the averages of the queue elements.We will insert this within the above code here.

#include<conio.h>
#include<stdio.h>
#define max 5
int queue[max],front=0,rear=0;

int sum=0;

int average;
int menu();
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
printf(" Queues using Arrays ");
do
{
ch=menu();
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4:
return;
default:printf(" Please enter a valid choice!!");
}
}while(1);
}

int menu()
{
int ch;
printf(" 1.ENQUEUE 2.DEQUEUE 3.DISPLAY 4.EXIT");
printf(" Enter your Choice: ");
scanf("%d",&ch);
return ch;
}

void enqueue()
{
int element;
if(rear==max)
{
printf(" Overflow!!");
}
else
{
printf(" Enter Element: ");
scanf("%d",&element);
queue[rear++]=element;
printf(" %d Enqueued at %d ",element,rear);
}

}

void dequeue()
{
if(rear==front)
{
printf(" Underflow!!");
}
else
{
front++;
printf(" Element is Dequeued from %d ",front);
}
}

void average()//This function calculates the averages of the queue elements by adding all elements/numberelmnts

{for(i=front;i<max;i++)

sum+=queue[i]; // sum of queue elements

average=sum/max; // average is calculates by dividing sum by number of elements(max)

}

printf(" the average over here "+average);

}


void display()
{
int i;
if(front==rear)
{
printf(" Queue is Empty!!!");
}
else
{
printf(" ");
for(i=front;i<max;i++)
{
printf(" | %d ",queue[i]);
}
printf("| ");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote