Question

In: Computer Science

Suppose you have declared a character array called board of size 9 for a game by...

Suppose you have declared a character array called board of size 9 for a game by the declaration char board[9] and you want to initialize it with a loop so that each array member has a space character each at the beginning of the game. Which one of the following loops is the correct initialization?

Solutions

Expert Solution

The below-given loop is the correct initialization:

for(int i=0; i<3; i++) /*outer loop for accessing ith row of the board*/ 
{
for(int j=0;j<3;j++) /*inner loop for accessing jth column or element of the particular ith row of the board*/
{
 board[i][j] = ' '; /* intialize each element of the board with space enclosed between ''*/
}
}

Explanation: I have written comments to explain the code. Please refer to them for a better understanding. Since it is a board game with a board size of 9, it means that there would be the same number of rows and columns in the board because a board is usually a square containing rows and columns. Means, it should be a 2-dimensional array of order [nXn] elements So, in order to have same number of rows and columns, there will be 3 rows and 3 columns respectively to have a total of 9 elements. And also, since it is a character array, so each array member will be initialized with a space character by including a space enclosed between ' '. (like ' <space> '). Just hit the space button enclosed between the ' ' to initialize them.

This is because a variable/element of character datatype is initialized with the desired character enclosed between ' '.

Thanks!


Related Solutions

Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which...
Suppose you have a class with 9 students. Create an Array called StudentName and StudentGrade which contains the student's name and the student's final grade. Create a function called "GetStudentInfo" for who you will pass the arrays and the user will enter the data for those 9 students. Back in main, loop through both arrays and display the information for each student after its entered in GetStudentInfo I need a C++ code for visual studio. Can anyone help me? I'll...
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as...
JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as follows: int[] creditScores = {670, 720, 815};? a. 0 b. 1 c. 2 d. 3 2. Which of the following correctly assigns the value 100 to each of the array element? Assume the array is declared as int [] num = new int[4] a. for(x=0;x<3;++x) num[x]=100; c. for(x=1;x<4;++x) num[x]=100 ; 3. Consider the following code fragment int[][] rectangle = new int[4][2]; What is the...
You must program the game called MaDi which is described below. There is a board of...
You must program the game called MaDi which is described below. There is a board of NxN (the size N is defined by the user and must be a number greater than or equal to 2), where in each box there is an instruction. The first (the [0] [0]) and the last box (the [N-1] [N-1]) have no instruction. Possible instructions are: 1. Don't move 2. Advance 4 places 3. Jump to the next row 4. Go back 2 places...
in java Suppose that an array is declared and constructed as below. Each part of this...
in java Suppose that an array is declared and constructed as below. Each part of this problem is independent (i.e. the answer to b) should not depend on your answer to a). int[] scores = {10, 8, 6, 4, 2, 0}; a) What is scores[1]? b) What is scores.length? c) What is scores[5]? d) What array will the method call below return? Read the API carefully Arrays.copyOfRange(scores, 2, 4) e) What will the method call below return? Read the API...
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice,...
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice, what is the probability that: a. You land on Jail b. You land on any Light Blue property (Connecticut Ave, Vermont Ave, or Oriental Ave.) c. You land on Mediterranian Ave d. You land on Chance or Community Chest 2. Suppose you are on “Jail” a. What space (or spaces) is/are most likely to be landed on on your next roll? Justify your response....
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT