Question

Question 1) Suppose a program has the following code: const int X = 2, Y =...

Question 1)

Suppose a program has the following code:

const int X = 2, Y = 3;

int sum;

int values[X][Y] = {{1, 2, 3},

                                 {4, 5, 6}};

for(int i=0; i<X; i++)

{

     sum=0;

     for(int j=0; j<Y; j++)

        sum+=values[i][j];

   cout<<sum<<endl;

}

What is this program getting the sum of?

Group of answer choices

D-) The columns of the 2D array

C-) The rows of the 2D array

A-) All of the elements of the 2D array

B-) The diagonals of the 2D array

Question 2

In the game of tic-tac-toe, two players (‘X’ and ‘O’) play against each other on a 3X3 (three by three) grid with 9 (nine) squares. There are three ways to win the game:

  • Get three of your symbol (‘X’ or ‘O’) in a row
  • Get three of your symbol (‘X’ or ‘O’) in a column
  • Get three of your symbol (‘X’ or ‘O’) diagonally

For example, if the current state of the game is:

O

X

O

X

X

O

Then player X has won because they got three X’s in a column.

Like many board games, the game of tic-tac-toe can be represented by a 2D array. Suppose the game board is represented by a 2D array named arr of type char (each index stores ‘X’ or ‘O’). To determine if a player has won the game, there are several cases that have to be checked. Which of the options below correctly determines if player X has won the game by getting three X’s in a column?

Group of answer choices

A)

if((arr[0][0] == ‘X’ || arr[0][1] == ‘X’ || arr[0][2] == ‘X’) &&

   (arr[1][0] == ‘X’ || arr[1][1] == ‘X’ || arr[1][2] == ‘X’) &&

  (arr[2][0] == ‘X’ || arr[2][1] == ‘X’ || arr[2][2] == ‘X’))

                                 cout<<”Player X has won by getting three X’s in a column”;

B)

if((arr[0][0] == ‘X’ && arr[0][1] == ‘X’ && arr[0][2] == ‘X’) ||

   (arr[1][0] == ‘X’ && arr[1][1] == ‘X’ && arr[1][2] == ‘X’) ||

  (arr[2][0] == ‘X’ && arr[2][1] == ‘X’ && arr[2][2] == ‘X’))

                                 cout<<”Player X has won by getting three X’s in a column”;

C)

if((arr[0][0] == ‘X’ || arr[1][0] == ‘X’ || arr[2][0] == ‘X’) &&

   (arr[0][1] == ‘X’ || arr[1][1] == ‘X’ || arr[2][1] == ‘X’) &&

  (arr[0][2] == ‘X’ || arr[1][2] == ‘X’ || arr[2][2] == ‘X’))

                                 cout<<”Player X has won by getting three X’s in a column”;

D)

if((arr[0][0] == ‘X’ && arr[1][0] == ‘X’ && arr[2][0] == ‘X’) ||

   (arr[0][1] == ‘X’ && arr[1][1] == ‘X’ && arr[2][1] == ‘X’) ||

(arr[0][2] == ‘X’ && arr[1][2] == ‘X’ && arr[2][2] == ‘X’))

                               cout<<”Player X has won by getting three X’s in a column”;

Question 3)

Suppose a program is supposed to search an array to see if a value occurs in the array. For example, if the array arr = [1, 2, 3, 4, 5, 6] and the search value search_value = 6, then the program would print out “Search value found”, because 6 occurs in the array. On the other hand, if the array arr = [1, 2, 3, 4, 5, 6], and the search value search_value = 7, the program would print out “Search value NOT found”, because 7 does NOT occur in the array.

NOTE: arr is an array storing numbers, SIZE is the size of the array, and search_value is the value that is being searched for in the array.

Which of the options below is the correct implementation of the program?

Group of answer choices

int i=0;

while(i < SIZE && arr[i] != search_value)

      i++;

if(i == SIZE)

       cout<<"Search value NOT found"<<endl;

else

    cout<<"Search value found"<<endl;

int i=0;

while(i < SIZE && arr[i] != search_value)

      i++;

if(i == SIZE)

       cout<<"Search value found"<<endl;

else

    cout<<"Search value NOT found"<<endl;

int i=0;

while(i < SIZE && arr[i] == search_value)

      i++;

if(i == SIZE)

       cout<<"Search value found"<<endl;

else

    cout<<"Search value NOT found"<<endl;

int i=0;

while(i < SIZE && arr[i] == search_value)

      i++;

if(i == SIZE)

       cout<<"Search value NOT found"<<endl;

else

    cout<<"Search value found"<<endl;

Question 4)

Suppose a program has the following 4X4 2D array with 16 (sixteen) cells:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

The goal of the program is to print out the four bolded diagonal values in the following order:

16, 11, 6, 1

Which of the options below is the correct implementation?

Group of answer choices

A-)

for(int curr_row=0, curr_col=0; curr_row<ROWS && curr_col<COLS;

curr_row++, curr_col++)

             cout<<arr[curr_row][curr_col]<<endl;

C-)

for(int curr_row=0, curr_col=COLS-1; curr_row<ROWS && curr_col>=0;

curr_row++, curr_col--)

             cout<<arr[curr_row][curr_col]<<endl;

D-)

for(int curr_row=ROWS-1, curr_col=0; curr_row>=0 && curr_col<COLS;

curr_row--, curr_col++)

             cout<<arr[curr_row][curr_col]<<endl;

B-)

for(int curr_row=ROWS-1, curr_col=COLS-1; curr_row>=0 && curr_col>=0;

curr_row--, curr_col--)

             cout<<arr[curr_row][curr_col]<<endl;

Question 5)

NOTE: ARR_SIZE is the size of the array letters, NUM_OF_FILE_LINES is the number of lines in the file that is associated with the file object inputfile, and count is initialized to zero (0).

Assume that NUM_OF_FILE_LINES is greater than or equal to ARRAY_SIZE. If NUM_OF_FILE_LINES is greater than ARRAY_SIZE, then only the first ARRAY_SIZE lines from the file will be read into the array.

All of the options below will correctly read the contents of a file into an array EXCEPT:

Group of answer choices

while(count < ARR_SIZE && inputfile >> letters[count])

          count++;

while(inputfile >> letters[count] && count < ARR_SIZE)

          count++;

for(int i=0; i<NUM_OF_FILE_LINES; i++)

      {                      

        if(i < ARR_SIZE)

               inputfile >> letters[i];

        else

             break;

      }

for(int i=0; i<ARR_SIZE; i++)

                              inputfile >> letters[i];

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Question 1)

Suppose a program has the following code:

const int X = 2, Y = 3;

int sum;

int values[X][Y] = {{1, 2, 3},

                                 {4, 5, 6}};

for(int i=0; i<X; i++)

{

     sum=0;

     for(int j=0; j<Y; j++)

        sum+=values[i][j];

   cout<<sum<<endl;

}

What is this program getting the sum of?

Answer: D-) The columns of the 2D array.

Explanation: The program loops through each row, initializes sum to 0, and then loop through each column in current row, adding each element to sum, and finally displays the sum after the inner loop terminates. And then moves on to the next row.

Question 2)

In the game of tic-tac-toe, two players (‘X’ and ‘O’) play against each other on a 3X3 (three by three) grid with 9 (nine) squares. There are three ways to win the game:

Get three of your symbol (‘X’ or ‘O’) in a row

Get three of your symbol (‘X’ or ‘O’) in a column

Get three of your symbol (‘X’ or ‘O’) diagonally

Which of the options below correctly determines if player X has won the game by getting three X’s in a column?

Answer: option B.

if((arr[0][0] == ‘X’ && arr[0][1] == ‘X’ && arr[0][2] == ‘X’) ||

   (arr[1][0] == ‘X’ && arr[1][1] == ‘X’ && arr[1][2] == ‘X’) ||

(arr[2][0] == ‘X’ && arr[2][1] == ‘X’ && arr[2][2] == ‘X’))

                                 cout<<”Player X has won by getting three X’s in a column”;

Explanation: This if statement checks if the columns at first row is all X’ or if the columns at second row is all X’ or if the columns at third row is all X’. If either one is true, “Player X has won by getting three X’s in a column” is printed.

Question 3)

Suppose a program is supposed to search an array to see if a value occurs in the array. For example, if the array arr = [1, 2, 3, 4, 5, 6] and the search value search_value = 6, then the program would print out “Search value found”, because 6 occurs in the array. On the other hand, if the array arr = [1, 2, 3, 4, 5, 6], and the search value search_value = 7, the program would print out “Search value NOT found”, because 7 does NOT occur in the array.

NOTE: arr is an array storing numbers, SIZE is the size of the array, and search_value is the value that is being searched for in the array.

Which of the options below is the correct implementation of the program?

Answer: Option numbers/labels are not visible, so I’m just writing down the code of correct answer:

int i=0;

while(i < SIZE && arr[i] != search_value)

      i++;

if(i == SIZE)

       cout<<"Search value NOT found"<<endl;

else

    cout<<"Search value found"<<endl;

Explanation: The code first initializes i to 0. Then it loops as long as i is a valid index (0 to SIZE-1) and element at i is not search_value. In every loop, the value of i is incremented. So after the loop, if i is SIZE, then we have not found search value in the array, otherwise, we found the search value at index i.

Question 4)

Suppose a program has the following 4X4 2D array with 16 (sixteen) cells:

The goal of the program is to print out the four bolded diagonal values in the following order:

16, 11, 6, 1

Which of the options below is the correct implementation?

Answer: option B-)

for(int curr_row=ROWS-1, curr_col=COLS-1; curr_row>=0 && curr_col>=0;

curr_row--, curr_col--)

             cout<<arr[curr_row][curr_col]<<endl;

Explanation: this option will start at last row and last column, prints the value, then moves to second last row, second last column, prints the next value and continue this upto first row, first column.

Question 5)

NOTE: ARR_SIZE is the size of the array letters, NUM_OF_FILE_LINES is the number of lines in the file that is associated with the file object inputfile, and count is initialized to zero (0).

Assume that NUM_OF_FILE_LINES is greater than or equal to ARRAY_SIZE. If NUM_OF_FILE_LINES is greater than ARRAY_SIZE, then only the first ARRAY_SIZE lines from the file will be read into the array.

All of the options below will correctly read the contents of a file into an array EXCEPT:

Answer: Since option numbers are not visible, I’m just writing down the code of correct answer (wrong option):

while(inputfile >> letters[count] && count < ARR_SIZE)

          count++;

Explanation: Out of all options given, this must be the wrong one. This is because, imagine if count currently equals ARR_SIZE. i.e the array is full, then when the while loop executes the condition at hand side of the && operator, which is inputfile >> letters[count], will cause error as count is not a valid index of letters array. So count < ARR_SIZE must be on the left side of && operator.

Add a comment
Know the answer?
Add Answer to:
Question 1) Suppose a program has the following code: const int X = 2, Y =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • XCORE Question 1 Consider the following program void Elint x, int y Y = y +...

    XCORE Question 1 Consider the following program void Elint x, int y Y = y + 1 cout<<x<<"*«y << endl; void nain) 1 int i, a13): all) = 15; a 2) - 203 a13) = 25; cout <i«"" <all) <<"" << a12) << ""« a[3] << endl; cout <i<** <all) << "" << a12) <<""« a[3] << endl; What values of the variable and array A are printed with the following rules. a parameters are passed by value bi parameters...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT