Hanwool Codes RSS Tag Admin Write Guestbook
C++ Challenge (3)
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;
}


Hanwool Codes. Designed by 코딩재개발.