Loading...
More C++ Posts
#include <iostream>using namespace std;int main(){int arr[] = {5, 1, 4, 20, 10, 2, 13, 11, 6, 21};int greed[] = {0, 0, 0, 0};int k = 0;int i;int set_index;while (k < 4){i = 0;while (i < 10){if (arr[i] > greed[k]){greed[k] = arr[i];set_index = i;}i++;}arr[set_index] = 0;k++;}cout << greed[0] << " " << greed[1] << " " << greed[2] << " " << greed[3] << endl;}
#include <iostream>#include <string> //Should already be in iostream#include <cstdlib>//A word score adds up the character values. a-z gets mapped to 1-26 for the values of the characters.//wordScore [wordValue]//Pipe in the input into stdin, or type the words yourself.//Lowercase words onlyint characterValue(const char b){return ((b >= 'a') && (b <= 'z'))? ((b - 'a') + 1) : 0;}int main(int argc, char** argv){//The first argument specifies if you are trying to look for a certain word scoreint wordValue = (argc > 1)? std::atoi(argv[1]) : 0;std::string line;while(std::getline(std::cin, line)){int sum = 0;for(const char c : line){sum += characterValue(c);}if(wordValue){ //If wordValue is 0 or the sum is the correct valueif(wordValue == sum){std::cout << line << std::endl;}} else {std::cout << sum << "\t" << line << std::endl;}}return 0;}
#include<iostream>using namespace std;const int rows = 8;const int cols = 8;char chessboard[rows][cols];void setBoard(char chessboard[][cols]);void printBoard(char chessboard[][cols]);void setBoard(char chessboard[][cols]) {for(int i = 0; i < rows; i++) {for(int j = 0; j < cols; j++) {if(i % 2 == 0 && j % 2 == 0) {chessboard[i][j] = 'x';} else {if(i % 2 != 0 && j % 2 == 1) {chessboard[i][j] = 'x';} else {chessboard[i][j] = '-';}}}}return;}void printBoard(char chessboard[][cols]) {for(int i = 0; i < rows; i++) {for(int j = 0; j < cols; j++) {cout << chessboard[i][j] << " ";}cout << endl;}return;}int main(int argc, char const *argv[]){setBoard(chessboard);printBoard(chessboard);return 0;}
#include <iostream>using namespace std;int main() {const int ROW_SIZE = 2;const int COLUMN_SIZE = 5; //establish all variablesint 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;}
#include <iostream>#include <fstream>#include <string>#include <cstring>using namespace std;//This program makes a new text file that contains all combinations of two letters.// aa, ab, ..., zy, zzint main(){string filename = "two_letters.txt";ofstream outFile;outFile.open(filename.c_str());if(!outFile.is_open()){cout << "Something's wrong. Closing..." << endl;return 0;}for(char first = 'a'; first <= 'z'; first++){for(char second = 'a'; second <= 'z'; second++){outFile << first << second << " ";}outFile << endl;}return 0;}
using namespace std;class Hash{int BUCKET; // No. of buckets// Pointer to an array containing bucketslist<int> *table;public:Hash(int V); // Constructor// inserts a key into hash tablevoid insertItem(int x);// deletes a key from hash tablevoid deleteItem(int key);// hash function to map values to keyint hashFunction(int x) {return (x % BUCKET);}void displayHash();};Hash::Hash(int b){this->BUCKET = b;table = new list<int>[BUCKET];}void Hash::insertItem(int key){int index = hashFunction(key);table[index].push_back(key);}void Hash::deleteItem(int key){// get the hash index of keyint index = hashFunction(key);// find the key in (inex)th listlist <int> :: iterator i;for (i = table[index].begin();i != table[index].end(); i++) {if (*i == key)break;}// if key is found in hash table, remove itif (i != table[index].end())table[index].erase(i);}// function to display hash tablevoid Hash::displayHash() {for (int i = 0; i < BUCKET; i++) {cout << i;for (auto x : table[i])cout << " --> " << x;cout << endl;}}// Driver programint main(){// array that contains keys to be mappedint a[] = {15, 11, 27, 8, 12};int n = sizeof(a)/sizeof(a[0]);// insert the keys into the hash tableHash h(7); // 7 is count of buckets in// hash tablefor (int i = 0; i < n; i++)h.insertItem(a[i]);// delete 12 from hash tableh.deleteItem(12);// display the Hash tableh.displayHash();return 0;}