Posts

Showing posts from 2020

Hackerrank -- C++ - Variable Sized Array Solution

#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 ,   q ;      cin   >>   n   >>   q ;      int  ** arr   =   new   int *[ n ];      for   (   int   i   =   0 ,   k   ;   i   <   n   ;   ++   i   )      {          cin   >>   k ;                   arr [ i ]   =   new   int [ k ];                   for   (   int   j   =   0   ;   j   <   k   ;   ++   j   )          {              cin   >>   arr [ i ][ j ];          }      }           for   (   int   i   =   0 ,   a ,   b   ;   i   <   q   ;   ++   i   )      {          cin   >>   a   >>   b ;                   cout   <<   arr [ a ][ b ]   <<   endl ;      }           return   0 ; }

Hackerrank C++ -------- Array Introduction

#include   < iostream > using   namespace   std ;    int   main (){      int   n   ,   arr [ 1000 ]   ,   i   ,   j   ,   g   ;      cin >>   n   ;         g   =   n    ;             for   (   i   =   0   ;   i < n   ;   i ++)   {          cin   >>   arr [ i ]   ;        }      for   (   j   =   g - 1   ;   j   >=   0   ;   j --   ){          cout   <<   arr [ j ]   <<   " "   ;        }        return   0 ; }

Hackerrank C++ ---- Pointer

#include   < stdio.h > #include< stdlib.h > void   update ( int   * a , int   * b )   {      // Complete this function          int   tmp ;      tmp =* a +* b ;      * b = abs (* a -* b );      * a = tmp ; } 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 ; }

Hackerrank C++ --- Functions

#include <iostream> #include <cstdio> using namespace std; int main() {     int a, b, c, d;     cin>>a ; cin>> b ; cin>>c ;cin>> d ;     int   e , f ;   // variable = Expression1 ? Expression2 : Expression3   e = a>b ? a : b ;   f = c > d ? c:d ;   int  ans = e > f ? e : f ;   cout<<ans<< endl ;                return 0; }

Hacerrank C++ --- For Loop

#include   < iostream > #include   < cstdio > using   namespace   std ; int   main ()   {      // Complete the code.      int   a   ,   b    ,   g   ;      cin >>   a   ;      cin >>   b   ;        for   (   g   =   a   ;   g <= b   ;   g ++)   {          if ( g == 1 ){              cout   <<   "one"   <<   endl ;          }          else   if ( g == 2 )   {              cout   <<   "two"   <<   endl ;          }          else   if ( g == 3 )   {              cout   <<   "three"   <<   endl ;          }          else   if ( g == 4 )   {              cout   <<   "four"   <<   endl ;          }          else   if ( g == 5 )   {              cout   <<   "five"   <<   endl ;          }          else   if ( g == 6 )   {              cout   <<   "six"   <<   endl ;          }          else   if ( g == 7 )   {              cout   <&l

Hackerrrank : Input and Output (C++)

Image
SOLUTION C++ :  #include   < cmath > #include   < cstdio > #include   < vector > #include   < iostream > #include   < algorithm > using   namespace   std ; int   main ()   {      /* Enter intyour code here. Read input from STDIN. Print output to STDOUT */          int   a , b , c , d   ;      cin >> a   ;   cin >> b   ;   cin >>   c   ;      d   =   a   + b   +   c   +   d   ;      cout << d   ;        return   0   ; } Objective In this challenge, we're practicing reading input from stdin and printing output to stdout. In C++, you can read a single whitespace-separated token of input using  cin , and print output to stdout using  cout . For example, let's say we declare the following variables: string s ; int n ; and we want to use  cin  to read the input "High 5" from stdin. We can do this with the following code: cin >> s >> n ; The above code reads the first word ("High") from stdi

CodeChef ATM Problem Code: HS08TEST

Image
SOLUTION C++ : #include<iostream> #include<iomanip> using namespace std; int main() { int a; //a-amounttodebit float b ; float c; std::cin >> a; std::cin >> b; if(( (a+0.50) < b) && ((a) % 5==0) ) { c = b - (a+0.50) ; cout<<fixed<<setprecision(2)<<c<<endl; } else { //std::cout << b << std::endl; cout<<fixed<<setprecision(2)<<b<<endl; } } QUESTION Pooja would like to withdraw  X  $US from an ATM. The cash machine will only accept the transaction if  X  is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction. Input Positive integer 0 <  X  <= 2000 - the amount of cash which Pooja wishes to withdraw. Nonnegative number 0<=  Y  <= 200