CS 107 - 7/20/15 char letterGrid [4][4]; // option 1: setting up the dice as 16 strings string dice [16]; dice[0] = "ACETWZ"; int r = rand() % 6; char letter = dice[0].at(r); // option 2: setting up the dice as 16 character array of 6 each // 2-d character array char dice2 [16][6]; dice2[0][0] = 'A'; dice2[0][1] = 'C'; dice2[0][2] = 'E'; dice2[0][3] = 'T'; dice2[0][4] = 'W'; dice2[0][5] = 'Z'; int r = rand() % 6; char letter = dice2[0][r]; // how do I get a radom dice into each position on the grid. char letterGrid [4][4]; letterGrid[0][0] = letter; string dice[16]; int dicePosition[16]; // initialize the values for ( int i = 0 ; i < 16 ; i ++) dicePositions[i] = i; //randomize the dicePosition values for ( int i = 0 ; i < 16 ; i ++) { int x = rand() % 16; // exchange the value at position i and X int temp = dicePosition[i]; dicePosition[i] = dicePosition[x]; dicePosition[x] = temp; } //to access my 16 letters for (int a = 0 ; a < 16 ; a++) { int r = rand() % 6; int dp = dicePosition[a]; char letter = dice[dp].at(r); } // an alternate way to write the above loop accessing the 16 letters // using a double nested for loop. int b = 0; for ( int m = 0 ; m < 4 ; m++ ) for (int n = 0 ; n < 4 ; n++ ) { int r = rand() % 6; // int a = m * 4 + n; int dp = dicePosition[b]; char letter = dice[dp].at(r); letterGrid[m][n] = letter; b++; } When we want a random number in the range from 1 to N (rand() % N) + 1 Three require functions 1. create the grid of letters 2. display the grid of letters 3. deal with 3 minutes of user input main () { char letterGrid[4][4]; createGrid ( letterGrid , ... ); displayGrid ( letterGrid , ); handleUserInput ( ); } void createGrid ( char letterGrid[4][4], ... ) { } ----------------------------------------------------- From Last Time, File Input and output. // basic file operations #include #include using namespace std; int main () { // part 1 - create variable ofstream myfile; // part 2 - open the file myfile.open ("example.txt"); // part 3 - use the file myfile << "Writing this to a file.\n"; // part 4 - close the file myfile.close(); return 0; } When reading from a file, we can detect when the End Of a File is reached using the eof() method of the ifstream class/object. eof() return true when the end of the file has been reached. For input, the typical program structure becomes // part 1 - create the varialbe ifstream myInputFile; // part 2 - open the file myInputFile.open ("userData.txt"); // part 3 - use the file "read until End of File is reached" while ( ! myInputFile.eof() ) { // read in the input using >> // process the input value } // part 4 - close the file myInputFile.close();