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, ...

Preprocessor Solution in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

Preprocessor directives are lines included in the code preceded by a hash sign (#). These lines are directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

#define INF 10000000
if( val == INF) {
//Do something
}
After the preprocessor has replaced the directives, the code will be
if( val == 10000000) { //Here INF is replaced by the value with which it's defined.
//Do something
}

h
You can also define function macros which have parameters.

#define add(a, b) a + b
int x = add(a, b);

The second statement after the preprocessor has replaced the directives will be:
int x = a + b;

To know more about preprocessor directives, you can go to this link

You’re spending your afternoon at a local school, teaching kids how to code. You give them a simple task: find the difference between the maximum and minimum values in an array of integers.
After a few hours, they came up with some promising source code. Unfortunately, it doesn’t compile! Since you don’t want to discourage them, you decide to make their code work without modifying it by adding preprocessor macros.
Review the locked stub code in your editor and add the preprocessor macros necessary to make the code work.


Input Format :

The first line contains an integer, N, denoting the size of the array.
The second line contains N space-separated integers x1, x2……, xn describing the elements in the array.

Constraints :

  • 1<= N <= 10^3
  • -10^3 <= xi <= 10^8

Output Format :

You are not responsible for printing any output to stdout. Once the necessary preprocessor macros are written, the locked code in your editor will print a line that says Result = Z, where Z is the difference between the maximum and minimum values in the array.


Sample Input :

5
32 332 -23 -154 65

Sample Output :

Result = 486

Explanation :

332 – (- 154) = 486


Solution :

//Preprocessor Solution in C++ - Hacker Rank Solution
/* Preprocessor Solution in C++ - Hacker Rank Solution START */
#define FUNCTION(name, operator) void name(int &current, int candidate){ !(current operator candidate) ? current = candidate : false; }
#define foreach(v, i) for(int i = 0; i < v.size(); i++)
#define io(v) cin >> v
#define INF 10000000
#define toStr(S) #S
/* Preprocessor Solution in C++ - Hacker Rank Solution END */
#include <iostream>
#include <vector>
using namespace std;

#if !defined toStr || !defined io || !defined FUNCTION || !defined INF
#error Missing preprocessor definitions
#endif 

FUNCTION(minimum, <)
FUNCTION(maximum, >)

int main(){
	int n; cin >> n;
	vector<int> v(n);
	foreach(v, i) {
		io(v)[i];
	}
	int mn = INF;
	int mx = -INF;
	foreach(v, i) {
		minimum(mn, v[i]);
		maximum(mx, v[i]);
	}
	int ans = mx - mn;
	cout << toStr(Result =) <<' '<< ans;
	return 0;

}

Comments