In: Computer Science
Objective:
Write this program in the C programming language
Loops with input, strings, arrays, and output.
Assignment:
It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory.
You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more than 100 characters per book title title. Use malloc to allocate the strings in the array.
So, it will be a menu:
Please find the code below:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char **books;
float price[10];
int count = 0;
int i;
books = (char **)malloc(10* sizeof(char*));
for(i=0;i<10;i++){
books[i] = (char *)malloc(100*
sizeof(char));
}
int choice;
while(1){
printf("Enter book
information\n");
printf("List all books and
prices\n");
printf("List total value of
inventory\n");
printf("Enter your choice :
");
scanf("%d",&choice);
if(choice==1){
printf("Enter
book name : ");
scanf("%s",books[count]);
printf("Enter
book price : ");
scanf("%f",&price[count]);
count++;
}else if(choice==2){
printf("%-20s%-20s\n","Book Name","Price");
int i;
for(i=0;i<count;i++){
printf("%-20s%-20.2f\n",books[i],price[i]);
}
}else if(choice==3){
printf("%-20s%-20s\n","S.NO","Book Name");
int i;
for(i=0;i<count;i++){
printf("%-20d%-20.2f\n",i+1,books[i]);
}
}
printf("\n\n");
}
return 0;
}
output: