Skip to main content

Hash Table Example

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

More C++ Posts

Infection Simulation

0 likes • Nov 18, 2022 • 2 views
C++
/*
this program will simulate the spreading of a disease through a
grid of people, starting from a user-defined person. It will count
the number of turns taken before everyone on the grid is immunized
to the disease after having caught it once.
This program will user the SIR model (Susceptible, Infectious, Recovered)
and cellular automata to simulate the people in the grid.
*/
#include <iostream>
using namespace std;
/* Any and all global variables */
const int SIZE = 8; //Size of the square person array
/* Any and all functions */
void gridDefaultify(char[][SIZE], int);
//Purpose: Sets each item in the person array to 's'
//Parameters: A square, two-dimensional array
// The size of that array's bounds
void gridDisplay(char[][SIZE], int);
//Purpose: Formats and prints the information in the person grid
//Parameters: A square, two-dimensional array
// The value of the current day
void nextTurn(char[][SIZE], char[][SIZE], int&);
//Purpose: Updates the grid of people, and the current day
//Parameters: Two square, two-dimensional arrays
// A reference to the current day (so that it can be updated)
int countInfected(char[][SIZE], int);
//Purpose: Counts the number of infectious people on the grid
//Parameters: A square, two-dimensional array
// The size of that array's bounds
int main(){
int currentDay = 0; //Infection begins on day 0, and ends one day after the last person is Recovered
char gridCurrent[SIZE][SIZE]; //Grid of all people
char gridUpdate[SIZE][SIZE]; //Where the user chooses to start the infection
int xToInfect;
int yToInfect; //Set of coordinates for the initial infection position, given by user
//Initializes the grids to all 's'
gridDefaultify(gridCurrent, SIZE);
gridDefaultify(gridUpdate, SIZE);
//The below block gets the initial infection coordinates from the user
cout << "Please enter a location to infect: ";
while(true){
cin >> xToInfect >> yToInfect;
xToInfect--;
yToInfect--;
if(xToInfect < 0 || yToInfect < 0 || xToInfect >= SIZE || yToInfect >= SIZE){
cout << "Those coordinates are outside the bounds of this region." << endl;
cout << "Please enter another location to infect: ";
continue;
} else {
gridCurrent[xToInfect][yToInfect] = 'i';
break;
}
}
//Displays the initial state of the grid
gridDisplay(gridCurrent, currentDay);
//The below block will display and update the grid until the infection is done.
while(true){
nextTurn(gridCurrent, gridUpdate, currentDay);
gridDisplay(gridCurrent, currentDay);
if(countInfected(gridCurrent, SIZE) == 0) break; //Once there are no more infected, the game is done
}
//Displays the number of days taken for the infection to end
cout << "It took " << currentDay + 1 << " days for the outbreak to end";
cout << endl;
return 0;
}
void gridDefaultify(char arr[][SIZE], int arrSize){
for(int x = 0; x < arrSize; x++){
for(int y = 0; y < arrSize; y++){
arr[x][y] = 's'; //Sets all items in the passed-in array to 's'
}
}
return;
}
void gridDisplay(char arr[][SIZE], int day){
cout << "Day " << day << endl; //Prints the current day
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
cout << arr[x][y] <<" "; //Prints the array's contents
}
cout << endl; //Formats with newlines
}
cout << endl; //Some spacing
return;
}
void nextTurn(char today[][SIZE], char update[][SIZE], int& day){
day++; //Updates the day
int xCheck; //X coordinate to be checked
int yCheck; //Y coordinate to be checked
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
//Sets all 'i' to 'r' in the new grid
if(today[x][y] == 'i' || today[x][y] == 'r'){
update[x][y] = 'r'; //Updates all infectious to recovered, and keeps current recovered
}
if(today[x][y] == 's'){ // If the person is susceptible...
for(int xCheck = x-1; xCheck <= x+1; xCheck++){ // Check all x coordinates around the person
for(int yCheck = y-1; yCheck <= y+1; yCheck++){ // Check all y coordinates around the person
if(xCheck == x && yCheck == y){
// Don't check at the person because there is no need to check there
} else {
if(xCheck >= 0 && yCheck >= 0 && xCheck < SIZE && yCheck < SIZE){ // Make sure the checked coordinates are in bounds
if(today[xCheck][yCheck] == 'i'){ //Is the person at the checked coordinates infected?
update[x][y] = 'i'; //If so, update the 's' to 'i' in the new grid
}
}
}
}
}
}
}
}
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
today[x][y] = update[x][y]; //Updates today's grid with the new values
}
}
}
int countInfected(char arr[][SIZE], int arrSize){
int count = 0;
for(int x = 0; x < arrSize; x++){
for(int y = 0; y < arrSize; y++){
if(arr[x][y] == 'i') count++; //Increments count for each infected person in the grid
}
}
return count;
}

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;
}

minimum matrix values

0 likes • Nov 18, 2022 • 3 views
C++
#include <iostream>
using namespace std;
int main() {
const int ROW_SIZE = 2;
const int COLUMN_SIZE = 5; //establish all variables
int 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;
}

set hostname syscall

0 likes • Oct 7, 2023 • 10 views
C++
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/utsname.h>
int main() {
char newHostname[] = "newhostname"; // Replace with the desired hostname
if (sethostname(newHostname, strlen(newHostname)) == 0) {
std::cout << "Hostname set to: " << newHostname << std::endl;
// Optionally, update the /etc/hostname file to make the change permanent
FILE *hostnameFile = fopen("/etc/hostname", "w");
if (hostnameFile != NULL) {
fprintf(hostnameFile, "%s\n", newHostname);
fclose(hostnameFile);
} else {
perror("Failed to update /etc/hostname");
}
} else {
perror("Failed to set hostname");
}
return 0;
}

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:
// T25seSBjLikKVGhlIGNvbXBpbGVyIGRvZXNuJ3QgYXBwcmVjaWF0ZSB5b3UgbWFraW5nIHRoZSBjaGFyYWN0ZXJzIHRoZSBwb2ludGVyIHJlZmVycyB0byBub24tY29uc3QsIGJ1dCBpdCdzIG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QgYnkgdmFsdWUsIG5vdCByZWZlcmVuY2Uu

Two Letter Combinations

0 likes • Nov 18, 2022 • 0 views
C++
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
//This program makes a new text file that contains all combinations of two letters.
// aa, ab, ..., zy, zz
int main(){
string filename = "two_letters.txt";
ofstream outFile;
outFile.open(filename.c_str());
if(!outFile.is_open()){
cout << "Something's wrong. Closing..." << endl;
return 0;
}
for(char first = 'a'; first <= 'z'; first++){
for(char second = 'a'; second <= 'z'; second++){
outFile << first << second << " ";
}
outFile << endl;
}
return 0;
}