Hanwool Codes RSS Tag Admin Write Guestbook
C++ (10)
2023-02-10 02:45:54

Variable Sized Arrays | HackerRank

 

Variable Sized Arrays | HackerRank

Find the element described in the query for integer sequences.

www.hackerrank.com

 

Goal

 

Create N number of variable-length arrays and find a element from quaries

 

Solution

 

If you know vector in C++, it would be easy problem.

you initialize vector of vector a and insert variable sized vector into a.

 

# Variable Sized Arrays

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n = 0;
    int q = 0;
    
    cin >> n >> q;
    
     vector<vector<int>> a;
    
    for(int i = 0 ; i < n ; i++){
        int k = 0;
        
        cin >> k;
        
        vector<int> v;
        
        int value = 0;
        int index = 1;
        while(cin >> value){
            
            v.push_back(value);
            index++;
            
            if (index > k){
                break;
            }
            
        }
        
        a.push_back(v);
        
    }
    
    for(int i = 0; i < q ; i++){
        int index, j ;
        cin >> index >> j;
        cout << a[index][j] << endl;
    }
    
    
    return 0;
}
2023-02-09 05:43:55

Arrays Introduction | HackerRank

 

Arrays Introduction | HackerRank

How to access and use arrays. Print the array in the reverse order.

www.hackerrank.com

 

Goal

 

Print the N integers of the array in the reverse order, space-separated on a single line.

 

Solution

 

1. Create array with N size.

2. Save N integers into array.

3. Print reverse array with space

 

// Arrays Introduction

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    
    cin >> n;
    
    int a[n];
    int tmp;
    int i = 0;
    
    while (cin >> tmp)
    {
        a[i] = tmp;
        i++;
    }
    
    for(int index = n - 1; index >=0 ; index--){
        if(index !=0){
            cout << a[index] << " ";
        }
        else{
            cout << a[index] << endl;
        }
    }
    
    
    return 0;
}

 

 

2023-02-08 05:29:54

Pointer | HackerRank

 

Pointer | HackerRank

Learn how to declare pointers and use them.

www.hackerrank.com

 

Goal

 

By using pointer in C++, save a + b value into a and | a - b | into b.

 

 

Solution

 

This problem makes us to pratice pointer. 

You can save sum and abs_min of a and b with * sign in a and b, respectively. 

 

// Pointer

#include <stdio.h>
#include <cstdlib>


void update(int *a,int *b) {
    // Complete this function

    int sum = *a + * b;
    
    int abs_min = (*a) - (*b);
    *a = sum;
    *b = abs(abs_min);
        
}

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;
}
2023-02-04 04:29:14

Functions | HackerRank

 

Functions | HackerRank

Learn how to write functions in C++. Create a function to find the maximum of the four numbers.

www.hackerrank.com

 

 

Goal

 

Get four arguments and return the greatest of the four integers

 

 

Solution

 

Because you just need to return the greatest integer.

You can compare a and b first, then c and d.

If you find already greateest integer, you can simply return the value.

 

// Functions

#include <iostream>
#include <cstdio>
using namespace std;

/*
Add `int max_of_four(int a, int b, int c, int d)` here.
*/

int max_of_four(int a, int b, int c, int d){
    
    
    if(a >= b){
        if (a >= c){
            if (a >= d){
                return a;
            }
            else{
                return d;
            }
        }
            
        else{
            
            if( c>=d){
                return c;
            }
            
            else{
                return d;
            }
        }
    }
    
    else{
        
        if (b >= c){
            if (b >= d){
                return b;
            }
            else {
                return d;
            }
        }
        
        else{
            
            if (c >= d){
                return c;
            }
            
            else {
                return d;
            }
            
        }
        
    }
    
    return 0;
}



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;
}
2023-01-24 06:01:01

For Loop | HackerRank

 

For Loop | HackerRank

Learn how to use for loop and print the output as per the given conditions

www.hackerrank.com

 

Goal

 

Print results according to two positive input values by using for loop statement.

 

 

Solution

 

To solve easily, I create vector string array included 1-9 numbers. If you use vector array, you can print numbers easily.

 

// For Loop
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

int main() {
    // Complete the code.
    int a,b;
    cin >> a;
    cin >> b;
    
    vector<string> numbers {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    
    for(int i = a; i <= b; i++){
        
        if(i>9){
            if( i % 2 == 0){
                cout << "even" << endl;
            }
            else{
                cout << "odd" << endl;
            }
        }
        
        else{
            
            cout << numbers[i] << endl;
        }
    }
    
    return 0;
}
2023-01-24 05:55:31

https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem

 

Conditional Statements | HackerRank

Practice using chained conditional statements.

www.hackerrank.com

 

Goal

 

 

Solution

 

You just need to create condtion statements for each condition like below.

 

// Conditional Statements

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);



int main()
{
    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    // Write your code here
    if(n > 9){
        printf("Greater than 9");
    }
    else if(n == 1){
        printf("one");
    }
    else if(n == 2){
        printf("two");
    }
    else if(n == 3){
        printf("three");
    }
    else if(n == 4){
        printf("four");
    }
    else if(n == 5){
        printf("five");
    }
    else if(n == 6){
        printf("six");
    }
    else if(n == 7){
        printf("seven");
    }
    else if(n == 8){
        printf("eight");
    }
    else { // n == 9
        printf("nine");
    }

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}
2023-01-22 18:00:34

Basic Data Types | HackerRank

 

Basic Data Types | HackerRank

Learn about the basic data types in C++. Take the given input and print them.

www.hackerrank.com

 

Goal

1. Read space-seprated values : int, long, char, float and double, respectively

2. Print each element on a new line in the same order it was received as input. + correct decimal points of floating and double points.

 

 

Solution

Follow HackerRank explanation of c++ data types and their format specifiers.

// Basic Data Types

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    long b;
    char c;
    float d;
    double e;

    scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
    printf("%d\n", a);
    printf("%ld\n", b);
    printf("%c\n", c);
    printf("%f\n", d);
    printf("%lf\n", e);
    return 0;
}
2023-01-18 06:13:37

Input and Output | HackerRank

 

Input and Output | HackerRank

Learn to take in the input and print the output. Take three number as input and print their sum as output.

www.hackerrank.com

 

Goal

 

Read 3 numbers from stdin and print their sum to stdout

 

 

Solution

 

This challenge is to practice reading input from stdin(standard input stream) and printing out to stdout(standard output stream).

 

// Input and Output

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int a, b,c ;
    int result = 0;
    cin >> a >> b >> c;
    result = a + b + c;
    cout << result << endl;
    return 0;
}

 

 

 

2023-01-18 06:06:40

Say "Hello, World!" With C++ | HackerRank

 

Say "Hello, World!" With C++ | HackerRank

Practice printing to stdout.

www.hackerrank.com

 

Goal

 

Solution

 

This is simple challenge to print "Hello, World!" to stdout. We can use easily an object of class iostream. "std::cout."

 

// Say "Hello, World!" With C++

#include <iostream>
#include <cstdio>
using namespace std;

int main() {

    cout << "Hello, World!" << endl;
    return 0;
}

 

2023-01-06 06:43:57

Today, I want to show how to install visual studio code for C++ IDE.

My OS is Windows 10.

The reason why I choose Visual Studio Code is because it is open-source and fast interface.

 

We need to install software and build environment path but it is not so hard!

Then, let's start!

 

1. Install Visual Studio Code

 

Install Visual Studio Code this link(Download Visual Studio Code - Mac, Linux, Windows)

 

Download Visual Studio Code - Mac, Linux, Windows

Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows. Download Visual Studio Code to experience a redefined code editor, optimized for building and debugging modern web and cloud applications.

code.visualstudio.com

 

If you install Visual Studio Code, now we need to install extension for C++.

I installed "C/C++" and "C/C++ Extension  Pack" Extensions. The Extension window is located here.

 

If you still don't know how to install exntesion, please check this link.(Managing Extensions in Visual Studio Code)

 

Managing Extensions in Visual Studio Code

Discover, add, update, disable and uninstall Visual Studio Code extensions (plug-ins) through the Extension Marketplace.

code.visualstudio.com

You might need to install SDK too. Here is link for SDK for Visual Studio Code (Download .NET SDK for Visual Studio Code (microsoft.com))

 

Download .NET SDK for Visual Studio Code

Download and install the .NET SDK for building .NET apps with Visual Studio Code on Linux, macOS, or Windows.

dotnet.microsoft.com

 

2. Install a compiler

 

We also need to install a compiler. C++ is a compiled language so we must complie before it can be run in your machine. Unfortunately, VS code is an editor and does not include a C++ compiler or debugger. Therefore, you need to install a compiler additionally.

 

But, maybe you already have a compiler. Check if you have a compiler.

Please open Command Prompt in your machine and write this command.

This command is to check for the GCC compiler:

g++ --version

If you can see g++ version like below, you have already a compiler.

 

If you don't have any compiler, I recommend to you to install MinGW-x64 via MSYS2. The MSYS2 link is here(MSYS2)

 

 

MSYS2

Software Distribution and Building Platform for Windows

www.msys2.org

After you install MSYS2, please Run MSYS2 terminal. You need to install the full Mingw-w64 toolchain. So please write this command in MSYS2 terminal.

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

 

If you install all toolchain, now you need to add the MinGW compiler to your environment PATH.

Please follow these steps.

 

1. open Windows settings.

2. search for Edit environment variables for your account. 

3. Choose the Path variable in your User variables and then select Edit.

4. add your mingw-w64 installed path into Path. If you don't change any installation path, it should be C:\msys64\mingw64\bin.

5. Select OK.

 

Now, we can check your MinGW compiler.

Please open Command Prompt in your machine and write this command. 

gcc --version
g++ --version
gdb --version

If you can see all version, you succeed to install MinGW.

But if you cannot see all, please check your PATH correct and C:\msys64\mingw64\bin folder has files. If you don't have files in C:\msys64\mingw64\bin folder, you need to install full Mingw-w64 toolchain again.

 

3. Run Hello World

So now, we need to check the compiler is working fine.

Let's open VS code and create a folder HelloWord and create helloworld.cpp file.

I just created one more parent folder "C++". But it doesn't matter!

Now, please paste this script into your source code(helloworld.cpp) and save source code:

#include <iostream>

int main()
{
    std::cout << "Hello World" << std::endl;
}

Now, we need to build source code first as I explained above. Select the Terminal → Run Build Task from the main menu.

There are several build options. But we choose C/C++: g++.exe build active file option.

If your build finished successfully, you can see an executable file called helloworld.exe in File Explorer.

 

To run helloworld.exe, please open new terminal in VS code.

run your program by typing ".\helloworld.exe" in your terminal. If everything is fine, you should see the output "Hello World".

 

So today, we learn how to install visual studio code for C++.

 

If you have any question, please write a comment.

 



Hanwool Codes. Designed by 코딩재개발.