Introduction Challenges C++

As I mentioned in a previous post, I will be solving challenges using C++ and Java. Earlier today I found in HackerRank a set of challenges for C++. The set seems to be split into the following categories:

  • Introduction
  • Strings
  • Classes
  • STL
  • Inheritance
  • Other Concepts

As usual, I like to start simple and progress towards the most complicated. In this blog entry I cover my solutions written in C++ using Microsoft Visual Studio 2013 Professional.

Challenge: Say “Hello, World!” with C++

The solution was already in the buffer:

#include <iostream>

#include <cstdio>

using namespace std;

int main() {

printf(“Hello, World!”);

return 0;

}

Challenge: Input and Output

#include <iostream>

using namespace std;

/*

*/

int main () {

int a,

b,

c;

// **** input the values ****

cin >> a >> b >> c;

// **** compute and display sum ****

int sum = a + b + c;

cout << sum << endl;

// **** ****

return 0;

}

Challenge: Basic Data Types

#include <iostream>

using namespace std;

/*

*/

int main () {

int           anInt;

long          aLong;

long long     aLongLong;

char          aChar;

float         aFloat;

double        aDouble;

// **** ****

int status = scanf(  “%d %ld %lld %c %f %lf”,

&anInt, &aLong, &aLongLong, &aChar, &aFloat, &aDouble);

// **** ****

status = printf(     “%d\n%ld\n%lld\n%c\n%.3f\n%.9lf\n”,

anInt, aLong, aLongLong, aChar, aFloat, aDouble);

// **** ****

return 0;

}

Challenge: Conditional Statements

#include <iostream>

using namespace std;

/*

*/

int main () {

// **** ****

int n;

// **** ****

cin >> n;

// **** ****

if (n == 1) {

cout << “one” << endl;

} else if (n == 2) {

cout << “two” << endl;

} else if (n == 3) {

cout << “three” << endl;

} else if (n == 4) {

cout << “four” << endl;

} else if (n == 5) {

cout << “five” << endl;

} else if (n == 6) {

cout << “six” << endl;

} else if (n == 7) {

cout << “seven” << endl;

} else if (n == 8) {

cout << “eight” << endl;

} else if (n == 9) {

cout << “nine” << endl;

} else {

cout << “Greater than 9” << endl;

}

// **** ****

return 0;

}

Challenge: For Loop

#include <iostream>

using namespace std;

/*

*/

int main () {

// **** ****

int a,

b;

// **** ****

cin >> a;

cin >> b;

// **** ****

for (int n = a; n <= b; n++) {

if (n == 1)

{

cout << “one” << endl;

}

else if (n == 2)

{

cout << “two” << endl;

}

else if (n == 3)

{

cout << “three” << endl;

}

else if (n == 4)

{

cout << “four” << endl;

}

else if (n == 5)

{

cout << “five” << endl;

}

else if (n == 6)

{

cout << “six” << endl;

}

else if (n == 7)

{

cout << “seven” << endl;

}

else if (n == 8)

{

cout << “eight” << endl;

}

else if (n == 9)

{

cout << “nine” << endl;

}

else if ((n % 2) == 1)

{

cout << “odd” << endl;

} else {

cout << “even” << endl;

}

}

// **** ****

return 0;

}

Challenge: Functions

#include <iostream>

using namespace std;

/*

*/

int max_of_four(int a, int b, int c, int d) {

int max       = a;

if (b >= max) {

max = b;

}

if (c >= max) {

max = c;

}

if (d >= max) {

max = d;

}

return max;

}

/*

*/

int main () {

int a, b, c, d;

scanf(“%d %d %d %d”, &a, &b, &c, &d);

int ans = max_of_four(a, b, c, d);

printf(“%d”, ans);

// **** ****

return 0;

}

Challenge: Pointer

#include <iostream>

using namespace std;

/*

*/

void update(int *a, int *b) {

int sum       = *a + *b;

int dif = *a – *b;

*a = sum;

*b = (dif >= 0) ? dif : -dif;

}

/*

*/

int main () {

int a, b;

int *pa = &a, *pb = &b;

scanf(“%d %d”, &a, &b);

update(pa, pb);

printf(“%d\n%d”, a, b);

// **** ****

return 0;

}

Challenge: Arrays Introduction

#include <iostream>

using namespace std;

/*

*/

int main () {

// **** ****

int N = 0;

cin >> N;

int arr[1000];

// **** ****

for (int i = 0; i < N; i++) {

cin >> arr[i];

}

// **** ****

for (int i = (N – 1); i >= 0; i–) {

cout << arr[i] << ” “;

}

cout << endl;

// **** ****

return 0;

}

There are a couple more challenges in the first set. Will see if I have some time at the end of the day; otherwise will attempt to solve them tomorrow morning.

If you have comments or questions regarding this or any other post in this blog, please do not hesitate and send me a message via email. I will not mention your name unless you explicitly tell me.

John

john.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.