Skip to main content

Enumeration Basics

0 likes • Nov 18, 2022 • 0 views
C++
Loading...

More C++ Posts

minimum matrix values

0 likes • Nov 18, 2022 • 3 views
C++
#include <iostream>
using namespace std;
int main() {
const int ROW_SIZE = 2;
const int COLUMN_SIZE = 5; //establish all variables
int matrix[ROW_SIZE][COLUMN_SIZE];
int minVal;
for (int i = 0; i < ROW_SIZE; ++i) // for loop to ask user to enter data.
{
for (int h = 0; h < COLUMN_SIZE; ++h) {
cout << "Enter data for row #" << i + 1 << " and column #" << h + 1 << ": ";
cin >> matrix[i][h];
}
}
cout << "You entered: " << endl;
for (int i = 0; i < ROW_SIZE; ++i) //for statements to output the array neatly
{
for (int h = 0; h < COLUMN_SIZE; ++h) {
cout << matrix[i][h] << "\t";
}
cout << endl;
}
cout << "Minimum for each row is: {";
for (int i = 0; i < ROW_SIZE; i++) //for statements to find the minimum in each row
{
minVal = matrix[i][0];
for (int h = 0; h < COLUMN_SIZE; h++) {
if (matrix[i][h] < minVal) // if matrix[i][h] < minVal -> minVal = matrix[i][h];
{
minVal = matrix[i][h];
}
}
cout << minVal << ", ";
}
cout << "}" << endl;
cout << "Minimum for each column is: {";
for (int i = 0; i < COLUMN_SIZE; i++) //for statements to find the minimum in each column
{
minVal = matrix[0][i];
for (int h = 0; h < ROW_SIZE; h++) {
if (matrix[h][i] < minVal) //replaces minVal with array index for that column that is lowest
{
minVal = matrix[h][i];
}
}
cout << minVal << ", ";
}
cout << "}" << endl;
return 0;
}

Wing Project 1

0 likes • Oct 31, 2021 • 1 view
C++
//Get data file at https://codecatch.net/post.php?postID=91e87d73
//Iteration 1 of Wing Project. Solution breaks down around n=35
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int getSum(map<int, int> list);
void readData(map<int, float>* data);
void lowestPrice();
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data);
//void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList);
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data);
float getPrice(map<int, int> set, map<int, float>* data);
template <typename S>
ostream& operator<<(ostream& os, const vector<S>& vector)
{
// Printing all the elements using <<
for (auto element : vector) {
os << element << " ";
}
return os;
}
bool operator==(map<int, int> m1, map<int, int> m2)
{
if(m1.size() != m2.size())
return false;
bool ret = true;
for(auto it = m1.begin(); it !=m1.end() && ret; it++)
{
if(ret && m1.count(it->first) != m2.count(it->first))
ret = false;
if(ret && m1.count(it->first) == 1)
{
if(m1.at(it->first) != m2.at(it->first))
ret = false;
}
}
return ret;
}
int main()
{
map<int, float> data;
readData(&data);
vector<map<int, int>> *sumsList;
sumsList = new vector<map<int, int>>;
findSums(40, sumsList, &data);
for(auto el : *sumsList)
{
for(auto it = el.begin(); it != el.end(); it++)
{
cout << it->first << "->" << it->second << " ";
}
cout << getPrice(el, &data) << endl;
}
return 0;
}
/* Returns the price of wings given a set of numbers of wings to buy.
* Returns -1 if the set contains a number that is not possible to buy.
*/
float getPrice(map<int, int> set, map<int, float>* data)
{
float price = 0;
for(auto it = set.begin(); it != set.end(); it++)
{
//If data doesn't contain an element of set, return -1
if(data->count(it->first) == 0)
return -1;
price += data->at(it->first) * it->second; //pricePerPacket * qtyOfPackets
}
return price;
}
/* Adds the elements of list.
* Suppose mapping is <num, qty>.
* Returns sum(num*qty)
*/
int getSum(map<int, int> list)
{
int sum = 0;
for(auto it = list.begin(); it != list.end(); it++)
sum += it->first * it->second;
return sum;
}
void findSums(int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
map<int, int> currList;
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void findSum(map<int, int> currList, int x, int n, vector<map<int, int>>* sumsList, map<int, float>* data)
{
//Append x to currList
if(currList.count(x) == 0)
currList.emplace(x, 1);
else
{
int val = 1+ currList.at(x);
currList.erase(x);
currList.emplace(x, val);
}
//Determine current sum, check for return cases
int currSum = getSum(currList);
if(currSum > n)
return;
else if(currSum == n)
{
//Check to make sure no duplicates
for(auto list : *sumsList)
{
if(list == currList)
return;
}
sumsList->push_back(currList);
return;
}
//Recur when currSum < n
auto it = data->begin();
while(it->first <= n-x && it != data->end())
{
findSum(currList, it->first, n, sumsList, data);
it++;
}
}
void readData(map<int, float>* data)
{
ifstream file ("./data", ifstream::in);
if(file.is_open())
{
int i = 0;
while(!file.eof())
{
float wings, price;
string skipnl;
file >> wings;
file >> price;
data->emplace(wings, price);
getline(file, skipnl);
i++;
}
}
}

Critques

0 likes • Feb 4, 2021 • 0 views
C++
#include <iostream>
using namespace std;
main
{
cout << "No tabbing. That's very sad :(\n";
cout << "No in-editor highlighting either :(((\n";
cout << "Descriptions might be niice too.";
}

C++ Range Slicer

0 likes • Oct 31, 2023 • 3 views
C++
//Leif Messinger
//Compile with C++ 20
#include <iostream>
#include <ranges>
#include <vector>
#include <functional>
#include <cctype> //toupper
#include <cxxabi.h>
template <typename T>
void printType(){
std::cout << abi::__cxa_demangle(typeid(T).name(), NULL, NULL, NULL) << std::endl;
}
template <typename T>
class Slicer{
public:
T begin_;
T end_;
T trueEnd;
Slicer(T begin, T end): begin_(begin), end_(begin), trueEnd(end){}
template<typename U>
Slicer(U&& vec) : begin_(vec.begin()), end_(vec.begin()), trueEnd(vec.end()){}
Slicer& finish(){
begin_ = end_;
end_ = trueEnd;
return (*this);
}
Slicer& to(long int index){
begin_ = end_;
if(index > 0){
end_ = (begin_ + index);
}else{
index *= -1;
end_ = (trueEnd - index);
}
return (*this);
}
Slicer& operator[](long int index){
return to(index);
}
T begin(){
return this->begin_;
}
T end(){
return this->end_;
}
Slicer& operator()(std::function<void(decltype(*begin_))> func) {
for(decltype(*begin_) thing : (*this)){
func(thing);
}
return (*this);
}
};
static_assert(std::ranges::range< Slicer<std::vector<int>::const_iterator> >);
int main(){
std::string vec = "abcdefghijklmnopqrstuvwxyz";
Slicer<std::string::const_iterator> bruh(vec);
//printType<decltype(bruh)>();
bruh.to(3)([](char yeet){
std::cout << yeet;
})
.to(-1)([](char yeet){
std::cout << char(std::toupper(yeet));
}).finish()([](char yeet){
std::cout << yeet << yeet << yeet << yeet << yeet;
});
std::cout << std::endl << std::endl;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
Slicer<int*> arrSlicer(arr, arr + (sizeof(arr)/sizeof(int)));
std::cout << "[";
arrSlicer.to(-1)([](int yeet){
std::cout << yeet << ", ";
}).finish()([](int yeet){
std::cout << yeet << "]" << std::endl;
});
return 0;
}

Hello World!

0 likes • Aug 31, 2020 • 2 views
C++
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
// Prints out "Hello World"
return 0;
}

UNT CSCE 1040 Goat Program

0 likes • Nov 18, 2022 • 1 view
C++
#include "goat.h" //include goat.h
void Goat::setBreed(string breed) {
this->breed = breed;
}
void Goat::setWeight(float weight) {
this->weight = weight;
}
void Goat::setName(string name) {
this->name = name;
}
void Goat::setGender(char gender) {
this->gender = gender;
}
void Goat::setSpayed(bool goatIsSpayed) {
this->goatIsSpayed = goatIsSpayed;
}
void Goat::setRegistrationID(string registrationID) {
this->registrationID = registrationID;
}
void Goat::setColor(string color) {
this->color = color;
}
void Goat::setOtherComments(string otherComments) {
this->otherComments = otherComments;
}
string Goat::getBreed() {
return breed;
}
float Goat::getWeight() {
return weight;
}
string Goat::getName() {
return name;
}
char Goat::getGender() {
return gender;
}
bool Goat::getSpayed() {
return goatIsSpayed;
}
string Goat::getRegistrationID() {
return registrationID;
}
string Goat::getColor() {
return color;
}
string Goat::getOtherComments() {
return otherComments;
}
Goat::Goat() {
breed = "";
weight = 0.0;
name = "";
gender = '\0';
goatIsSpayed = false;
registrationID = "";
color = "";
otherComments = "";
}
Goat::Goat(string goatBreed, float goatWeight, string goatName, char goatGender, bool goatSpayedStatus, string goatRegistrationID, string goatColor, string goatOtherComments) {
breed = goatBreed;
weight = goatWeight;
name = goatName;
gender = goatGender;
goatIsSpayed = goatSpayedStatus;
registrationID = goatRegistrationID;
color = goatColor;
otherComments = goatOtherComments;
}
Goat::~Goat() {
cout << "goat destroyed" << endl;
}
void Goat::printinfo() {
cout << "Breed: " << breed << endl << "weight: " << weight << endl << "Name: " << name << endl << "Gender: " << gender << endl << "is Spayed: ";
if(goatIsSpayed) { //here I do a logical test on boolean goatIsSpayed. if true cout << true else cout << false
cout << "True";
} else {
cout << "False";
}
cout << endl << "Registration ID: " << registrationID << endl << "Color Description: " << color << endl << "Other Comments: " << otherComments << endl << endl;
}