Update MP2

This commit is contained in:
2024-03-07 18:40:11 -06:00
parent 00e479a3a1
commit dd2bb0c99a
35 changed files with 2752 additions and 74 deletions

View File

@@ -1,10 +1,18 @@
//
// Created by caleb on 3/6/24.
//
#import <iostream>
#import <vector>
#include <iostream>
#include <vector>
#include <random>
#include <exception>
#include <climits>
#include <cstdlib>
#include <map>
#include <algorithm>
#include "colormap.h"
using namespace std::literals::string_literals; // import std::string literals
#include "binarySearch.h"
class OutOfBoundsException: public std::exception {
virtual const char* what() const throw()
@@ -117,9 +125,30 @@ int getLowestInColumn(std::vector<std::vector<int>> arr, int desiredColumn) {
return lowest;
}
std::string printVector(std::vector<int> arr) {
std::vector<int> getRowMap(std::vector<std::tuple<int, int>> locationVector, int row) {
std::vector<int> rowMap;
for (const auto& tuple : locationVector) {
int tupleRow = std::get<0>(tuple); // Get the first element of the tuple
int tupleValue = std::get<1>(tuple); // Get the second element of the tuple
if (tupleRow == row) {
rowMap.push_back(tupleValue); // Add the tuple value to the rowMap
}
}
return rowMap;
}
std::string printVector(std::vector<int> arr, std::vector<int> locationVector = {-1}) {
std::string output = "{";
std::map<std::string, int> columnMap;
for (int i = 0; i < arr.size(); ++i) {
// check if iteration value matches one of the locationVector values. If it does, append an ASCII color sequence.
if (binarySearch(locationVector, i) == i) {
output.append(colorIterator());
}
output.append(COLOR_WHITE);
output.append(std::to_string(arr[i]));
if ((arr.size() - 1) > i) {
output.append(", ");
@@ -129,14 +158,30 @@ std::string printVector(std::vector<int> arr) {
return output;
}
std::string printVector(std::vector<std::vector<int>> arr) {
std::string printVector(std::vector<std::vector<int>> arr, std::vector<std::tuple<int, int>> locationVector = {{-1, -1}}) {
std::string output = "{\n";
std::tuple highlightTuple;
for (int i = 0; i < arr.size(); ++i) {
output.append(printVector(arr[i]));
std::vector<int> rowVector = getRowMap(locationVector, i);
output.append(printVector(arr[i], rowVector));
// print ",\n" if at the end of the loop
if ((arr.size() - 1) > i) {
output.append(",\n");
}
}
output.append("\n}");
return output;
}
std::vector<std::vector<int>> random2DArray(int x, int y) {
std::vector<std::vector<int>> array(x, std::vector<int>(y, 0));
// setup random number generation
srand((unsigned) time(NULL));
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
array[i][j] = rand() / 1000;
}
}
return array;
}