In: Computer Science
Explanation
To solve the following problem we will require two header files:
We will start by declaring some variables for loop control and array size. Here we will declare the desired array of character type with the given size (10x10).
To generate a random letter we will need to create an array that will store all the possible letters to be picked. hence we will create an array alphabet with possible letters.
now we are ready for storing random values in the final array. We will create a nested loop and move row-wise. at each index first we will create a random number between 0 to 51. which will be in the range of all possible alphabet array. then we will store the letter at that position to the final 2D array.
once all the indexes are full we will display the array using nested loops again. This time we will print the value at the specified index followed by black space for a clear view and will also change line at end of each row.
---------------------------------------------------------------------code--------------------------------------------------------------------------
#include <iostream>
#include <stdlib.h> //for rand() funcion
using namespace std;
int main()
{
int i,j,size=10,MAX=52,RandIndex; //declaring variables for loop
and size of arrays
char array_random[size][size]; //declaring array to store random
letters of specified size
//creating an array witha all posible letters
char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z'};
//generating and storing random letters from alphabet array in the
10x10 random array
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
//generating a random number between 0 to 51
RandIndex = rand() % MAX;
//storing the letter at that index to random array
array_random[i][j]=alphabet[RandIndex];
}
}
//displaying the generated array with random letters
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
//printing leters with at each specific index followed by a blank
space
cout << array_random[i][j]<<" ";
}
cout<<endl; //changing line after each row completion
}
}