Skip to content

Instantly share code, notes, and snippets.

View mshafae's full-sized avatar

Michael Shafae mshafae

View GitHub Profile
@mshafae
mshafae / cpsc120_do_while_loop_prompt.cc
Last active December 13, 2023 23:17
CPSC 12120 Loop until a number is entered that matches the criteria.
// Gist https://gist.github.com/cfbbe37b090687dd8a57acfbf7706844
#include <iostream>
int main(int argc, char const *argv[]) {
int input_number{0};
do {
std::cout << "Enter a number between 10 and 20: ";
std::cin >> input_number;
@mshafae
mshafae / cpsc120_int_reference.cc
Last active December 13, 2023 23:17
CPSC 120 Example showing how references are different than copies of a value.
// Gist https://gist.github.com/880c7bba03f62a3cf7e6a16321aacaf6
#include <iostream>
int main(int argc, char const* argv[]) {
int number{42};
int& number_reference = number;
int a_copy{number};
int another_copy{-23};
@mshafae
mshafae / whats_inside.cc
Created October 4, 2022 03:59
Shows numbers formatted as bytes, hex, and floating point.
// Gist https://gist.github.com/c92859aace3e06a76e11adee327d8224
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
// https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
// Retrieved Feb. 2021
@mshafae
mshafae / cpsc120_absolute_path.cc
Created October 20, 2022 00:21
CPSC 120 Given a relative path to a file/directory, print the absolute path for the item.
// Gist https://gist.github.com/a3f48da332e04129d816ddf4bf64daf2
#include <filesystem>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char const *argv[]) {
std::vector<std::string> args{argv, argv + argc};
@mshafae
mshafae / cpsc120_pwd.cc
Last active October 20, 2022 00:43
CPSC 120 Program that mimics the command pwd.
// Gist https://gist.github.com/fba16c441b459231d6fc5d2e35d046aa
#include <filesystem>
#include <iostream>
int main(int argc, char const *argv[]) {
// works just like `pwd`
std::cout << std::filesystem::current_path().string() << "\n";
return 0;
@mshafae
mshafae / cpsc120_cat.cc
Last active April 16, 2024 21:07
CPSC 120 Open a file and read it line by line; print the contents to your terminal.
// Gist https://gist.github.com/49124a3e4c73ad44ac331dc023b61326
// Filename cpsc120_cat.cc
// CompileCommand clang++ -std=c++17 cpsc120_cat.cc
// An implementation of the `cat` program which prints the contents of a file
// to the std::cout (stdout).
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
@mshafae
mshafae / cpsc120_filter.cc
Last active December 13, 2023 23:17
CPSC 120 Filter through an array using an if condition and continue.
// Gist https://gist.github.com/37b21d3ba3d4630be6d97dae1b8fbde1
#include <array>
#include <iostream>
int main(int argc, char const* argv[]) {
std::array<int, 6> my_array{10, 11, 12, 13, 14, 15};
for (const int& number : my_array) {
if (number % 2 != 0) {
@mshafae
mshafae / cpsc120_find_value.cc
Last active December 13, 2023 23:17
CPSC 120 Find a value in an array.
// Gist https://gist.github.com/ccd1c720a571b84e855c59e00f66df99
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char const* argv[]) {
vector<std::string> arguments{argv, argv + argc};
bool found{false};
@mshafae
mshafae / cpsc120_fscat.cc
Last active April 16, 2024 21:11
CPSC 120 Read a file line by line and print the contents to the terminal. Use the filesystem library for the path instead of a std::string.
// Gist https://gist.github.com/5b341c7d272ce7442754699351008d3e
// Filename cpsc120_fscat.cc
// CompileCommand clang++ -std=c++17 cpsc120_fscat.cc
// An implementation of the `cat` program which prints the contents of a file
// to the std::cout (stdout) using the filesystem::path type.
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
@mshafae
mshafae / cpsc120_skip_first_arg.cc
Last active December 13, 2023 23:17
CPSC 120 Example of how to skip the first command line argument using a boolean flag.
// Gist https://gist.github.com/77e6c030cb519c2cbb221a00eff2cbe2
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char const* argv[]) {
std::vector<std::string> arguments{argv, argv + argc};
bool should_skip_first{true};