Skip to content

Instantly share code, notes, and snippets.

View mshafae's full-sized avatar

Michael Shafae mshafae

View GitHub Profile
@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 / 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 / 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 / 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 / cpsc120_prompt.cc
Created February 16, 2023 21:51
CPSC 120 Example showing different ways to prompt the computer user for input.
// Gist https://gist.github.com/70457715ff7fc2fcdcdaf44437624dd0
#include <iostream>
#include <string>
std::string PromptForString(std::string query) {
std::cout << query;
std::string answer;
std::cin >> answer;
return answer;
@mshafae
mshafae / cpsc120_prompt.cc
Last active February 19, 2025 19:51
CPSC 120 Example showing different ways to prompt the computer user for input.
// cpsc120_prompt.cc
// Gist https://gist.github.com/70457715ff7fc2fcdcdaf44437624dd0
#include <iostream>
#include <string>
std::string PromptForString(std::string query) {
std::cout << query;
std::string answer;
std::cin >> answer;
@mshafae
mshafae / cpsc120_image.cc
Last active December 13, 2023 23:18
CPSC 120 Example of creating an image using nested for loops and GraphicsMagick.
// Gist https://gist.github.com/mshafae/1e50f814b084f19af6a702ba8f1b9d6d
// Filename cpsc120_image.cc
// CompileCommand clang++ -std=c++17 -I /usr/include/GraphicsMagick -lGraphicsMagick++ cpsc120_image.cc
// Create a simple, small image from a given
// filename. GraphicsMagick will figure out how to output to the given file
// format.
// Supported formats: http://www.graphicsmagick.org/formats.html
// Example:
// clang++ -std=c++17 -I /usr/include/GraphicsMagick -lGraphicsMagick++
@mshafae
mshafae / cpsc120_stoi_try_catch.cc
Created November 9, 2022 02:38
CPSC 120 Example handling of exceptions generated by std::stoi()
// Gist https://gist.github.com/e319fd4912f979828fc7aca6a714820d
#include <iostream>
#include <string>
int main(int argc, char const* argv[]) {
std::string a_big_number{"65536"};
int the_integer_value{-1};
try {
@mshafae
mshafae / cpsc120_2D_array.cc
Last active November 4, 2022 19:13
CPSC 120 Demonstrate declaring and statically initializing a 2D std::array of string.
// Gist https://gist.github.com/697c2c169ebd5cdef9a3eb7d8c0bee1e
#include <array>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char const *argv[]) {
std::vector<std::string> arguments{argv, argv + argc};
@mshafae
mshafae / cpsc120_iterator.cc
Last active November 3, 2022 19:42
CPSC 120 Demonstrate the use of iterators and a few algorithms from the C++ standard library.
// Gist https://gist.github.com/36392c28705f3b62dda5d99d1db340b3
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main(int argc, char const *argv[]) {