ASDV-Cpp/Assignments/MP2_CalebFontenot_clion/2DArrayOperations.cpp

190 lines
5.1 KiB
C++
Raw Normal View History

2024-03-07 10:16:43 -06:00
//
// Created by caleb on 3/6/24.
//
2024-03-07 18:40:11 -06:00
#include <iostream>
#include <vector>
#include <random>
2024-03-07 10:16:43 -06:00
#include <exception>
#include <climits>
2024-03-07 18:40:11 -06:00
#include <cstdlib>
#include <map>
#include <algorithm>
using namespace std::literals::string_literals; // import std::string literals
#include "binarySearch.h"
2024-03-08 09:15:43 -06:00
#include "colormap.h"
2024-03-07 10:16:43 -06:00
class OutOfBoundsException: public std::exception {
virtual const char* what() const throw()
{
return "Method attempted to call a vector location that was larger than the vector itself.";
}
} OutOfBoundsException;
int getTotal(std::vector<int> arr) {
int sum = 0;
for (int i = 0; i < arr.size(); ++i) {
sum += arr[i];
}
return sum;
}
int getTotal(std::vector<std::vector<int>> arr) {
int sum = 0;
for (int i = 0; i < arr.size(); ++i) {
sum += getTotal(arr[i]);
}
return sum;
}
int getAverage(std::vector<int> arr) {
return getTotal(arr) / arr.size(); // NOLINT(*-narrowing-conversions)
}
int getAverage(std::vector<std::vector<int>> arr) {
return getTotal(arr) / arr.size();
}
int getRowTotal(std::vector<std::vector<int>> arr, int desiredRow) {
// validate
if (desiredRow > arr[desiredRow].size()) {
throw OutOfBoundsException;
}
int sum = 0;
for (int i = 0; i < arr[desiredRow].size(); ++i) {
sum += arr[desiredRow][i];
}
return sum;
}
int getColumnTotal(std::vector<std::vector<int>> arr, int desiredColumn) {
// validate
if (desiredColumn > arr.size()) {
throw OutOfBoundsException;
}
int sum = 0;
for (int i = 0; i < arr.size(); ++i) {
sum += arr[i][desiredColumn];
}
return sum;
}
int getHighestInRow(std::vector<std::vector<int>> arr, int desiredRow) {
// validate
if (desiredRow > arr[desiredRow].size()) {
throw OutOfBoundsException;
}
int highest = INT_MIN;
for (int i = 0; i < arr[desiredRow].size(); ++i) {
if (arr[desiredRow][i] > highest) {
highest = arr[desiredRow][i];
}
}
return highest;
}
int getLowestInRow(std::vector<std::vector<int>> arr, int desiredRow) {
// validate
if (desiredRow > arr[desiredRow].size()) {
throw OutOfBoundsException;
}
int lowest = INT_MAX;
for (int i = 0; i < arr[desiredRow].size(); ++i) {
if (arr[desiredRow][i] < lowest) {
lowest = arr[desiredRow][i];
}
}
return lowest;
}
int getHighestInColumn(std::vector<std::vector<int>> arr, int desiredColumn) {
// validate
if (desiredColumn > arr.size()) {
throw OutOfBoundsException;
}
int highest = INT_MIN;
for (int i = 0; i < arr.size(); ++i) {
if (arr[i][desiredColumn] > highest) {
highest = arr[i][desiredColumn];
}
}
return highest;
}
int getLowestInColumn(std::vector<std::vector<int>> arr, int desiredColumn) {
// validate
if (desiredColumn > arr.size()) {
throw OutOfBoundsException;
}
int lowest = INT_MAX;
for (int i = 0; i < arr.size(); ++i) {
if (arr[i][desiredColumn] < lowest) {
lowest = arr[i][desiredColumn];
}
}
return lowest;
}
2024-03-07 18:40:11 -06:00
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}) {
2024-03-07 10:16:43 -06:00
std::string output = "{";
2024-03-07 18:40:11 -06:00
std::map<std::string, int> columnMap;
2024-03-07 10:16:43 -06:00
for (int i = 0; i < arr.size(); ++i) {
2024-03-07 18:40:11 -06:00
// 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());
}
2024-03-07 10:16:43 -06:00
output.append(std::to_string(arr[i]));
if ((arr.size() - 1) > i) {
output.append(", ");
}
2024-03-08 09:15:43 -06:00
if (binarySearch(locationVector, i) == i) {
output.append(COLOR_WHITE.c_str());
}
2024-03-07 10:16:43 -06:00
}
output.append("}");
return output;
}
2024-03-07 18:40:11 -06:00
std::string printVector(std::vector<std::vector<int>> arr, std::vector<std::tuple<int, int>> locationVector = {{-1, -1}}) {
2024-03-07 10:16:43 -06:00
std::string output = "{\n";
2024-03-07 18:40:11 -06:00
std::tuple highlightTuple;
2024-03-07 10:16:43 -06:00
for (int i = 0; i < arr.size(); ++i) {
2024-03-07 18:40:11 -06:00
std::vector<int> rowVector = getRowMap(locationVector, i);
output.append(printVector(arr[i], rowVector));
// print ",\n" if at the end of the loop
2024-03-07 10:16:43 -06:00
if ((arr.size() - 1) > i) {
output.append(",\n");
}
}
output.append("\n}");
return output;
2024-03-07 18:40:11 -06:00
}
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;
2024-03-08 09:15:43 -06:00
}