0

I have written code for taking the input of the name, address, tariff, grade and number of rooms of any number of hotels. But now I'm stuck at a point where I have to take the input of a grade from the user and display the hotels in ascending order of price. I can successfully display the hotels of a given grade but I'm having trouble sorting the hotels according to price because this is the first time my code has gotten this long and confusing.

Here's my code so far:

#include <stdio.h>
#include <string.h>

//Structure definition starts
struct hotels
{
    char name[25];
    char address[300];
    int grade, avgCharge, rooms;
};
//Structure definition over

 //Main function starts
main()
{
    int i, j, k, hotels, choice, grade, temp;
    struct hotels a[50];
    //Declaration part over---------------------------------------------------------------------------

    printf("Enter the number of hotels\n");
    scanf("%d", &hotels);
    printf("\n\n");

    //Taken number of hotels for upcoming loops-------------------------------------------------------

    //for Loop for input of hotels and their details starts-------------------------------------------

    for (i = 0; i < hotels; i++)
    {
        fflush(stdin);
        printf("Enter the name of hotel %d\n", i + 1);
        gets(a[i].name);
        printf("\n");

        printf("Enter the address of %s\n", a[i].name);
        gets(a[i].address);
        printf("\n");

        printf("Enter the grade of the hotel from 1 to 5. (5 being the best)\n");
        scanf("%d", &a[i].grade);
        printf("\n");

        printf("Enter the average charge per night of the hotel.\n");
        scanf("%d", &a[i].avgCharge);
        printf("\n");

        printf("Enter the number of available rooms of the hotel.\n");
        scanf("%d", &a[i].rooms);
        printf("\n\n");
    }
    //Input part over, switch part starts-------------------------------------------------------------


    printf("\n\n");
    printf("Choose the task to perform-\n");
    printf("1. Show hotels of a particular grade from low to high price.\n");
    printf("2. Filter and show hotels under a particular tariff only.\n\n");

    scanf("%d", &choice);
    switch (choice)
    {
    case 1:
        printf("Enter the grade of the hotel to apply the list filter.\n");
        scanf("%d", &grade);
        printf("The following hotels were found- \n\n");
        for (i = 0; i < hotels; i++)
        {
            for (j = i + 1; j < hotels; j++) // Sorting according to price- Low to High
            {
                if (a[i].avgCharge > a[j].avgCharge)
                {
                    temp = a[i].avgCharge;
                    a[i].avgCharge = a[j].avgCharge;
                    a[j].avgCharge = temp;
                }
            }

            if (a[i].grade == grade) //Comparison for displaying the hotels of selected grade only.
            {
                printf("Hotel %d= %s\n", i + 1, a[i].name);
            }

        }
    }

}

Now, this works as expected so far, I just can't figure out how to display the hotels in ascending order of price. Any help will be greatly appreciated. Thanks!

AviusX
  • 374
  • 2
  • 12
  • 2
    Note: if you’re doing C, please don’t tag C++. It’s a different language altogether – Sami Kuhmonen Oct 15 '18 at 19:00
  • Your `main()` lacks a return type. – Swordfish Oct 15 '18 at 19:01
  • 1. break up your code into separate functions (this will make it less confusing). – tike Oct 15 '18 at 19:04
  • My professor told me to use a c++ compiler and the files are of cpp format so I think that's why it works. I don't know anything about c++ yet but I'm guessing that c works in cpp formats? I'm sorry I have no idea what I'm talking about. – AviusX Oct 15 '18 at 19:04
  • @tike What do you mean? – AviusX Oct 15 '18 at 19:06
  • 2. In order to achieve your goal, you will have to copy the hotels that match the given grade into a second array and then sort this second array by price, using a sort algorithm. https://en.wikipedia.org/wiki/Sorting_algorithm should get you started. – tike Oct 15 '18 at 19:06
  • @AviusX https://www.tutorialspoint.com/cprogramming/c_functions.htm – tike Oct 15 '18 at 19:07
  • `fflush(stdin);` plus `gets();` --> professor stuck in the past. See [Using fflush(stdin)](https://stackoverflow.com/q/2979209/2410359) and [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2410359) – chux - Reinstate Monica Oct 15 '18 at 19:24

2 Answers2

2

The problem is that when you find two items that needs to be swapped you are changing just their charge which is not correct. Instead you should swap whole items directly.

Something like this (only interested part):

if (a[i].avgCharge > a[j].avgCharge)
{
   hotels temp = a[i];
   a[i] = a[j];
   a[j] = temp;
}

Sidenote: I advice you to name your structure hotel, because that's what really is, no? Afterwards when you declare an array of hotel struct you can name that array hotels.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
1

Try replacing if (a[i].avgCharge > a[j].avgCharge) with if ((a[i].avgCharge > a[j].avgCharge) || (a[i].avgCharge == a[j].avgCharge && a[i].grade > a[j].grade))

Basically saying: You need to change the order of two hotels, during the sort, if either:

  • one is more expensive
  • they're both the same price, but one has higher rating

Edit: You'll need to break out the printing in a second for loop, after the sort is done. Edit 2: as @NiVeR says, you need to swap the whole hotels:

                temp = a[i];
                a[i] = a[j];
                a[j] = temp;

And a new type for temp.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42