Write a C program that reads and maintains the price list of books in one dimens
ID: 3760864 • Letter: W
Question
Write a C program that reads and maintains the price list of books in one dimensional array of type float in increasing order of price. The program should do following operations on the price list in separate functions (write separate functions for these tasks and call them in main()):
(i) Insert a new book's price into the list without changing the order. [Hint: Require shifting of elements right after insertion.]
(ii) Find and delete the requested price entry from the list. [Hint: Require shifting of elements left after finding the element to be deleted.]
(iii) Count number of books whose price is less than Rs.150.
(iv) Count number of books whose price is more than Rs. 1550.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void insertPrice(float Array[20], int numOfElements, float p)
{
int i;
for(i = numOfElements-1; Array[i] > p && i >= 0; i-- )
Array[i+1] = Array[i];
Array[i+1] = p;
}
void deletePrice(float Array[20], int numOfElements, float p)
{
int i;
for(i = 0; i < numOfElements; i++)
if(Array[i] == p)
break;
for(; i < numOfElements-1; i++)
Array[i] = Array[i+1];
}
int count1(float Array[20], int numOfElements)
{
int i, count = 0;
for(i = 0; i < numOfElements; i++)
if(Array[i] < 150)
count++;
return count;
}
int count2(float Array[20], int numOfElements)
{
int i, count = 0;
for(i = 0; i < numOfElements; i++)
if(Array[i] > 1550)
count++;
return count;
}
void display(float Array[20], int n)
{
int i;
for(i = 0; i < n; i++)
printf("%.2f ", Array[i]);
printf(" ");
}
int main()
{
float Array[20], price;
int ch, count = 0;
while(1)
{
printf("1. Insert Price. ");
printf("2. Delete Price. ");
printf("3. Display Price. ");
printf("4. Count books price < 150. ");
printf("5. Count books price > 1550. ");
printf("6. Exit. ");
printf("Enter your choice: ");
scanf("%i", & ch);
switch(ch)
{
case 1: printf("Enter the price: ");
scanf("%f", &price);
insertPrice(Array, count, price);
count++;
break;
case 2: printf("Enter the price: ");
scanf("%f", &price);
deletePrice(Array, count, price);
count--;
break;
case 3: display(Array, count);
break;
case 4: printf("The number of books whose price < 150 is: %i ", count1(Array, count));
break;
case 5: printf("The number of books whose price > 1550 is: %i ", count2(Array, count));
break;
case 6: exit(0);
default: printf("Invalid choice. ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.