Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Exceptional Server in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

Your friend set up a small computational server that performs complex calculations.
It has a function that takes 2 large numbers as its input and returns a numeric result. Unfortunately, there are various exceptions that may occur during execution.

Complete the code in your editor so that it prints appropriate error messages, should anything go wrong. The expected behavior is defined as follows:

  • If the compute function runs fine with the given arguments, then print the result of the function call.
  • If it fails to allocate the memory that it needs, print Not enough memory.
  • If any other standard C++ exception occurs, print Exception: S where S is the exception’s error message.
  • If any non-standard exception occurs, print Other Exception.

Input Format :

The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines describes a test case as 2 space-separated integers, A and B, respectively.

Constraints :

  • 1 <= T <= 10^3
  • 0 <= A,B <= 2^60

Output Format :

For each test case, print a single line containing whichever message described in the Problem Statement above is appropriate. After all messages have been printed, the locked stub code in your editor prints the server load.


Sample Input :

2
-8 5
1435434255433 5

Sample Output :

Exception: A is negative
Not enough memory
2

Explanation :

-8 is negative, hence ‘Exception: A is negative’ is thrown. Since the second input is too large, ‘not enough memory’ is displayed. 2 is the server load.


Solution :

//Exceptional Server in C++ - Hacker Rank Solution
#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;

class Server 
{
    private:
	    static int load;
    public:
	    static int compute(long long A, long long B) 
	    {
		    load += 1;
		    if(A < 0) 
		    {
			    throw std::invalid_argument("A is negative");
		    }
		    vector<int> v(A, 0);
		    int real = -1, cmplx = sqrt(-1);
		    if(B == 0) throw 0;
		    real = (A/B)*real;
		    int ans = v.at(B);
		    return real + A - B*ans;
	    }
	    static int getLoad() 
	    {
		    return load;
	    }
};
int Server::load = 0;

int main() 
{
	int T; cin >> T;
	while(T--) {
		long long A, B;
		cin >> A >> B;

		/* Enter your code here. */
        try 
        {
            cout << Server::compute(A, B) << endl;
        } 
        catch (std::bad_alloc& error) 
        {
            cout << "Not enough memory" << endl;
        }
        catch (std::exception& error) 
        {
            cout << "Exception: " << error.what() << endl;
        }
        catch (...) 
        {
            cout << "Other Exception" << endl;
        }
        /* ------------*/
	}
	cout << Server::getLoad() << endl;
	return 0;
}

Comments