diff --git a/languages/c++/CMakeLists.txt b/languages/c++/CMakeLists.txt new file mode 100644 index 0000000..5ffd049 --- /dev/null +++ b/languages/c++/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.13) +project(the-honk) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(pascal code/pascal.cc) +add_executable(babylonian code/babylonian.cc) diff --git a/languages/c++/babylonian.cc b/languages/c++/code/babylonian.cc similarity index 84% rename from languages/c++/babylonian.cc rename to languages/c++/code/babylonian.cc index 1de722f..9f2021b 100644 --- a/languages/c++/babylonian.cc +++ b/languages/c++/code/babylonian.cc @@ -12,7 +12,7 @@ int sqrt(int number) { } int main() { - cout << "Type a number to predict the square root of:"; + cout << "Type a number to predict the square root of: "; int number; cin >> number; diff --git a/languages/c++/pascal.cc b/languages/c++/code/pascal.cc similarity index 58% rename from languages/c++/pascal.cc rename to languages/c++/code/pascal.cc index 3338675..c88e002 100644 --- a/languages/c++/pascal.cc +++ b/languages/c++/code/pascal.cc @@ -3,12 +3,12 @@ using namespace std; // Calculate Pascal's triangle -vector> pascal(int rowCount) { - vector> rows{{1}}; +vector> pascal(int rowCount) { + vector> rows{{1}}; for (int i = 0; i < rowCount; i++) { - vector previousRow = rows[rows.size() - 1]; - vector newRow{1}; + vector previousRow = rows[rows.size() - 1]; + vector newRow{1}; for (int j = 0; j < previousRow.size() - 1; j++) { newRow.push_back(previousRow[j] + previousRow[j + 1]); @@ -21,7 +21,8 @@ vector> pascal(int rowCount) { return rows; } -int vectorStringLength(vector vector) { +// Find the length of a vector if it were outputted as a string with a space between each value +int vectorStringLength(vector vector) { int value = vector.size(); for (int i = 0; i < vector.size(); i++) { @@ -31,15 +32,18 @@ int vectorStringLength(vector vector) { return value; } -int main() { - vector> pascalsTriangle = pascal(10); +// Draw Pascal's Triangle +void drawPascalTriangle(int rowCount) { + vector> pascalsTriangle = pascal(rowCount); int bottomRowSize = vectorStringLength(pascalsTriangle[pascalsTriangle.size() - 1]); - // Output the triangle - for (int i = 0; i < pascalsTriangle.size(); i++) { - vector currentRow = pascalsTriangle[i]; + for (int i = 0; i < pascalsTriangle.size() - 1; i++) { + vector currentRow = pascalsTriangle[i]; + int rowSize = vectorStringLength(currentRow); + int sizeDifference = bottomRowSize - rowSize; + string spacing = ""; for (int j = 0; j < sizeDifference / 2; j++) { @@ -63,3 +67,12 @@ int main() { } } } + +int main() { + cout << "How many rows of Pascal's Triangle would you like to calculate? "; + + int rowCount; + cin >> rowCount; + + drawPascalTriangle(rowCount); +}