Skip to content

Instantly share code, notes, and snippets.

View mshafae's full-sized avatar

Michael Shafae mshafae

View GitHub Profile
@mshafae
mshafae / cpsc120_skip_first_array.cc
Created October 20, 2022 00:30
CPSC 120 Exampl of how to skip the first element of an array using a boolean flag.
// Gist https://gist.github.com/746e191172af461f09f7b06bbebd3680
#include <array>
#include <iostream>
#include <string>
int main(int argc, char const* argv[]) {
std::array<int, 6> my_array{10, 11, 12, 13, 14, 15};
bool should_skip_first{true};
@mshafae
mshafae / cpsc120_write_message.cc
Last active October 30, 2024 06:25
CPSC 120 Read a line of text from cin and then write the line out to a file
// Gist https://gist.github.com/30ad98e4ca6f0aa45db9e9a31c5625af
// Filename cpsc120_write_message.cc
// CompileCommand clang++ -std=c++17 cpsc120_write_message.cc
// Read a line of text from cin and then write the line out to a file
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
@mshafae
mshafae / cpsc120_rename_jpeg.cc
Last active October 22, 2022 17:00
CPSC 120 Demonstrate how to use the filesystem library and functions to find files and rename them.
// Gist https://gist.github.com/f6a5ebf19ad736a3bdcf28be34b84cc4
// To try out this program, you don't need to have a folder full of JPEG images.
// Do the following from from the command line to create some sample data to
// demonstrate how this program works.
// $ mkdir sample_data
// $ cd sample_data
// $ touch blue.jpeg red.jpeg yellow.jpeg orange.jpeg green.jpeg purple.jpeg
// $ cd ..
@mshafae
mshafae / vscode_extensions_and_style.sh
Last active October 26, 2022 05:35
VS Code extension and C++ style configuration script.
#!/bin/bash
# Gist https://gist.github.com/5618d1816364893ce9236d5d50272aa2
backup_file ()
{
DATE=`date +"%Y%m%d-%S"`
NAME=${1}
NEWNAME=${NAME}-${DATE}.og
echo "Copying ${NAME} to ${NEWNAME}"
@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[]) {
@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_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_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_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_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;