Introduction:
Welcome to another challenge in the HackerRank 30 Days of Code series! In today's session, we're diving into the realm of data types. Understanding how to work with different data types is fundamental in programming. If you're new to this, don't worry! We'll guide you through the challenge, breaking down the steps to ensure you grasp the concepts.
The Challenge:
The task at hand involves completing the provided code. Three variables, i, d, and s, are already declared and initialized. Your mission is to:
Declare three new variables: one of type int, one of type double, and one of type string.
Read three lines of input from stdin and initialize your new variables.
Use the + operator to perform specific operations.
Print the results as specified in the Output Format section below.
Format & Sample:
Let's delve into the expected format and review a sample to better understand the challenge.
Input Format:
The first line contains an integer that you must sum with i.
The second line contains a double that you must sum with d.
The third line contains a string that you must concatenate with s.
Output Format:
Print the sum of both integers on the first line, the sum of both doubles (scaled to 1 decimal place) on the second line, and then the two concatenated strings on the third line.
Sample Input:
12
4.0
is the best place to learn and practice coding!
Sample Output:
16
8.0
HackerRank is the best place to learn and practice coding!
Explanation:
When we sum the integers i (4) and the input integer (12), we get the integer 16.
When we sum the floating-point numbers d (4.0) and the input double (4.0), we get 8.0.
When we concatenate s ("HackerRank ") with the input string ("is the best place to learn and practice coding!"), we get the full message.
Code Breakdown:
Now, let's explore the code provided and understand each section.
Reading Input:
In this section, we declare new variables (ii, dd, and ss) to store the input. The cin.ignore() is used to consume the newline character left by the previous cin, ensuring that getline works correctly.
Printing Output:
These lines print the desired output. The first line prints the sum of integers, the second line prints the sum of doubles with one decimal place, and the third line prints the concatenated string.
Final Code:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "HackerRank ";
// Declare second integer, double, and String variables.
int ii;
double dd;
string ss;
// Read and save an integer, double, and String to your variables.
cin >> ii;
cin >> dd;
cin.ignore(); // Consume the newline character left by previous cin
getline(cin, ss);
// Print the sum of both integer variables on a new line.
// Print the sum of the double variables on a new line.
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
cout << i + ii << endl;
cout << fixed << setprecision(1) << d + dd << endl;
cout << s + ss << endl;
return 0;
}
That concludes our exploration of the data types challenge. Understanding how to manipulate different data types is crucial for more complex programming tasks. Feel free to experiment with the code and explore further challenges on HackerRank to enhance your coding skills. Happy coding!