Posts

Showing posts from August, 2020

Introduction to 2-D Array's

  #include <iostream> using namespace std ; int main (){ int a[ 100 ][ 100 ] ; int m , n ; cout << "Enter no. of rows : " ; cin >> m ; cout << "Enter no. of columns : " ; cin >> n ; cout << "Enter elements : " ; for ( int i = 0 ; i < m ; ++i) { for ( int j = 0 ; j < n ; ++j) { cin >> a[i][j] ; } } // Printing the given input // 1) Rowwise for ( int i = 0 ; i < m ; ++i) { for ( int j = 0 ; j < n ; ++j) { cout << a[i][j] << " " ; } cout << ' \n ' ; } // 2) Columnwise for ( int i = 0 ; i < n ; ++i) { for ( int j = 0 ; j < m ; ++j) { cout << a[j][i] << " " ; } cout << ' \n ' ; } return 0 ; }

Print all suffixes?

  #include <iostream> #include <cstring> using namespace std ; void print_all_suffixes ( char str[]){ for ( int e = strlen(str) ; e >= 0 ; --e) { for ( int s = strlen(str) ; s >= e ; --s) { cout << str[s] ; } cout << ' \n ' ; } } int main (){ char str[] = "abcd" ; print_all_suffixes(str) ; return 0 ; }

Print all prefixes?

#include <iostream> using namespace std ; void print_all_prefixes ( char str[]){ for ( int i = 0 ; str[i] != ' \0 ' ; ++i) { for ( int j = 0 ; j <= i ; ++j) { cout << str[j] ; } cout << ' \n ' ; } } int main (){ char str[] = "abcd" ; print_all_prefixes(str) ; return 0 ; }

CodeChef::MODEFREQ(incomplete)

  #include <iostream> using namespace std ; int main (){ int T ; cin >> T ; while (T --){ int N ; cin >> N ; int A[N] ; for ( int i = 0 ; i < N ; ++i) { cin >> A[i] ; } int temp[N] ; for ( int i = 0 ; i < N ; ++i) { temp[i] = 0 ; } int count{ 0 } ; for ( int i = 0 ; i < N ; ++i) { if (temp[i] == 0 ){ for ( int j = i ; j < N ; ++j) { if (A[j] == A[i]){ count = count + 1 ; temp[j] = 1 ; } } cout << count << ' \n ' ; count = 0 ; } } } return 0 ; }

CodeChef::FCTRL2(incomplete)

  #include <iostream> using namespace std ; int main (){ long long int t ; cin >> t ; long long int n[t] ; long long int fact{ 1 } ; for ( long long int i = 0 ; i < t ; ++i) { cin >> n[i] ; } for ( long long int i = 0 ; i < t ; ++i) { for ( long long int j = 1 ; j <= n[i] ; ++j) { fact = fact*j ; } cout << fact << ' \n ' ; fact = 1 ; } return 0 ; }

CodeChef::FLOW006

  # include <iostream> ; using namespace std ; int main (){ int T ; int sum{ 0 } ; cin >> T ; int N[T] ; for ( int i = 0 ; i < T ; ++i) { cin >> N[i] ; } for ( int i = 0 ; i < T ; ++i) { while (N[i] != 0 ){ sum = sum + N[i]% 10 ; N[i] = N[i]/ 10 ; } cout << sum << ' \n ' ; sum = 0 ; } return 0 ; }

Reverse a String ??

#include <iostream> using namespace std ; //Instead of using length functon you can use a inbuilt function called 'strlen' but if you want to //use it then you must include <cstring> library. Program will become lot easier. int length ( char input[]) { int count{ 0 } ; for ( int i = 0 ; input[i] != ' \0 ' ; ++i) { count++ ; } return count ; } void reverse ( char input[]){ int start{ 0 } ; int end{length(input) - 1 } ; while (start < end){ swap(input[start] , input[end]) ; start ++ ; end -- ; } } int main (){ char name[ 100 ] ; cout << "Enter string : " ; cin.getline(name , 100 ) ; reverse(name) ; cout << "Reversed string is " << name ; }

Length of the string ?

#include <iostream> using namespace std ; //Instead of using length functon you can use a inbuilt function called 'strlen' but if you want to //use it then you must include <cstring> library. Program will become lot easier. int length ( char input[]) { int count{ 0 } ; for ( int i = 0 ; input[i] != ' \0 ' ; ++i) { count ++ ; } return count ; } int main (){ char name[ 100 ] ; cout << "Enter your name :" ; cin >> name ; cout << length(name) ; return 0 ; }

Code Chef:: FLOW002

  #include <iostream> using namespace std ; int main (){ int T ; int A ; int B ; cin >> T ; int remainder[T] ; for ( int i = 0 ; i < T ; ++i) { cin >> A ; cin >> B ; remainder[i] = A%B ; } for ( int i = 0 ; i < T ; ++i) { cout << remainder[i] << ' \n ' ; } return 0 ; }

Reversing The Array(Diffrent from "Printing Array in Reverse Order")

  # include <iostream> using namespace std ; int main (){ int length_of_array ; cout << "Enter the no. of Elements :" ; cin >> length_of_array ; int array[length_of_array] ; for ( int i = 0 ; i < length_of_array ; i++) { cin >> array[i] ; } cout << "Before Reversing : " << ' \n ' ; for ( int i = 0 ; i < length_of_array ; i++) { cout << array[i] << " " ; } cout << ' \n ' ; int start{ 0 } ; int end{length_of_array - 1 } ; while (start <= end){ swap(array[start] , array[end]) ; start ++ ; end -- ; } cout << "After Reversing : " << ' \n ' ; for ( int i = 0 ; i < length_of_array ; ++i) { cout << array[i] << " " ; } return 0 ; }

Printing Array In Reverse Order

  # include <iostream> using namespace std ; int main (){ int length_of_array ; cout << "Enter the no. of Elements :" ; cin >> length_of_array ; int array[length_of_array] ; for ( int i = 0 ; i < length_of_array ; ++i) { cin >> array[i] ; } cout << "Elements from Left to Right : " << ' \n ' ; for ( int i = 0 ; i < length_of_array ; i++) { cout << array[i] << " " ; } cout << ' \n ' ; cout << "Elements from Right to Left : " << ' \n ' ; for ( int i = length_of_array - 1 ; i >= 0 ; i--) { cout << array[i] << " " ; } return 0 ; }

Swapping given variables

Image
 

Finding Largest and Smallest Element in Array

  # include <iostream> using namespace std ; int main (){ int length_of_array ; cout << "Enter the no. of Elements :" ; cin >> length_of_array ; int array[length_of_array] ; for ( int i = 0 ; i < length_of_array ; ++i) { cin >> array[i] ; } int largest{ INT16_MIN } ; for ( int i = 0 ; i < length_of_array ; ++i) { if (array[i] > largest) { largest = array[i] ; } } int smallest{ INT32_MAX } ; for ( int i = 0 ; i < length_of_array ; ++i) { if (array[i] < smallest){ smallest = array[i] ; } } cout << "Largest no. in the array is " << largest << ' \n ' ; cout << "Smallest no. in the array is " << smallest << ' \n ' ; return 0 ; }

Sum of Array Elements

  # include <iostream> using namespace std ; int main (){ int length_of_array ; cout << "Enter the no. of elements :" ; cin >> length_of_array ; int array[length_of_array] ; for ( int i = 0 ; i < length_of_array ; ++i) { cin >> array[i] ; } int sum{ 0 } ; for ( int i = 0 ; i < length_of_array ; ++i) { sum = sum + array[i] ; } cout << sum ; return 0 ; }

August cookoff::CHEFNWORK (Incomplete)

#include <iostream> using namespace std ; int main (){ int t ; cin >> t ; while (t --){ int n ; int k ; cin >> n >> k ; int w[n] ; for ( int i = 0 ; i < n ; ++i) { cin >> w[i] ; } int sum{ 0 } ; int count{ 1 } ; bool check{ false } ; for ( int i = 0 ; i < n ; ++i) { if (w[i] > k){ check = true ; } } if (check){ cout << - 1 << ' \n ' ; } } return 0 ; }

Code Chef :: START01

 #include <iostream> int main() {     int x ;     std::cin >> x ;     std::cout << x ;     return 0; }

Code Chef:: FLOW001

#include <iostream> using namespace std ; int main (){ int t ; cin >> t ; int a[t] ; int b[t] ; for ( int i = 0 ; i < t ; ++i) { cin >> a[i] >> b[i] ; } for ( int i = 0 ; i < t ; ++i) { cout << a[i] + b[i] << ' \n ' ; } return 0 ; }

Code Chef :: Enormous input test

 #include <iostream> int main() {     int n;     int k;     std::cin >> n;     std::cin >> k;     int tot{};     for (int i = 0; i<n ; i++)     {         long long int t ;         std::cin >> t ;         if (t%k == 0)         {             tot++ ;             }         else         {             tot = tot ;         }     }     std::cout << tot ;     return 0; }

Code chef : ATM problem

// CODE: HS08TEST // logic // the withdrawal amount should be a multiple of 5 // the bank balance should not be equal to the // withdrawal amount because for every transaction // the bank charges 0.50 USD. // the withdrawal amount should be an int whereas // the bank balance and remaining balance after // transaction should be float with 2 digits // precision.  #include <iostream> int main() {     int x;     float y;     float z{0.5};     std::cin >> x ;     std::cin >> y ;     if (x%5 == 0 && x<y && (x + 0.5) <= y)     {         std::cout << double(y - x - z);     }     else     {         std::cout << double(y);     }     return 0; }

Calculator in C++

 #include <iostream> int main() {    int num1 ;    int num2 ;    char op ;    std::cout << "What would you like to do with two numbers (Give symbols like addition(+),substraction(-))";    std::cin >> op;    std::cout << "Enter first number : ";    std::cin >> num1;    std::cout << "Enter second number : ";    std::cin >> num2;    int result;    if (op == '+')    {        result = num1 + num2;    }    else if (op == '-')    {        result = num1 - num2;    }    else if (op == '*')    {        result = num1 * num2;    }    else if (op == '/')    {        result = num1 / num2;    }    else {        std::cout << "Invalid Operator";    }    std::cout << "Your answer is " << result ; }