Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Converting easy C++ Code to MATLAB code? How would I convert the following into

ID: 3720945 • Letter: C

Question

Converting easy C++ Code to MATLAB code?

How would I convert the following into MATLAB code?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand(time(NULL));
int board[512][512] = {0};//Initializing to 0: default value
int hits[20][2] = {0};//count hits on each numbers for each players
int properHitsLeft[2]={20,20};//remaining distinct hits for each players
int count=0, x, y, score, times;
cout << "Dart game: round the world" << endl;
  
/* Construction of the board*/
board[256][256] = 50;//Bull's eye
/* Inner bull construction*/
board[255][255] = 25;
board[255][256] = 25;
board[255][257] = 25;
board[256][255] = 25;
board[256][257] = 25;
board[257][255] = 25;
board[257][256] = 25;
board[257][257] = 25;
for (score= 1; score<=20; score++) {// For score = 1 to 20
for (times= 0; times<20; times++) {//Fill board for 20 times
x = rand()%512;
y = rand()%512;
if (board[x][y] == 0) {
board[x][y] = score;
} else {
times--;//If already preoccupied by some other score-value, try again
}
}
}
  
/*Game starts*/
while (properHitsLeft[0]*properHitsLeft[1] != 0){
x = rand()%512;
y = rand()%512;
if (board[x][y] > 0 && board[x][y] < 21) {
//If shot inside the board excluding bull's eye and outer bull
if (hits[board[x][y]-1][count%2] == 0) {
properHitsLeft[count%2]--;
}
hits[board[x][y]-1][count%2]++;
}
count++;
}
cout<< "Total darts thrown: "<< count <<endl;
if (properHitsLeft[0] == 0) cout<< "Winner: Player 1 ";
else cout<< "Winner: Player 2 ";
cout<< "Darts thrown by player: "<< count/2 <<endl;
return 0;
}

Explanation / Answer

First of all, I should appreciate the question you have asked. As the conversion process takes time, I can't able to give you equivalent MAT code. But I can tell you the way. First thing is first, Automatic conversion is not there. You must use mex. See matlab help. You can not convert c++ code to matlab code but you can use this c++ code in matlab using mex.

Here are a couple of links might helpful for you:
http://www.mathworks.com/help/matlab/creating-c-c-and-fortran-programs-to-be-callable-from-matlab-mex-files.html
https://www.youtube.com/watch?v=3zdVQf1MotY

Final note:
Either a re-implementation is required in general, or you can call the C++ code from Matlab using the MEX-interface.?

Hope this helps.