Skip to main content

minimum matrix values

0 likes • Nov 18, 2022 • 3 views
C++
Loading...

More C++ Posts

Const value const pointer question

0 likes • Aug 25, 2023 • 11 views
C++
#include <iostream>
int main(){
const char* const hello = "Hello, world!";
const char* bruh = hello;
char* const yeet = hello;
std::cout << bruh << std::endl;
std::cout << yeet << std::endl;
return 0;
}
/*
Place your bets!
Will the program:
a.) Print "Hello, world!" twice?
b.) Compile error on line 5 (bruh initialize line) because the pointer gets implicit cast to non-const?
c.) Compile error on line 7 (yeet initialize line) because the char gets implicit cast to non-const?
d.) Both b and c?
e.) Compile error line 11 (print yeet) because the pointer is constant and can't be incremented
f.) Print "Hello, world!" then print the pointer address in hexadecimal
g.) Both b and e?
h.) Both c and e?
i.) B, c, and e?
*/
// The answer is in this base 64 string:
// T25seSBjLikKVGhlIGNvbXBpbGVyIGRvZXNuJ3QgYXBwcmVjaWF0ZSB5b3UgbWFraW5nIHRoZSBjaGFyYWN0ZXJzIHRoZSBwb2ludGVyIHJlZmVycyB0byBub24tY29uc3QsIGJ1dCBpdCdzIGZpbmUgd2l0aCB5b3UgY29weWluZyBhIGNvbnN0YW50IHZhbHVlLCBpLmUuIHRoZSBwb2ludGVyLCB0byBhIG5vbi1jb25zdGFudCB2YXJpYWJsZS4KSWYgeW91IHJlcGxhY2UgdGhhdCBsaW5lIHdpdGggY2hhciogY29uc3QgeWVldCA9IGNvbnN0X2Nhc3Q8Y2hhciogY29uc3Q+KGhlbGxvKTsgSXQnbGwgcHJpbnQgIkhlbGxvLCB3b3JsZCEiIHR3aWNlLCB3aGljaCBpcyB2ZXJ5IHN0cmFuZ2UgY29uc2lkZXJpbmcgdGhhdCB5ZWV0IGlzIGEgY29uc3QgcG9pbnRlciwgc28geW91J2QgdGhpbmsgaXQgd291bGQgcHJpbnQgYXMgYSBoZXhhZGVjaW1hbCBiZWNhdXNlIGlmIHlvdSB0cnkgdG8gKCsreWVldCkgd2hpbGUgbG9vcGluZyB0aHJvdWdoIHRoZSBzdHJpbmcsIHlvdSdkIGdldCBhbiBlcnJvciwgYmVjYXVzZSBpdCdzIGNvbnN0IGFuZCBjYW4ndCBiZSBjaGFuZ2VkLgpJbnN0ZWFkIG9mIHVzaW5nIGEgdGVtcGxhdGUgZnVuY3Rpb24gZm9yIG9zdHJlYW06Om9wZXJhdG9yPDwsIHRoZXkgbWFrZSBpdCBhIGZ1bmN0aW9uIHRoYXQgdGFrZXMgdHlwZSBjb25zdCBjaGFyKiwgYW5kIEMrKyBoYXMgbm8gcHJvYmxlbXMgcHJvbW90aW5nIGEgdmFyaWFibGUgdG8gY29uc3RhbnQgd2hlbiBpbXBsaWNpdCBjYXN0aW5nLCBhbmQgaXQgaGFzIG5vIHByb2JsZW1zIGltcGxpY2l0IGNhc3RpbmcgdGhlIGNvbnN0IHBvaW50ZXIgdG8gYSBub3JtYWwgcG9pbnRlciBiZWNhdXNlIGl0J3MgbWFraW5nIGEgY29weS4gVGhlIHBvaW50ZXIgZ2V0cyBjb3BpZWQgYmVjYXVzZSB0aGUgcG9pbnRlciBpcyBwYXNzZWQgYnkgdmFsdWUsIG5vdCByZWZlcmVuY2Uu

Critques

0 likes • Feb 4, 2021 • 0 views
C++
#include <iostream>
using namespace std;
main
{
cout << "No tabbing. That's very sad :(\n";
cout << "No in-editor highlighting either :(((\n";
cout << "Descriptions might be niice too.";
}

Hash Table Example

0 likes • Nov 18, 2022 • 0 views
C++
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;
}

Literal Bruh

0 likes • Jul 30, 2023 • 5 views
C++
//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;
}

wordScore.cpp

0 likes • Apr 16, 2023 • 0 views
C++
#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;
}

GCD using Stein's Algorithm

0 likes • Jun 30, 2023 • 2 views
C++
// 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;
}