Answer to Question #297116 in C for Kamalesh

Question #297116

Write a c program to make a queue from user input and check whether prime number present or not in the queue and find frequency of the prime number.



Hints


Make a menu which will having following menu options


-insert queue data


-insert in which position


-delete queue data


-show queue


-show prime numbers in queue


-show frequency of prime number in queue




1
Expert's answer
2022-02-12T16:22:22-0500
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
int is_prime(int num);
void insert();
void insert_pos();
void delete();
void display();
void show_prime_numbers();
void show_frequency();
int queue_array[MAX];
int rear = -1;
int front = -1;
int main()
{
	int choice;
	while (1)
	{
		printf("1.Insert element to queue \n");
		printf("2.Insert element to queue on position \n");
		printf("3.Delete element from queue \n");
		printf("4.Display all elements of queue \n");
		printf("5.Show prime numbers in queue \n");
		printf("6.Show frequency of prime number in queue \n");
		printf("7.Quit \n");
		printf("Enter your choice : ");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			insert();
			break;
		case 2:
			insert_pos();
			break;
		case 3:
			delete();
			break;
		case 4:
			display();
			break;
		case 5:
			show_prime_numbers();
			break;
		case 6:
			show_frequency();
			break;
		case 7:
			exit(1);
		default:
			printf("Wrong choice \n");
		}
	}
}
void show_frequency()
{
	int i;
	int size = 0;
	int prime = 0;
	double fr = 0;
	if (front == -1)
		printf("Queue is empty \n");
	else
	{
		
		for (i = front; i <= rear; i++)
		{
			size++;
			if (is_prime(queue_array[i]))
			{
				prime++;
			}
		}
		fr = (double)prime / (double)size;
		printf("Frequency : %lf \n", fr);
	}


}
void show_prime_numbers()
{
	int i;
	if (front == -1)
		printf("Queue is empty \n");
	else
	{
		printf("Prime is : \n");
		for (i = front; i <= rear; i++)
		{
			if (is_prime(queue_array[i]))
			{
				printf("%d ", queue_array[i]);
			}
		}
		printf("\n");
	}
}
void insert_pos()
{
	int item;
	int id;
	if (rear == MAX - 1)
		printf("Queue Overflow \n");
	else
	{
		if (front == -1)
			front = 0;
		printf("Inset the element in queue index : ");
		scanf("%d", &id);
		printf("Inset the element in queue : ");
		scanf("%d", &item);
		if (id > rear)
		{
			rear = rear + 1;
		}
		queue_array[id] = item;
	}
}
void insert()
{
	int item;
	if (rear == MAX - 1)
		printf("Queue Overflow \n");
	else
	{
		if (front == -1)
			front = 0;
		printf("Inset the element in queue : ");
		scanf("%d", &item);
		rear = rear + 1;
		queue_array[rear] = item;
	}
}
void delete()
{
	if (front == -1 || front > rear)
	{
		printf("Queue Underflow n");
		return;
	}
	else
	{
		printf("Element deleted from queue is : %d\n", queue_array[front]);
		front = front + 1;
	}
}
void display()
{
	int i;
	if (front == -1)
		printf("Queue is empty \n");
	else
	{
		printf("Queue is : \n");
		for (i = front; i <= rear; i++)
			printf("%d ", queue_array[i]);
		printf("\n");
	}
}
int is_prime(int num)
{
	if (num <= 1) return 0;
	if (num % 2 == 0 && num > 2) return 0;
	for (int i = 3; i < num / 2; i += 2)
	{
		if (num % i == 0)
			return 0;
	}
	return 1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

kamalesh
13.02.22, 08:38

Thank you very much

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS