• Nov 18, 2022 •AustinLeath
0 likes • 4 views
#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; }
• Oct 23, 2022 •LeifMessinger
0 likes • 1 view
//Leif Messinger //Finds all sets of 5 5 letter words that don't have duplicate letters in either themselves or each other. //First it reads the words in and puts them in groups of their bitmasks //After that, we recurse on each group. Before doing that, we remove the group from the set of other groups to check it against. #include <cstdio> //getchar, printf #include <cassert> //assert #include <vector> #include <set> #include <algorithm> //std::copy_if #include <iterator> //std::back_inserter #define CHECK_FOR_CRLF true #define MIN_WORDS 5 #define MAX_WORDS 5 #define WORD_TOO_LONG(len) (len != 5) const unsigned int charToBitmask(const char bruh){ assert(bruh >= 'a' && bruh <= 'z'); return (1 << (bruh - 'a')); } void printBitmask(unsigned int bitmask){ char start = 'a'; while(bitmask != 0){ if(bitmask & 1){ putchar(start); } bitmask >>= 1; ++start; } } //Pointer needs to be deleted const std::set<unsigned int>* getBitmasks(){ std::set<unsigned int>* bitmasksPointer = new std::set<unsigned int>; std::set<unsigned int>& bitmasks = (*bitmasksPointer); unsigned int bitmask = 0; unsigned int wordLength = 0; bool duplicateLetters = false; for(char c = getchar(); c >= 0; c = getchar()){ if(CHECK_FOR_CRLF && c == '\r'){ continue; } if(c == '\n'){ if(!(WORD_TOO_LONG(wordLength) || duplicateLetters)) bitmasks.insert(bitmask); bitmask = 0; wordLength = 0; duplicateLetters = false; continue; } if((bitmask & charToBitmask(c)) != 0) duplicateLetters = true; bitmask |= charToBitmask(c); ++wordLength; } return bitmasksPointer; } void printBitmasks(const std::vector<unsigned int>& bitmasks){ for(unsigned int bruh : bitmasks){ printBitmask(bruh); putchar(','); putchar(' '); } puts(""); } //Just to be clear, when I mean "word", I mean a group of words with the same letters. void recurse(std::vector<unsigned int>& oldBitmasks, std::vector<unsigned int> history, const unsigned int currentBitmask){ //If there's not enough words left if(oldBitmasks.size() + (-(history.size())) + (-MIN_WORDS) <= 0){ //If there's enough words if(history.size() >= MIN_WORDS){ //Print the list printBitmasks(history); } return; //To make it faster, we can stop it after 5 words too }else if(history.size() >= MAX_WORDS){ //Print the list printBitmasks(history); return; } //Thin out the array with only stuff that matches the currentBitmask. std::vector<unsigned int> newBitmasks; std::copy_if(oldBitmasks.begin(), oldBitmasks.end(), std::back_inserter(newBitmasks), [¤tBitmask](unsigned int bruh){ return (bruh & currentBitmask) == 0; }); while(newBitmasks.size() > 0){ //I know this modifies 'oldBitmasks' too. It's intentional. //This makes it so that the word is never involved in any of the child serches or any of the later searches in this while loop. const unsigned int word = newBitmasks.back(); newBitmasks.pop_back(); std::vector<unsigned int> newHistory = history; newHistory.push_back(word); recurse(newBitmasks, newHistory, currentBitmask | word); } } int main(){ const std::set<unsigned int>* bitmasksSet = getBitmasks(); std::vector<unsigned int> bitmasks(bitmasksSet->begin(), bitmasksSet->end()); delete bitmasksSet; recurse(bitmasks, std::vector<unsigned int>(), 0); return 0; }
0 likes • 5 views
#include <iostream> using namespace std; /* Function: get_coeff Parameters: double& coeff, int pos passed from bb_4ac Return: type is void so no return, but does ask for user to input data that establishes what a b and c are. */ void get_coeff(double& coeff, int pos) { char position; if(pos == 1) { position = 'a'; } else if(pos == 2) { //a simple system to determine what coefficient the program is asking for. position = 'b'; } else { position = 'c'; } cout << "Enter the co-efficient " << position << ":"; //prompt to input coeff coeff = 5; //input coeff } /* Function: bb_4ac Parameters: no parameters passed from main, but 3 params established in function, double a, b, c. Return: b * b - 4 * a * c */ double bb_4ac() { double a, b, c; //coefficients of a quadratic equation get_coeff(a, 1); // call function 1st time get_coeff(b, 2); // call function 2nd time get_coeff(c, 3); // call function 3rd time return b * b - 4 * a * c; //return b * b - 4 * a * c } int main() { cout << "Function to calculate the discriminant of the equation. . . " << endl; double determinate = bb_4ac(); //assign double determinate to bb_4ac function cout << "The discriminant for given values is: " << determinate << endl; //output the determinate! }
1 like • 9 views
#include <iostream> using namespace std; int main() { int arr[5]; for(int i = 0; i < 5; i++) { arr[i] = i; } for(int i = 0; i < 5; i++) { cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl; } for(int i=0;i<5;i++) { for(int j=i+1;j<5;j++) { if(arr[i]>arr[j]) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } cout << endl; for(int i = 0; i < 5; i++) { cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl; } return 0; }
• Feb 4, 2021 •aedrarian
0 likes • 0 views
#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."; }
• Jun 17, 2024 •oceantran27
0 likes • 3 views
#include <iostream> using namespace std; int main { cout << 1; }