• Jun 17, 2024 •oceantran27
0 likes • 3 views
#include <iostream> using namespace std; int main { cout << 1; }
• Aug 5, 2023 •usama
1 like • 5 views
/* 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 value for(int i = 0; i < size; i++) { if(input[i] > 0) { sum += input[i]; n++; } } //If no numbers higher than 0, answer is 1 if(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 numbers cout << calcMissing(new int[1]{0}, 1) << endl; }
• Sep 7, 2022 •LeifMessinger
0 likes • 0 views
#include <iostream> #include <cstring> int main(int argc, char** argv){ //With decimal if(strstr(argv[1], ".") != nullptr){ int i = 0; //Skip i to first non 0 digit while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //If digit comes before decimal if((argv[1] + i) < strstr(argv[1], ".")){ //Good example of pointer arithmetic std::cout << strlen(argv[1] + i) - 1 << std::endl; //Another good example }else{ //If digit is after decimal std::cout << strlen(argv[1] + i) << std::endl; } }else{ //Without decimal int m = 0; int i = 0; while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //In case of some number like 0045 for(; argv[1][i] != '\0'; ++i){ if(argv[1][i] >= '1' && argv[1][i] <= '9') m = i + 1; } std::cout << m << std::endl; } return 0; }
• Nov 18, 2022 •AustinLeath
0 likes • 15 views
#include<iostream> using namespace std; const int rows = 8; const int cols = 8; char chessboard[rows][cols]; void setBoard(char chessboard[][cols]); void printBoard(char chessboard[][cols]); void setBoard(char chessboard[][cols]) { for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { if(i % 2 == 0 && j % 2 == 0) { chessboard[i][j] = 'x'; } else { if(i % 2 != 0 && j % 2 == 1) { chessboard[i][j] = 'x'; } else { chessboard[i][j] = '-'; } } } } return; } void printBoard(char chessboard[][cols]) { for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { cout << chessboard[i][j] << " "; } cout << endl; } return; } int main(int argc, char const *argv[]) { setBoard(chessboard); printBoard(chessboard); return 0; }
• Aug 31, 2020 •joshwrou
1 like • 3 views
#include <iostream> using namespace std; int main() { cout << "Hello World!\n"; // Prints out "Hello World" return 0; }
0 likes • 1 view
#include "goat.h" //include goat.h void Goat::setBreed(string breed) { this->breed = breed; } void Goat::setWeight(float weight) { this->weight = weight; } void Goat::setName(string name) { this->name = name; } void Goat::setGender(char gender) { this->gender = gender; } void Goat::setSpayed(bool goatIsSpayed) { this->goatIsSpayed = goatIsSpayed; } void Goat::setRegistrationID(string registrationID) { this->registrationID = registrationID; } void Goat::setColor(string color) { this->color = color; } void Goat::setOtherComments(string otherComments) { this->otherComments = otherComments; } string Goat::getBreed() { return breed; } float Goat::getWeight() { return weight; } string Goat::getName() { return name; } char Goat::getGender() { return gender; } bool Goat::getSpayed() { return goatIsSpayed; } string Goat::getRegistrationID() { return registrationID; } string Goat::getColor() { return color; } string Goat::getOtherComments() { return otherComments; } Goat::Goat() { breed = ""; weight = 0.0; name = ""; gender = '\0'; goatIsSpayed = false; registrationID = ""; color = ""; otherComments = ""; } Goat::Goat(string goatBreed, float goatWeight, string goatName, char goatGender, bool goatSpayedStatus, string goatRegistrationID, string goatColor, string goatOtherComments) { breed = goatBreed; weight = goatWeight; name = goatName; gender = goatGender; goatIsSpayed = goatSpayedStatus; registrationID = goatRegistrationID; color = goatColor; otherComments = goatOtherComments; } Goat::~Goat() { cout << "goat destroyed" << endl; } void Goat::printinfo() { cout << "Breed: " << breed << endl << "weight: " << weight << endl << "Name: " << name << endl << "Gender: " << gender << endl << "is Spayed: "; if(goatIsSpayed) { //here I do a logical test on boolean goatIsSpayed. if true cout << true else cout << false cout << "True"; } else { cout << "False"; } cout << endl << "Registration ID: " << registrationID << endl << "Color Description: " << color << endl << "Other Comments: " << otherComments << endl << endl; }