• Nov 18, 2022 •AustinLeath
0 likes • 15 views
#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; }
• Nov 19, 2022 •CodeCatch
0 likes • 0 views
#include <iostream> #include <vector> using namespace std; void swap(int *a, int *b) { int temp = *b; *b = *a; *a = temp; } void heapify(vector<int> &hT, int i) { int size = hT.size(); int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < size && hT[l] > hT[largest]) largest = l; if (r < size && hT[r] > hT[largest]) largest = r; if (largest != i) { swap(&hT[i], &hT[largest]); heapify(hT, largest); } } void insert(vector<int> &hT, int newNum) { int size = hT.size(); if (size == 0) { hT.push_back(newNum); } else { hT.push_back(newNum); for (int i = size / 2 - 1; i >= 0; i--) { heapify(hT, i); } } } void deleteNode(vector<int> &hT, int num) { int size = hT.size(); int i; for (i = 0; i < size; i++) { if (num == hT[i]) break; } swap(&hT[i], &hT[size - 1]); hT.pop_back(); for (int i = size / 2 - 1; i >= 0; i--) { heapify(hT, i); } } void printArray(vector<int> &hT) { for (int i = 0; i < hT.size(); ++i) cout << hT[i] << " "; cout << "\n"; } int main() { vector<int> heapTree; insert(heapTree, 3); insert(heapTree, 4); insert(heapTree, 9); insert(heapTree, 5); insert(heapTree, 2); cout << "Max-Heap array: "; printArray(heapTree); deleteNode(heapTree, 4); cout << "After deleting an element: "; printArray(heapTree); }
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! }
• Jun 30, 2023 •Iceman_71
0 likes • 7 views
#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; }
• Jun 17, 2024 •oceantran27
0 likes • 3 views
#include <iostream> using namespace std; int main { cout << 1; }
• Dec 24, 2021 •aedrarian
3 likes • 21 views
/* Good morning! Here's your coding interview problem for today. This problem was asked by Stripe. Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3. You can modify the input array in-place. */ #include <iostream> using namespace std; int calcMissing(int* input, int size) { int sum = 0; int n = 1; //add one to account for missing value for(int i = 0; i < size; i++) { if(input[i] > 0) { sum += input[i]; n++; } } //If no numbers higher than 0, answer is 1 if(sum == 0) return 1; return (n*(n+1)/2) - sum; //Formula is expectedSum - actualSum /* expectedSum = n*(n+1)/2, the formula for sum(1, n) */ } int main() { cout << calcMissing(new int[4]{3, 4, -1, 1}, 4) << endl; cout << calcMissing(new int[3]{1, 2, 0}, 3) << endl; //No positive numbers cout << calcMissing(new int[1]{0}, 1) << endl; }