QUESTION: I AM VERY GOOD AT CODING IN VECTORS BUT MY PROFESSOR ONLY ACCEPTS MY P
ID: 3756084 • Letter: Q
Question
QUESTION: I AM VERY GOOD AT CODING IN VECTORS BUT MY PROFESSOR ONLY ACCEPTS MY PROGRAM USING ONLY ARRAYS I NEED HELP TO CONVERT THIS WORKING BULLS & COWS PROGRAM USING ONLY ARRAYS, WITHOUT FUNCTIONS, AND WITHOUT POINTERS TO VECTORS. PLEASE!
#include <iostream>
#include <math.h>
#include <vector>
#include <time.h>
using namespace std;
int main() {
int length = -1; //Initial length before input
int codeInt;
vector<int> code;
srand(time(NULL)); //Random seed based on time
while (length != 0 && length != 3 && length != 4 && length != 5) { //Until valid number
cout << "Enter number of digits in code (3, 4 or 5): "; //Must be 0, 3, 4, or 5
cin >> length;
if (length == 0) { //If cheat
cout << "Enter code: "; //Enter code
cin >> codeInt;
while (codeInt / 10000 > 9) { //If code length > 5
cout << "Code length must be less than 5... Enter your own code: ";//Get new code
cin >> codeInt; //Enter code
}
cout << "Enter number of digits in code: ";
cin >> length;
bool done = false;
while (done == false) {
if ((codeInt / (int)pow(10,length-1)) > 9) { //If length is too short
cout << "Something's not right, your length was";
cout << "probably too short... please try again. ";
cout << "Enter number of digits in code: "; //Get new length
cin >> length;
}
else if (length != 3 && length != 4 && length != 5) { //If length not right
cout << "Code length must be 3, 4, or 5... ";
cout << "Enter number of digits in code: ";
cin >> length; //Enter length
}
else { //Otherwise
done = true; //Move on
}
}
code.resize(length); //Set size of code
cout << "Number to guess: ";
for (int a = 0; a < length; a++) {
code[a] = codeInt / pow(10, length-a-1); //Itterates through magnitudes
codeInt = codeInt % (int)pow(10,length-a-1); //Stores code into vector
cout << code[a];
}
cout << endl;
} else {
bool duplicates = true;////////////////////////////////
while (duplicates) {//////////////////////////////////
duplicates = false;////////////////////////////////////
code.resize(length); //Set size of code
codeInt = rand() % (int)pow(10,length); //Random code
for (int a = 0; a < length; a++) {
code[a] = codeInt / pow(10,length-a-1); //Itterates through magnitudes
codeInt = codeInt % (int)pow(10,length-a-1); //Stores code into vector
}
for (int a = 0; a < length - 1; a++) {/////////////////
for (int b = a+1; b < length; b++) {///////////////////
if (code[a] == code[b]) {//////////////////////////////
duplicates = true;/////////////////////////////////////
}/////////////////////////////////////////////////////
}/////////////////////////////////////////////////////
}//////////////////////////////////////////////////////
}
}
int bulls, cows, guessInt;
vector<int> guess;
guess.resize(length);
while (bulls != length) { //Until you've won
bulls = cows = 0;
bool duplicates = true;
while (duplicates == true) {
duplicates = false;
cout << "Enter guess: "; //Enter guess
cin >> guessInt;
for (int a = 0; a < length; a++) {
guess[a] = guessInt / pow(10, length-a-1); //Itterates through magnitudes
guessInt = guessInt % (int)pow(10,length-a-1); //Stores guess into vector
}
for ( int a = 0; a < length -1; a++) {
for ( int b = a+1; b < length; b++) {
if (guess[a] == guess[b]) {
if (duplicates == false) {
cout << "Each number must be different. ";
}
duplicates = true;
}
}
}
}
for (int a = 0; a < length; a++) {
if (guess[a] == code[a]) { //If you've gotten a bull
bulls++; //Increment bulls
}
for (int b = 0; b < length; b++) {
if (guess[a] == code[b]) { //If you've gotten a cow
cows++; //Increment cows
}
}
} //Because bulls also add a cow
cows -= bulls; //Remove excess cows
if (cows + bulls > length) { //Remove excess cows
cows = length - bulls; //Remove excess cows
}
if (bulls != length) {
cout << bulls << " bulls " << cows << " cows! "; //Display bulls and cows
}
else {
cout << bulls << " bulls - ";
for ( int i = 0; i < guess.size(); i++) {
cout << guess[i];
}
cout << " is correct! ";
}
}
return 0;
}
Explanation / Answer
Instead of vector, an array can be used. First an int pointer is declared. Then after the length is obtained, memory is allocated to the pointer using malloc. Instead of size function, 'length' is used as the length of the array is length.
Please find the modified code below:
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
int main() {
int length = -1; //Initial length before input
int codeInt;
int *code; //int pointer for code
srand(time(NULL)); //Random seed based on time
while (length != 0 && length != 3 && length != 4 && length != 5) { //Until valid number
cout << "Enter number of digits in code (3, 4 or 5): "; //Must be 0, 3, 4, or 5
cin >> length;
if (length == 0) { //If cheat
cout << "Enter code: "; //Enter code
cin >> codeInt;
while (codeInt / 10000 > 9) { //If code length > 5
cout << "Code length must be less than 5... Enter your own code: ";//Get new code
cin >> codeInt; //Enter code
}
cout << "Enter number of digits in code: ";
cin >> length;
bool done = false;
while (done == false) {
if ((codeInt / (int)pow(10,length-1)) > 9) { //If length is too short
cout << "Something's not right, your length was";
cout << "probably too short... please try again. ";
cout << "Enter number of digits in code: "; //Get new length
cin >> length;
}
else if (length != 3 && length != 4 && length != 5) { //If length not right
cout << "Code length must be 3, 4, or 5... ";
cout << "Enter number of digits in code: ";
cin >> length; //Enter length
}
else { //Otherwise
done = true; //Move on
}
}
code = (int *)malloc(length * sizeof(int));//Set size of code
cout << "Number to guess: ";
for (int a = 0; a < length; a++) {
code[a] = codeInt / pow(10, length-a-1); //Itterates through magnitudes
codeInt = codeInt % (int)pow(10,length-a-1); //Stores code into vector
cout << code[a];
}
cout << endl;
} else {
bool duplicates = true;////////////////////////////////
while (duplicates) {//////////////////////////////////
duplicates = false;////////////////////////////////////
code = (int *)malloc(length * sizeof(int)); //Set size of code
codeInt = rand() % (int)pow(10,length); //Random code
for (int a = 0; a < length; a++) {
code[a] = codeInt / pow(10,length-a-1); //Itterates through magnitudes
codeInt = codeInt % (int)pow(10,length-a-1); //Stores code into vector
}
for (int a = 0; a < length - 1; a++) {/////////////////
for (int b = a+1; b < length; b++) {///////////////////
if (code[a] == code[b]) {//////////////////////////////
duplicates = true;/////////////////////////////////////
}/////////////////////////////////////////////////////
}/////////////////////////////////////////////////////
}//////////////////////////////////////////////////////
}
}
int bulls, cows, guessInt;
int *guess; // int pointer for guess
guess = (int *)malloc(length * sizeof(int)); //set size of guess
while (bulls != length) { //Until you've won
bulls = cows = 0;
bool duplicates = true;
while (duplicates == true) {
duplicates = false;
cout << "Enter guess: "; //Enter guess
cin >> guessInt;
for (int a = 0; a < length; a++) {
guess[a] = guessInt / pow(10, length-a-1); //Itterates through magnitudes
guessInt = guessInt % (int)pow(10,length-a-1); //Stores guess into vector
}
for ( int a = 0; a < length -1; a++) {
for ( int b = a+1; b < length; b++) {
if (guess[a] == guess[b]) {
if (duplicates == false) {
cout << "Each number must be different. ";
}
duplicates = true;
}
}
}
}
for (int a = 0; a < length; a++) {
if (guess[a] == code[a]) { //If you've gotten a bull
bulls++; //Increment bulls
}
for (int b = 0; b < length; b++) {
if (guess[a] == code[b]) { //If you've gotten a cow
cows++; //Increment cows
}
}
} //Because bulls also add a cow
cows -= bulls; //Remove excess cows
if (cows + bulls > length) { //Remove excess cows
cows = length - bulls; //Remove excess cows
}
if (bulls != length) {
cout << bulls << " bulls " << cows << " cows! "; //Display bulls and cows
}
else {
cout << bulls << " bulls - ";
for ( int i = 0; i < length; i++) {
cout << guess[i];
}
cout << " is correct! ";
}
}
return 0;
}
}