• Nov 18, 2022 •AustinLeath
0 likes • 0 views
using namespace std; class Hash { int BUCKET; // No. of buckets // Pointer to an array containing buckets list<int> *table; public: Hash(int V); // Constructor // inserts a key into hash table void insertItem(int x); // deletes a key from hash table void deleteItem(int key); // hash function to map values to key int hashFunction(int x) { return (x % BUCKET); } void displayHash(); }; Hash::Hash(int b) { this->BUCKET = b; table = new list<int>[BUCKET]; } void Hash::insertItem(int key) { int index = hashFunction(key); table[index].push_back(key); } void Hash::deleteItem(int key) { // get the hash index of key int index = hashFunction(key); // find the key in (inex)th list list <int> :: iterator i; for (i = table[index].begin(); i != table[index].end(); i++) { if (*i == key) break; } // if key is found in hash table, remove it if (i != table[index].end()) table[index].erase(i); } // function to display hash table void Hash::displayHash() { for (int i = 0; i < BUCKET; i++) { cout << i; for (auto x : table[i]) cout << " --> " << x; cout << endl; } } // Driver program int main() { // array that contains keys to be mapped int a[] = {15, 11, 27, 8, 12}; int n = sizeof(a)/sizeof(a[0]); // insert the keys into the hash table Hash h(7); // 7 is count of buckets in // hash table for (int i = 0; i < n; i++) h.insertItem(a[i]); // delete 12 from hash table h.deleteItem(12); // display the Hash table h.displayHash(); return 0; }
• Sep 7, 2022 •LeifMessinger
#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; }
0 likes • 1 view
/* Algorithm: Step 1: Get radius of the cylinder from the user and store in variable r Step 2: Get height of the cylinder from the user and store in variable h Step 3: Multiply radius * radius * height * pi and store in v Step 4: Display the volume */ #include <iostream> using namespace std; int main() { float r; //define variable for radius float h; //define variable for height float v; float pi; pi=3.1416; cout<<"Enter radius:"; cin>>r; cout<<"Enter height:"; cin>>h; v=r*r*h*pi; //compute volume cout<<"Radius:"<<r<<"\tHeight:"<<h<<endl; //display radius and height cout<<"\n************************\n"; cout<<"Volume:"<<v<<endl;//display volume return 0; }
• Oct 23, 2022 •LeifMessinger
//Leif Messinger //Finds all sets of 5 5 letter words that don't have duplicate letters in either themselves or each other. //First it reads the words in and puts them in groups of their bitmasks //After that, we recurse on each group. Before doing that, we remove the group from the set of other groups to check it against. #include <cstdio> //getchar, printf #include <cassert> //assert #include <vector> #include <set> #include <algorithm> //std::copy_if #include <iterator> //std::back_inserter #define CHECK_FOR_CRLF true #define MIN_WORDS 5 #define MAX_WORDS 5 #define WORD_TOO_LONG(len) (len != 5) const unsigned int charToBitmask(const char bruh){ assert(bruh >= 'a' && bruh <= 'z'); return (1 << (bruh - 'a')); } void printBitmask(unsigned int bitmask){ char start = 'a'; while(bitmask != 0){ if(bitmask & 1){ putchar(start); } bitmask >>= 1; ++start; } } //Pointer needs to be deleted const std::set<unsigned int>* getBitmasks(){ std::set<unsigned int>* bitmasksPointer = new std::set<unsigned int>; std::set<unsigned int>& bitmasks = (*bitmasksPointer); unsigned int bitmask = 0; unsigned int wordLength = 0; bool duplicateLetters = false; for(char c = getchar(); c >= 0; c = getchar()){ if(CHECK_FOR_CRLF && c == '\r'){ continue; } if(c == '\n'){ if(!(WORD_TOO_LONG(wordLength) || duplicateLetters)) bitmasks.insert(bitmask); bitmask = 0; wordLength = 0; duplicateLetters = false; continue; } if((bitmask & charToBitmask(c)) != 0) duplicateLetters = true; bitmask |= charToBitmask(c); ++wordLength; } return bitmasksPointer; } void printBitmasks(const std::vector<unsigned int>& bitmasks){ for(unsigned int bruh : bitmasks){ printBitmask(bruh); putchar(','); putchar(' '); } puts(""); } //Just to be clear, when I mean "word", I mean a group of words with the same letters. void recurse(std::vector<unsigned int>& oldBitmasks, std::vector<unsigned int> history, const unsigned int currentBitmask){ //If there's not enough words left if(oldBitmasks.size() + (-(history.size())) + (-MIN_WORDS) <= 0){ //If there's enough words if(history.size() >= MIN_WORDS){ //Print the list printBitmasks(history); } return; //To make it faster, we can stop it after 5 words too }else if(history.size() >= MAX_WORDS){ //Print the list printBitmasks(history); return; } //Thin out the array with only stuff that matches the currentBitmask. std::vector<unsigned int> newBitmasks; std::copy_if(oldBitmasks.begin(), oldBitmasks.end(), std::back_inserter(newBitmasks), [¤tBitmask](unsigned int bruh){ return (bruh & currentBitmask) == 0; }); while(newBitmasks.size() > 0){ //I know this modifies 'oldBitmasks' too. It's intentional. //This makes it so that the word is never involved in any of the child serches or any of the later searches in this while loop. const unsigned int word = newBitmasks.back(); newBitmasks.pop_back(); std::vector<unsigned int> newHistory = history; newHistory.push_back(word); recurse(newBitmasks, newHistory, currentBitmask | word); } } int main(){ const std::set<unsigned int>* bitmasksSet = getBitmasks(); std::vector<unsigned int> bitmasks(bitmasksSet->begin(), bitmasksSet->end()); delete bitmasksSet; recurse(bitmasks, std::vector<unsigned int>(), 0); return 0; }
• Jun 17, 2024 •oceantran27
0 likes • 3 views
#include <iostream> using namespace std; int main { cout << 1; }
• 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; }