Skip to content

Instantly share code, notes, and snippets.

View mshafae's full-sized avatar

Michael Shafae mshafae

View GitHub Profile
@mshafae
mshafae / cpsc120_tip.cc
Created February 16, 2023 21:59
CPSC 120 Example calculating a 15% tip.
// Gist https://gist.github.com/f6d31d8630589c2e8986de8ce6238350
#include <iostream>
int main(int argc, char const *argv[]) {
// Constant tip rate of 15% expressed as a decimal of 0.15
const double kTipRate{0.15};
std::cout << "What was the bill's sum? ";
double bill;
@mshafae
mshafae / clear-terminal.py
Last active March 6, 2023 06:03
CPSC 386 Clearing the terminal three different ways.
#!/usr/bin/env python3
#
# Clearing the terminal three different ways.
#
# Gist https://gist.github.com/da88307f3b58b7a82bd26a8e998d6ce5
#
def simple():
# you can make it fancier by figuring out how many rows
# the terminal has and replace the hard-coded 24.
@mshafae
mshafae / cpsc-386-q08.py
Created June 28, 2023 05:29
CPSC 386 Quiz 8 Sample Solutions
#!/usr/bin/env python3
# Gist: https://gist.github.com/mshafae/0ca00a411cab94a106d5585451f62dff
"""Example solutions to the CA/US quiz using the us_state.pckl and ca_county.pckl pickle files."""
from collections import namedtuple
import pickle
import locale
@mshafae
mshafae / cpsc120_trunc_convert_double.cc
Created October 3, 2023 04:46
CPSC 120 Example of using trunc to truncate a double and static_cast to convert a double to an int.
// cpsc120_trunc_convert_double.cc
// Gist https://gist.github.com/mshafae/7b281ca822da831d8ffb85982034c917
#include <cmath>
#include <iostream>
#include <limits>
int main(int argc, char const *argv[]) {
// This is a really large number, a 1 with 308 zeros after it.
// double decimal_number{1.0e308};
@mshafae
mshafae / cpsc120_double_comparison.cc
Last active October 8, 2024 18:58
Demonstrating how doubles are approximating Real numbers and how direct comparison must be avoided.
// Gist https://gist.github.com/mshafae/77d3cda92a90746210f3224c1cf83555
// Filename cpsc120_double_comparison.cc
// CompileCommand clang++ -std=c++17 cpsc120_double_comparison.cc
// Demonstrating how doubles are approximating Real numbers and how direct
// comparison must be avoided.
#include <iomanip>
#include <iostream>
// lhs stands for "left hand side"
@mshafae
mshafae / cpsc120_filelines_to_vector.cc
Last active April 16, 2024 21:13
CPSC 120 Example of reading a file line by line and storing each line into a std::vector
// Gist https://gist.github.com/mshafae/4d282935ea5deaf8c2e6aca83a9fcac8
// Filename cpsc120_filelines_to_vector.cc
// CompileCommand clang++ -std=c++17 cpsc120_filelines_to_vector.cc
// Read a file line by line and place contents into a std::vector
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
@mshafae
mshafae / cpsc120_filelines_to_vector.cc
Last active April 16, 2024 21:12
CPSC 120 Example of reading a file line by line, storing it in a std::vector, and then selecting random lines from the vector to print.
// Gist https://gist.github.com/mshafae/4d282935ea5deaf8c2e6aca83a9fcac8
// Filename cpsc120_filelines_to_vector.cc
// CompileCommand clang++ -std=c++17 cpsc120_filelines_to_vector
// Read a file line by line and place contents into a std::vector
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
@mshafae
mshafae / cpsc120_2D_vector.cpp
Last active December 13, 2023 23:18
Create a vector of vectors, the inner vectors contain strings
// Gist https://gist.github.com/mshafae/eb239941e0171dcf564d91a3f75c64a7
// Filename cpsc120_2D_vector.cpp
// CompileCommand clang++ -std=c++17 cpsc120_2D_vector.cpp
// Create a vector of vectors, the inner vectors contain strings
// Note that C++ is row major order.
#include <iostream>
#include <string>
#include <vector>
@mshafae
mshafae / cpsc120_vector.cc
Created November 13, 2023 21:25
Working with C++ vectors.
// Gist https://gist.github.com/mshafae/ff7818bd3514ad0539e90b5d2f8933b3
// Filename cpsc120_vector.cc
// CompileCommand clang++ -std=c++17 cpsc120_vector.cc
// Working with C++ vectors.
#include <iostream>
#include <vector>
int main(int argc, char const* argv[]) {
@mshafae
mshafae / cpsc120_2D_vector_pt2.cpp
Created November 14, 2023 05:59
Create a vector of vectors, the inner vectors contain strings
// Gist https://gist.github.com/mshafae/759d0d17b0bb03873516dcc9e16f5dca
// Filename cpsc120_2D_vector_pt2.cpp
// CompileCommand clang++ -std=c++17 cpsc120_2D_vector_pt2.cpp
// Create a vector of vectors, the inner vectors contain strings
// Note that C++ is row major order.
#include <iostream>
#include <string>
#include <vector>