Wednesday, March 3, 2021

Menu Driven C Structure Program Implementing Linear Search and Insertion Sort In Both Orders

 

Create an array of structures for PRODUCT with the following data items:

Product ID

Product Name

Unit Price


a. Get the input from the user, for N Products and display them in a neat format.    

b. Implement Linear Search based on Product Name and display the result.               

c. Implement Insertion Sort based on Price and display the products in a neat format. 



 #include<stdio.h>

 #include<string.h>

struct product

{

 int id;

 float price;

 char item_name [20];

 };

 static int N=4;

 void display(struct product []);

 void intakeValues(struct product []);

  void linearSearch(struct product []);

  void iSA(struct product []);

 void main ()

 { 

  struct product bill[N];

 intakeValues(bill);

 printf("\nItems as entered\n\n");

 printf("\n\n\n");

  display(bill);

 linearSearch(bill);

 iSA(bill);

  getch ();

  }

  void intakeValues(struct product bill[])

  {

   int i;

   for(i=0;i<N;i++)

   {

   printf("\nProduct ID: ");

   scanf("%d",&bill[i].id);

   fflush(stdin);

   printf("\nProduct Name: ");

   gets(bill[i].item_name);

   fflush(stdin);

   printf("\nProduct price: ");

   scanf("%f",&bill[i].price);   

  }

}

void display(struct product bill[])

{

int i;

printf("\n        ID           Item Name           Price");

   for(i=0;i<N;i++)

   {

printf("\n%10d   %15s   %15.2f", bill[i].id, bill[i].item_name, bill[i].price);

}

}

void linearSearch(struct product bill[])

{

char toSearch[30];

int i,c=0;

fflush(stdin);

printf("\nEnter the product name: ");

gets(toSearch);

printf("\n        ID           Item Name           Price");

printf("\n\n\n");

for(i=0;i<N;i++)

   {

   if(strcmpi(toSearch,bill[i].item_name)==0)

   {

   c++;   

   printf("\n%10d   %15s   %15.2f", bill[i].id, bill[i].item_name, bill[i].price);

   }

   }

if(c==0)

printf("\nNot a single record found");

}

void iSA(struct product bill[])

{

int p,i,k,x,te;

float temp,t;

char tch[30];

for(i = 1 ; i <N; i++)

{

x=0;

p= i -1;

temp = bill[i].price;

while(temp <bill[p].price && p>=0)

{

  t=bill[p+1].price;

  bill[p+1].price = bill[p].price;

  bill[p].price=t;  

  te=bill[p+1].id;

  bill[p+1].id = bill[p].id;

  bill[p].id=te;  

  strcpy(tch,bill[p+1].item_name);

  strcpy(bill[p+1].item_name,bill[p].item_name);

   strcpy(bill[p].item_name,tch);

  p --;

  x++;

}

}

printf("\n\n\nItems sorted on the base of price\n\n");

printf("\n\n\n");

display(bill);

}



No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner