• Nov 18, 2022 •AustinLeath
0 likes • 5 views
#include <iostream> using namespace std; /* Function: get_coeff Parameters: double& coeff, int pos passed from bb_4ac Return: type is void so no return, but does ask for user to input data that establishes what a b and c are. */ void get_coeff(double& coeff, int pos) { char position; if(pos == 1) { position = 'a'; } else if(pos == 2) { //a simple system to determine what coefficient the program is asking for. position = 'b'; } else { position = 'c'; } cout << "Enter the co-efficient " << position << ":"; //prompt to input coeff coeff = 5; //input coeff } /* Function: bb_4ac Parameters: no parameters passed from main, but 3 params established in function, double a, b, c. Return: b * b - 4 * a * c */ double bb_4ac() { double a, b, c; //coefficients of a quadratic equation get_coeff(a, 1); // call function 1st time get_coeff(b, 2); // call function 2nd time get_coeff(c, 3); // call function 3rd time return b * b - 4 * a * c; //return b * b - 4 * a * c } int main() { cout << "Function to calculate the discriminant of the equation. . . " << endl; double determinate = bb_4ac(); //assign double determinate to bb_4ac function cout << "The discriminant for given values is: " << determinate << endl; //output the determinate! }
• Dec 24, 2021 •aedrarian
3 likes • 21 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; }
• Jun 17, 2024 •oceantran27
0 likes • 3 views
#include <iostream> using namespace std; int main { cout << 1; }
• Jun 30, 2023 •Iceman_71
1 like • 7 views
// Iterative C++ program to // implement Stein's Algorithm //#include <bits/stdc++.h> #include <bitset> using namespace std; // Function to implement // Stein's Algorithm int 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 the greatest power of 2 that 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 code int main() { int a = 12, b = 780; printf("Gcd of given numbers is %d\n", gcd(a, b)); return 0; }
0 likes • 2 views
#include <iostream> #include "PlaylistNode.h" using namespace std; void PrintMenu(string title); int main() { string plTitle; cout << "Enter playlist's title:" << endl; getline(cin, plTitle); PrintMenu(plTitle); return 0; } void PrintMenu(string title) { Playlist list; string id; string sname; string aname; int length; int oldPos; int newPos; char choice; while(true) { cout << endl << title << " PLAYLIST MENU" << endl; cout << "a - Add song" << endl; cout << "d - Remove song" << endl; cout << "c - Change position of song" << endl; cout << "s - Output songs by specific artist" << endl; cout << "t - Output total time of playlist (in seconds)" << endl; cout << "o - Output full playlist" << endl; cout << "q - Quit" << endl << endl; cout << "Choose an option:" << endl; cin >> choice; cin.ignore(); if (choice == 'q') { exit(1); } else if (choice == 'a') { cout << "\nADD SONG" << endl; cout << "Enter song's unique ID: "; cin >> id; cin.ignore(); cout << "Enter song's name: "; getline(cin,sname); cout << "Enter artist's name: "; getline(cin,aname); cout << "Enter song's length (in seconds): "; cin >> length; list.AddSong(id, sname, aname, length); } else if (choice == 'd') { cout << "\nREMOVE SONG" << endl; cout << "Enter song's unique ID: "; cin >> id; list.RemoveSong(id); } else if (choice == 'c') { cout << "\nCHANGE POSITION OF SONG" << endl; cout << "Enter song's current position: "; cin >> oldPos; cout << "Enter new position for song: "; cin >> newPos; list.ChangePosition(oldPos, newPos); } else if (choice == 's') { cout << "\nOUTPUT SONGS BY SPECIFIC ARTIST" << endl; cout << "Enter artist's name: "; getline(cin, aname); list.SongsByArtist(aname); } else if (choice == 't') { cout << "\nOUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" << endl; cout << "Total time: " << list.TotalTime() << " seconds" << endl; } else if (choice == 'o') { cout << endl << title << " - OUTPUT FULL PLAYLIST" << endl; list.PrintList(); } else { cout << "Invalid menu choice! Please try again." << endl; } } }
• Apr 16, 2023 •LeifMessinger
0 likes • 0 views
#include <iostream> #include <string> //Should already be in iostream #include <cstdlib> //A word score adds up the character values. a-z gets mapped to 1-26 for the values of the characters. //wordScore [wordValue] //Pipe in the input into stdin, or type the words yourself. //Lowercase words only int characterValue(const char b){ return ((b >= 'a') && (b <= 'z'))? ((b - 'a') + 1) : 0; } int main(int argc, char** argv){ //The first argument specifies if you are trying to look for a certain word score int wordValue = (argc > 1)? std::atoi(argv[1]) : 0; std::string line; while(std::getline(std::cin, line)){ int sum = 0; for(const char c : line){ sum += characterValue(c); } if(wordValue){ //If wordValue is 0 or the sum is the correct value if(wordValue == sum){ std::cout << line << std::endl; } } else { std::cout << sum << "\t" << line << std::endl; } } return 0; }