// 1) prompt, receive input, report input // 2) prompt, receive input, report input in a loop // 3) replace 4 variables with an array of 4 elements // 4) convert 4 integer inputs to 1 string input // 5) add hard-wired answer array and compute "totally correct" response // 6) populate answer array with random values // 7) compute "partially correct" response #include #include #include using std::cout; using std::cin; using std::string; using std::stringstream; #if 0 // 1) prompt, receive input, report input int main() { string first, second, third, fourth; cout << "Input: "; cin >> first >> second >> third >> fourth; cout << " "; cout << first << ' ' << second << ' ' << third << ' ' << fourth << '\n'; } // Input: 4 x 2 one // 4 x 2 one // 2) prompt, receive input, report input int main() { int first, second, third, fourth; while (true) { cout << "Input: "; cin >> first >> second >> third >> fourth; cout << " "; cout << first << ' ' << second << ' ' cout << third << ' ' << fourth << '\n'; } } // Input: 4 3 2 1 // 4 3 2 1 // Input: 1 2 3 4 5 // 1 2 3 4 // Input: 6 7 8 // 5 6 7 8 // 3) replace 4 variables with an array of 4 elements int main() { int input[4]; while (true) { cout << "Input: "; for (int i=0; i < 4; i = i + 1) cin >> input[i]; cout << " "; for (int i=0; i < 4; i = i + 1) cout << input[i] << ' '; cout << '\n'; } } // 4) convert 4 integer inputs to 1 string input int main() { string input_string; int input[4]; while (true) { cout << "Input: "; getline( cin, input_string ); if (input_string == "quit") break; stringstream ss; ss << input_string; for (int i=0; i < 4; i = i + 1) ss >> input[i]; cout << " "; for (int i=0; i < 4; i = i + 1) cout << input[i] << ' '; cout << '\n'; } } // Input: 1 2 3 4 // 1 2 3 4 // Input: 5 6 7 // 5 6 7 4 // Input: 8 9 1 2 3 // 8 9 1 2 // Input: quit // 5) add hard-wired answer array and compute "totally correct" response int main() { string input_string; int input[4]; int answer[] = { 2, 6, 4, 1 }; int num_correct = 0; while (true) { cout << "Input: "; getline( cin, input_string ); if (input_string == "quit") break; if (input_string == "answer") { cout << " "; for (int i=0; i < 4; i = i + 1) cout << answer[i] << ' '; cout << '\n'; continue; } stringstream ss; ss << input_string; for (int i=0; i < 4; i = i + 1) ss >> input[i]; for (int i=0; i < 4; i = i + 1) if (input[i] == answer[i]) num_correct = num_correct + 1; cout << " " << num_correct << '\n'; if (num_correct == 4) break; num_correct = 0; } } // Input: 1 3 2 4 // 0 // Input: answer // 2 6 4 1 // Input: 2 6 1 4 // 2 // Input: 2 6 4 1 // 4 // 6) populate answer array with random values int main() { string input_string; int input[4]; int answer[4]; srand( time( 0 ) ); for (int i=0; i < 4; i = i + 1) answer[i] = rand() % 6 + 1; int num_correct = 0; while (true) { cout << "Input: "; getline( cin, input_string ); if (input_string == "quit") break; if (input_string == "answer") { cout << " "; for (int i=0; i < 4; i = i + 1) cout << answer[i] << ' '; cout << '\n'; continue; } stringstream ss; ss << input_string; for (int i=0; i < 4; i = i + 1) ss >> input[i]; for (int i=0; i < 4; i = i + 1) if (input[i] == answer[i]) num_correct = num_correct + 1; cout << " " << num_correct << '\n'; if (num_correct == 4) break; num_correct = 0; } } // Input: 1 3 5 2 // 1 // Input: answer // 1 4 4 3 // Input: 1 4 3 4 // 2 // Input: 1 4 4 3 // 4 // 7a) compute "partially correct" response (exactly) void evaluate_guess( int answer[], int guess[], int& totally_correct, int& partially_correct ) { bool guess_used[4] = { false, false, false, false }; bool answer_used[4] = { false, false, false, false }; totally_correct = partially_correct = 0; // Compute the totally correct value (from the previous increment) for (int i=0; i < 4; i = i + 1) if (guess[i] == answer[i]) { totally_correct = totally_correct + 1; guess_used[i] = answer_used[i] = true; } // Compute the partially correct value for (int i=0; i < 4; i = i + 1) { // step thru the guess if (guess_used[i]) continue; for (int j=0; j < 4; j = j + 1) { // step thru the answer // If this slot in answer has been previously used, skip it if (answer_used[j]) continue; if (guess[i] == answer[j]) { partially_correct = partially_correct + 1; guess_used[i] = answer_used[j] = true; // Now that this guess slot has been "bound" to an answer // slot, don't try to match it to another answer slot break; } } } } int main() { string input_string; int guess[4]; int answer[4]; srand( time( 0 ) ); for (int i=0; i < 4; i = i + 1) answer[i] = rand() % 6 + 1; int num_correct, num_partial_correct; while (true) { cout << "Input: "; getline( cin, input_string ); if (input_string == "quit") break; if (input_string == "answer") { cout << " "; for (int i=0; i < 4; i = i + 1) cout << answer[i] << ' '; cout << '\n'; continue; } stringstream ss; ss << input_string; for (int i=0; i < 4; i = i + 1) ss >> guess[i]; evaluate_guess( answer, guess, num_correct, num_partial_correct ); cout << " " << num_correct << " " << num_partial_correct << '\n'; if (num_correct == 4) break; } } // Input: 1 1 2 2 // 1 1 // Input: 3 3 4 4 // 0 1 // Input: 1 4 1 5 // 1 1 // Input: answer // 4 2 2 5 // Input: 2 4 2 5 // 2 2 // Input: 4 2 2 5 // 4 0 // 7b) compute "partially correct" response (subtract black) int min( int one, int two ) { if (one < two) return one; else return two; } void evaluate_guess( int answer[], int answer_counts[], int guess[], int& totally_correct, int& partially_correct ) { int guess_counts[7] = { 0,0,0,0,0,0,0 }; totally_correct = partially_correct = 0; // Count the number of occurrences of each digit in the guess // [answer_counts was computed in main()] for (int i=0; i < 4; i = i + 1) guess_counts[ guess[i] ] = guess_counts[ guess[i] ] + 1; // Over-compute the partially_correct value for (int i=1; i < 7; i = i + 1) partially_correct = partially_correct + min( guess_counts[i], answer_counts[i] ); // Compute the totally_correct value for (int i=0; i < 4; i = i + 1) if (guess[i] == answer[i]) totally_correct = totally_correct + 1; // Remove the totally_correct value from the partially_correct value partially_correct = partially_correct - totally_correct; } int main() { string input_string; int guess[4]; int answer[4]; int answer_counts[7] = { 0 }; srand( time( 0 ) ); for (int i=0; i < 4; i = i + 1) answer[i] = rand() % 6 + 1; for (int i=0; i < 4; i = i + 1) answer_counts[ answer[i] ] = answer_counts[ answer[i] ] + 1; int num_correct, num_partial_correct; while (true) { cout << "Input: "; getline( cin, input_string ); if (input_string == "quit") break; if (input_string == "answer") { cout << " "; for (int i=0; i < 4; i = i + 1) cout << answer[i] << ' '; cout << '\n'; continue; } stringstream ss; ss << input_string; for (int i=0; i < 4; i = i + 1) ss >> guess[i]; evaluate_guess( answer, answer_counts, guess, num_correct, num_partial_correct ); cout << " " << num_correct << " " << num_partial_correct << '\n'; if (num_correct == 4) break; } } #endif