## Saturday, March 28, 2020

### Quiz game in C Part 2: Multi-dimensional C array

In this article, we will discuss the gameplay() function but not all only the usages of one dimension, two-dimension, and third dimension character array. Choosing the correct dimension of the array is very important to store information effectively. We have to display a question, four options and store the correct answer to compare the answer provided by the user. Remember these three information are connected to each other if we mismatch the order then the game will go wrong.
Here, we will declare three character array to store questions, four options, and answer in the same order of the array index.

Question is a string which is an array of character and we will ask 10 questions in total. We create a two-dimension character array, the first index is a total number of questions i.e 10 and the second index is the length of a string.
``````    char question;
strcpy(question, "This is question no 1");
strcpy(question, "This is question no 2");
strcpy(question, "This is question no 3");
strcpy(question, "This is question no 4");
strcpy(question, "This is question no 5");
strcpy(question, "This is question no 6");
strcpy(question, "This is question no 7");
strcpy(question, "This is question no 8");
strcpy(question, "This is question no 9");
strcpy(question, "This is question no 10");
``````
We can't assign string using "=" to the two-dimension array and correct way to use it is strcpy() function.

Next, we create a three-dimension array to store four options for each question. A first index is a number of questions i.e 10, a second index is a number of options i.e 4 and a third index is the length of a string.
``````    char option = {
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
};``````
Now, we create a one-dimension array to store the answer corresponding to the questions.
``    char answer={'a','b','c','d','a','b','c','d','a','b'};``
``````void gamePlay(){
system("cls");
char question;
strcpy(question, "This is question no 1");
strcpy(question, "This is question no 2");
strcpy(question, "This is question no 3");
strcpy(question, "This is question no 4");
strcpy(question, "This is question no 5");
strcpy(question, "This is question no 6");
strcpy(question, "This is question no 7");
strcpy(question, "This is question no 8");
strcpy(question, "This is question no 9");
strcpy(question, "This is question no 10");

char option = {
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
{"option1","option2","option3","option4"},
};

char letter={'a','b','c','d'};

for(int i=0;i<10;i++){
printf("%s\n",question[i]);
for(int j=0;j<4;j++){
printf("%c) %s\n",letter[j],option[i][j]);
}