## Thursday, 5 April 2018

### Structure sorting,searching with array

Define a structure that describes a hotel. It should have members that include the name, address, grade, average room charge and number of rooms.

Write a function to perform the following tasks.

i) To print all hotels details of a given grade in order of charges.
ii) To print hotel details with room charges less than given values.

Solution :
/********************************
Program : Structure sorting,searching with array
for more program visit :http://girfahelp.blogspot.in/p/c-language-structure-programming.html
**********************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 10
typedef struct ht
{
char name[30];
int room;
int charge;
}hotel;
void input(hotel*,int);
void print(hotel *);
void printbyRate(hotel *,int,int);
void main()
{
int i,opt,rate;
hotel rec[MAX];
char ch;
clrscr();
printf("How many record you want to insert>> ");
scanf("%
d",&i);
if(i>MAX)
printf("Maximum capacity of record holding is %d",MAX);
else
{
input(rec,i);
do
{
clrscr();
printf("\n************************\n");
printf("1. Print by Rate\n2. Print by grade\n0. Exit");
printf("\n************************\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter Rate for search>> ");
scanf("%d",&rate);
printbyRate(rec,rate,i);
getch();
break;
case 2:
fflush(stdin);
scanf("%c",&ch);
getch();
break;
case 0:
break;
}
}while(opt!=0);
}
}
void input(hotel *str,int n)
{
int j;
for(j=0;j<n;j++)
{
printf("\nInput for %d'st record\n",j+1);
printf("Enter Hotel name>> ");
fflush(stdin);
gets(str->name);
fflush(stdin);
fflush(stdin);
printf("Enter number of rooms>> ");
scanf("%d",&str->room);
printf("Enter average room charge>> ");
scanf("%d",&str->charge);
str++;
}
}
void printbyRate(hotel *str,int rate,int size)
{
int i;
for(i=0;i<size;i++)
{
if(str->charge<rate)
print(str);
str++;
}
}
{
hotel tmp[MAX],tv;
int i,j,k=0;
char g;
/* Saving data into array for sort */
for(i=0;i<s;i++)
{
{
strcpy(tmp[k].name,str->name);
tmp[k].room=str->room;
tmp[k].charge=str->charge;
k++;
}
str++;
}
/* Sorting with bubble sort */
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(tmp[j].charge>tmp[j+1].charge)
{
strcpy(tv.name,tmp[j].name);
tv.room=tmp[j].room;
tv.charge=tmp[j].charge;

strcpy(tmp[j].name,tmp[j+1].name);
tmp[j].room=tmp[j+1].room;
tmp[j].charge=tmp[j+1].charge;

strcpy(tmp[j+1].name,tv.name);
tmp[j+1].room=tv.room;
tmp[j+1].charge=tmp[j+1].charge;
}
}
}
for(i=0;i<k;i++)
{
printf("\nName=%s",tmp[i].name);