1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <iostream> #include <string> using namespace std; class TicTacToe { private: char table[3][3]; public: TicTacToe() { } //insert numbers void setTable() { int n = 1; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { table[i][j] = '0' + n;//cast n to char n++; } } } //output table to screen void printTable() { for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) if(j < 2) { cout << table[i][j] << "|"; } else { cout << table[i][j] << endl; } if(i < 2) { cout << "-+-+-\n"; } } } //player move and set to X or O void playerMove(char num, char player) { bool wrongMove = true;//wrong move for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) {//mark square with X or O if(table[i][j] == num) { table[i][j] = player; wrongMove = false; } } } if(wrongMove == true) { cout << "Wrong move!\n"; } } //cheking for winers bool checkWinner(char player, bool gameOver) { //winning conditions for(int i = 0; i < 3; i++)//checking rows if(table[i][0] == table[i][1] && table[i][1] == table[i][2]) gameOver = true; for(int i = 0; i < 3; i++)//checking columns if(table[0][i] == table[1][i] && table[1][i] == table[2][i]) gameOver = true; //diagonals if(table[0][0] == table[1][1] && table[1][1] == table[2][2]) gameOver = true; if(table[0][2] == table[1][1] && table[1][1] == table[2][0]) gameOver = true; if(gameOver == true) { cout << "Player " << player << " wins!\n\n"; } return gameOver; } //checking for a draw bool checkDraw(bool gameOver) { int n = 1, count = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { //cheking if board is full if(table[i][j] == '0'+n) { count++; } n++; } } if(count < 1) { cout << "It's a draw!\n\n"; gameOver = true; } return gameOver; } }; int main() { bool done = false, gameOver = false; char player = 'O', num; TicTacToe myGame; myGame.setTable(); do { if(player == 'X') { player = 'O'; } else { player = 'X'; } myGame.printTable(); cout << "Player \"" << player << "\" turn or (q) to quit> "; cin >> num; cout << "\n"; if(num == 'q') { cout << "Goodbye!...\n"; break; } myGame.playerMove(num, player); gameOver = myGame.checkWinner(player, gameOver); gameOver = myGame.checkDraw(gameOver); //if game over, starting over if(gameOver == true) { myGame.setTable(); gameOver = false; } }while(!done); system("pause"); return 0; } |
BracketingSearch from cplusplus. The solution to computer guessing the number the user is thinking is achieve best by using Binary Search. A binary search locates a value in a sorted array by determining whether the value occurs in the first or second half, then repeating the search in one of the halves. In this case the computer start with a range 1 – 100, and using the num = (1 + 100) / 2 formula the computer continually adjust the range until it guesses the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | //Guessing game: the computer guess the //number that the user is thinking. //this is done by using Binary Search algorithm #include <iostream> using namespace std; int guessNumber(int& l, int& h, int& m, char c); void guessRight(int&, int&, int&, int&); int main() { int low = 0, high = 100, count = 0, mid = 0; char a; do { if(count == 0) //star game over { system("cls"); cout << "Think a number from 1 - 100. \n"; system("pause"); cout << "\n\n"; mid = guessNumber(low, high, mid, a); } count++;//count and menu cout << "Is your number " << mid << " ?\n"; cout << "Enter (c) for correct\n"; cout << "Enter (l) if number is low\n"; cout << "Enter (h) if number is high\n"; cout << "Enter (q) to quit\n"; cin >> a; if(a == 'c') { guessRight(count, high, low, mid); } else if(a == 'l') { mid = guessNumber(low, high, mid, a); } else if(a == 'h') { mid = guessNumber(low, high, mid, a); } else if(a == 'q') { cout << "Goodbye!\n"; break; } else { cout << "Wrong choice try again\n"; } }while(a != 'q'); system("pause"); return 0; } //binary search int guessNumber(int& low, int& high, int& mid, char a) { if(a == 'l') { low = mid + 1; }//if number is low if(a == 'h') { high = mid - 1; }//if number is high int m = (high + low) / 2; return m; } void guessRight(int& count, int& high, int& low, int& mid)//guess right { cout << "Congrats, you guessed in " << count << " tries!\n\n"; high = 100; low = 0; mid = 0; count = 0;//reseting all num system("pause"); } |
This is the solution to PancakeGlutton exercise from cplusplus.com. Printing out an array of number in descending or ascending order can be accomplish with out doing any type of sort. ok, let’s say you want to find out how may pancakes a every person ate, and then output in descending order starting with the most pancakes. In this case the output is in descending order, so all you have to do is find the biggest number and then decrease by one and print every number in the array. Here’s an example.
#include <iostream> #include <string> using namespace std; int main() { int persons[10]; for(int i = 0; i < 10; i++) { cout << "Enter how many pancakes person " << i << " ate: "; cin >> persons[i]; } //finding biggest and smallest int small = persons[0], big = persons[0], idSmall, idBig; for(int j = 0; j < 10; j++) { if(small > persons[j]) { small = persons[j]; idSmall = j; } if(big < persons[j]) { big = persons[j]; idBig = j; } } cout << "The person who ate the most pancakes > " << idBig << endl; cout << "The person who ate the least pancakes > " << idSmall << endl; //output in descending order starting with most pancakes for(big; big >= 0; big--) { for(int i = 0; i < 10; i++) { if(persons[i] == big) { cout << "Person " << i << " ate " << big << endl; } } } system("pause"); return 0; }
Finding whether a word or number is a palindrome is very common in programming languages. A palindrome word or number is one that reads the same in either direction. Words: bob, boob, ana… Numbers: 1, 2, 33, 121, 222, 3445443.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> #include <string> using namespace std; //finding palindromes int main() { cout << "Enter word or number: "; string temp, input; cin >> input; temp = input; reverse(input.begin(), input.end());//reversing input if(temp == input) { cout << temp << " is a palindrome!" << endl; } //checking for palin else { cout << temp << " is not a palindrome!" << endl; } system("pause"); return 0; } |
Finding the minimum and maximum in an array is a classical programming exercise in C++. Here’s how is done!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; //finding min and max in array int main() { int num[5] = { 2, 50, 100, 25, 78 }; int max = num[0], min = num[0]; //set max & min to some value for(int i = 0; i < 5; i++)//loop through array { if(num[i] > max) { max = num[i]; } //find max if(num[i] < min) { min = num[i]; } //find min } cout << "Maximum number: " << max << endl; cout << "Minimum number: " << min << endl; system("pause"); return 0; } |
A Prime number can only be divided by one and itself. For example; 13 can only be divided by 1 and 13. Here’s a C++ code that prints out the all prime numbers between 0 and 100.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | bool isPrime=true; int num = 100; for ( int i = 0; i <= num; i++) { for ( int j = 2; j <= num; j++) { if ( i!=j && i % j == 0 ) { isPrime=false; break; } } if (isPrime) { cout <<"Prime:"<< i << endl; } isPrime=true; } |