Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution for Hotel and prices | hackerrank |c++ #522

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions HackerRank/Classes and objects/classes and objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @file Box It!.cpp
* @author Vipul Kumar Singh
* @brief (https://www.hackerrank.com/challenges/classes-objects/problem)
* @version 0.1
* @date 2021-10-02
*
* @copyright Copyright (c) 2021
*
*/

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

// Write your Student class here
class Student
{
private :
int scores[5], sum = 0;

public :
void input()
{
for (int i = 0; i < 5; i++)
{
cin>>scores[i];
}
}
int calculateTotalScore()
{
for (int j = 0; j<5 ; j++)
{
sum = sum + scores[j];
}
return sum;
}
};

int main() {
int n; // number of students
cin >> n;
Student *s = new Student[n]; // an array of n students

for(int i = 0; i < n; i++){
s[i].input();
}

// calculate kristen's score
int kristen_score = s[0].calculateTotalScore();

// determine how many students scored higher than kristen
int count = 0;
for(int i = 1; i < n; i++){
int total = s[i].calculateTotalScore();
if(total > kristen_score){
count++;
}
}

// print result
cout << count;

return 0;
}
114 changes: 114 additions & 0 deletions HackerRank/Hotel Prices/Hotel Prices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @file "Hotel Prices.cpp"
* @author Vipul Kumar Singh
* @brief (https://www.hackerrank.com/challenges/hotel-prices/problem)
* @version 0.1
* @date 2021-10-02
*
* @copyright Copyright (c) 2021
*
*/

#include <iostream> // header files
#include <vector>

using namespace std;

class HotelRoom {

private:
/*
access specifier : private
it means that the variables declared in this access specifier private cannot be accessed
by the other class data members and functions

*/
int bedrooms_; // for the no. of bedrooms in class hotelroom
int bathrooms_; // for the no. of bathrooms in class hotelroom


public:
HotelRoom(int bedrooms, int bathrooms) // parameterized constructor for the assign some values to the variables
{
bedrooms_ = (bedrooms);
bathrooms_ = (bathrooms) ;

}

int get_price() // the function is used to calculate the price of the hotel
{
int room_price = 50*bedrooms_ + 100*bathrooms_ ;

return room_price;
}

};


/*
Creating new class called Hotel Apartment which inheriting from the base class HotelRoom
*/
class HotelApartment : public HotelRoom {
public:
HotelApartment(int bedrooms, int bathrooms) : HotelRoom(bedrooms + 2, bathrooms) {}

int get_price()
{
int hotel_price = ( HotelRoom::get_price()); // using the base class function called get_price() and storing them in hotel_price variable
return hotel_price ;
}
};

int main() {
int n;
cin >> n;
vector<HotelRoom*> rooms;
for (int i = 0; i < n; ++i)
{
string room_type;
int bedrooms;
int bathrooms;

cin >> room_type >> bedrooms >> bathrooms;

if (room_type == "standard")
{
rooms.push_back(new HotelRoom(bedrooms, bathrooms));
/*
dynamic memory allocation using new to create new object of HotelRoom class and
also call the parameterized constructor and
the data is stored in vector rooms
*/
}

else
{
rooms.push_back(new HotelApartment(bedrooms, bathrooms));
/*
dynamic memory allocation using new to create new object of HotelApartment class and
also call the parameterized constructor and
the data is stored in vector rooms
*/
}
}

int total_profit = 0;

for (auto room : rooms)
{
total_profit += room->get_price();
}

cout << total_profit << endl;

//deletion of objects that created by new operator for optimum performance

for (auto room : rooms)
{
delete room;
}

rooms.clear(); // eraseing the data from vector and size became 0

return 0;
}