Friday, January 21, 2011

C language Structure program on hotel


About the program

Define a structure that can store details of a hotel. It should have structure members that include the name, address, grade, average room charge and no. of rooms.
Functions of this program will perform the following operations:
Display hotels of a given grade in order of charges.
Display hotels with room charges less than a given value.

Codes of the structure program on hotel

#include< stdio.h>
#include< string.h>
struct s
{
 char name[40],add[40],grade;
 float charge;
 int num;
};
void hot1(struct s *ob,char g,int n)
{
/* this function will display the hotel names of specified grade in order of charge.*/
 int i,j,x=0;
 char final[10][40],dummy[40];
 float r[10],t;
 for(i=0;i<n;i++)
 {
  if(g==ob[i].grade)
  {
   strcpy(final[x],ob[i].name);
   r[x]=ob[i].charge;
   x++;
   }
  }
  for(i=0;i<x;i++)
  {
   for(j=i+1;j<x;j++)
   {
    if(r[i]>r[j])
    {
     t=r[i];
     r[i]=r[j];
     r[j]=t;
     strcpy(dummy,final[i]);
     strcpy(final[i],final[j]);
     strcpy(final[j],dummy);
     }
    }
   }
  puts("Hotel names with specified GRADE in order of charge.");
  for(i=0;i<x;i++)
  {
   printf("\n%s%10.2f",final[i],r[i]);
   }
  }
  void hot2(struct s *ob,float r1,int n)
  {
  /* this function will display hotel names below the specified rate*/
  int i,x=0;
  char final[10][40];
  float r[10];
  for(i=0;i<n;i++)
  {
   if(r1>ob[i].charge)
   {
    r[x]=ob[i].charge;
    strcpy(final[x],ob[i].name);
    x++;
    }
   }
  puts("Hotel names below the specified RATE.");
  for(i=0;i<x;i++)
  {
   printf("\n%s%10.2f",final[i],r[i]);
   }
 }
 void main()
 {
  struct s hotel[10];
  int n,i;
  char g;
  float rate;
  clrscr();
  puts("Enter the  number of hotels(within 10):-");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
   fflush(stdin);
   puts("Enter the hotel name:-");
   gets(hotel[i].name);
   puts("Enter the hotel address:-");
   gets(hotel[i].add);
   puts("Enter the hotel grade:-('a','b','c')");
   scanf("%c",&hotel[i].grade);
   puts("Enter the room charge:-");
   scanf("%f",&hotel[i].charge);
   puts("Enter the room numbers:-");
   scanf("%d",&hotel[i].num);
   clrscr();
  }
  fflush(stdin);
  puts("Now we will search for specific hotels(Press any key)....");
  getch();
  clrscr();
  printf("\nEnter the grade for hotel('a','b','c'):-");
  scanf("%c",&g);
    hot1(hotel,g,n);
    printf("\nPress any key to continue....");
    getch();
  printf("\nEnter the charge:-");
  scanf("%f",&rate);
  puts("\n**************\n");
  hot2(hotel,rate,n);
  getch();
  }


Technical analysis of the above structure program

A structure is defined with number of structure member or elements to store the name of the hotel, address of the hotel, grade of hotels, number of rooms available and average rate.

Structure type array variable is created and the records are stored using for loop. Function ‘void hot1(struct s *,char,int)’ is used to display the hotel names of specified grade in order of charge.

The other function ‘void hot2(struct s *,float ,int )’ displays hotel names below the specified rate as entered by user.

2 comments:

  1. Hello there, i am a beginner i programming and i am currently working on my first project and i am struggling with the following question. Can any one out there please help or give me a hint?

    A guest house charges a $200.00 minimum fee per room for three days. The guest house charges
    an additional $50 per hour for each day in excess of three days. The maximum charge for any
    given week is $1000.00.Assume that no one is allowed to stay for longer than a week. Write a
    program that calculates and prints the charges for each of the 8 customers who rented 8 rooms in the
    previous week. You should enter the number of days rented for each customer. Your program
    should print the results in a neat tabular format and should calculate and print the total of last
    week’s receipts. The program should use the function calculateRental to determine the charge for
    each customer. Your output should appear in the following format:

    Enter days rented by eight customers: 2, 5, x...

    Customer Days Rent ($)
    1 2 200.0
    2 5 300.0
    8.. x.. y..
    Total 7 500.0

    ReplyDelete
  2. I think 'an additional $50 per hour' will be an additional $50 per per day, your sample calculation also shows it. Here is the program:

    #include< stdio.h>
    struct tag
    {
    int no;
    float rent;
    }obj;
    struct tag calculateRental(int day)
    {
    if(day< =3)
    {
    obj.no=day;
    obj.rent=200;
    }
    else if(day==7)
    {
    obj.no=7;
    obj.rent=1000;
    }
    else if(day >3 && day< 7)
    {
    obj.no=day;
    day=day-3;
    obj.rent=200+(day*50);
    }
    return obj;
    }

    void main()
    {
    int arr[10];
    float r=0;
    int d=0;
    struct tag ob;
    int i;
    for(i=0;i< 8;i++)
    {
    printf("Enter number of days booked by customer number %d:",(i+1));
    scanf("%d",&arr[i]);
    if(arr[i] >7)
    i--;
    }
    printf("\nHere is the bill\n");
    printf("\nCustomer No. Days Stayed Amount Rs.\n");
    for(i=0;i< 8;i++)
    {
    ob=calculateRental(arr[i]);
    printf("%d %d %.2f\n",(i+1),ob.no,ob.rent);
    r=r+ob.rent;
    d=d+ob.no;
    }
    printf("Total %d %.2f",d,r);
    getch();
    }

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner