Loading...
More C++ Posts
#include <iostream>using namespace std;int main() {const int ROW_SIZE = 2;const int COLUMN_SIZE = 5; //establish all variablesint matrix[ROW_SIZE][COLUMN_SIZE];int minVal;for (int i = 0; i < ROW_SIZE; ++i) // for loop to ask user to enter data.{for (int h = 0; h < COLUMN_SIZE; ++h) {cout << "Enter data for row #" << i + 1 << " and column #" << h + 1 << ": ";cin >> matrix[i][h];}}cout << "You entered: " << endl;for (int i = 0; i < ROW_SIZE; ++i) //for statements to output the array neatly{for (int h = 0; h < COLUMN_SIZE; ++h) {cout << matrix[i][h] << "\t";}cout << endl;}cout << "Minimum for each row is: {";for (int i = 0; i < ROW_SIZE; i++) //for statements to find the minimum in each row{minVal = matrix[i][0];for (int h = 0; h < COLUMN_SIZE; h++) {if (matrix[i][h] < minVal) // if matrix[i][h] < minVal -> minVal = matrix[i][h];{minVal = matrix[i][h];}}cout << minVal << ", ";}cout << "}" << endl;cout << "Minimum for each column is: {";for (int i = 0; i < COLUMN_SIZE; i++) //for statements to find the minimum in each column{minVal = matrix[0][i];for (int h = 0; h < ROW_SIZE; h++) {if (matrix[h][i] < minVal) //replaces minVal with array index for that column that is lowest{minVal = matrix[h][i];}}cout << minVal << ", ";}cout << "}" << endl;return 0;}
#include <iostream>#include <cmath>#include <string.h>using namespace std;int main() {string tickerName;int numOfContracts;float currentOptionValue;cout << "Enter a stock ticker: ";getline(cin, tickerName);cout << "Enter the current number of " << tickerName << " contracts you are holding: ";cin >> numOfContracts;cout << "Enter the current price of the option: ";cin >> currentOptionValue;cout << "The value of your " << tickerName << " options are: $" << (currentOptionValue * 100.00) * (numOfContracts);cout << endl;return 0;}
/*Good morning! Here's your coding interview problem for today.This problem was asked by Stripe.Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.You can modify the input array in-place.*/#include <iostream>using namespace std;int calcMissing(int* input, int size){int sum = 0;int n = 1; //add one to account for missing valuefor(int i = 0; i < size; i++){if(input[i] > 0){sum += input[i];n++;}}//If no numbers higher than 0, answer is 1if(sum == 0)return 1;return (n*(n+1)/2) - sum; //Formula is expectedSum - actualSum/* expectedSum = n*(n+1)/2, the formula for sum(1, n) */}int main(){cout << calcMissing(new int[4]{3, 4, -1, 1}, 4) << endl;cout << calcMissing(new int[3]{1, 2, 0}, 3) << endl;//No positive numberscout << calcMissing(new int[1]{0}, 1) << endl;}
#include <iostream>#include <cstring>int main(int argc, char** argv){//With decimalif(strstr(argv[1], ".") != nullptr){int i = 0;//Skip i to first non 0 digitwhile(argv[1][i] < '1' || argv[1][i] > '9') ++i;//If digit comes before decimalif((argv[1] + i) < strstr(argv[1], ".")){ //Good example of pointer arithmeticstd::cout << strlen(argv[1] + i) - 1 << std::endl; //Another good example}else{//If digit is after decimalstd::cout << strlen(argv[1] + i) << std::endl;}}else{//Without decimalint m = 0;int i = 0;while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //In case of some number like 0045for(; argv[1][i] != '\0'; ++i){if(argv[1][i] >= '1' && argv[1][i] <= '9') m = i + 1;}std::cout << m << std::endl;}return 0;}
//Constant prefix notation solver using bruh//Could make it infix or postfix later#include<string>#include<vector>#include<iostream>std::vector<long double> bruhBuff;long double operator ""bruh(long double a){bruhBuff.push_back(a);return a;}long double operator ""bruh(const char op){if(bruhBuff.size() < 2) throw "Bruh weak";long double b = bruhBuff.back();bruhBuff.pop_back();long double a = bruhBuff.back();bruhBuff.pop_back();switch(op){case (int)('+'):return a + b;case (int)('-'):return a - b;case (int)('*'):return a * b;case (int)('/'):return a / b;}return 69l;}int main(){1.0bruh;2.0bruh;std::cout << '+'bruh << std::endl;return 0;}
// Iterative C++ program to// implement Stein's Algorithm//#include <bits/stdc++.h>#include <bitset>using namespace std;// Function to implement// Stein's Algorithmint gcd(int a, int b){/* GCD(0, b) == b; GCD(a, 0) == a,GCD(0, 0) == 0 */if (a == 0)return b;if (b == 0)return a;/*Finding K, where K is thegreatest power of 2that divides both a and b. */int k;for (k = 0; ((a | b) & 1) == 0; ++k){a >>= 1;b >>= 1;}/* Dividing a by 2 until a becomes odd */while ((a & 1) == 0)a >>= 1;/* From here on, 'a' is always odd. */do{/* If b is even, remove all factor of 2 in b */while ((b & 1) == 0)b >>= 1;/* Now a and b are both odd.Swap if necessary so a <= b,then set b = b - a (which is even).*/if (a > b)swap(a, b); // Swap u and v.b = (b - a);} while (b != 0);/* restore common factors of 2 */return a << k;}// Driver codeint main(){int a = 12, b = 780;printf("Gcd of given numbers is %d\n", gcd(a, b));return 0;}