Question

In: Computer Science

Write a C++ program that makes a 10x10 array and picks random letters and displays in...

Write a C++ program that makes a 10x10 array and picks random letters and displays in different positions

Solutions

Expert Solution

Explanation

To solve the following problem we will require two header files:

  • iostream -> for cout method
  • stdlib.h -> for rand() function to generate random values

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
}
}


Related Solutions

In C++, write a program that will read up to 10 letters (characters) into an array...
In C++, write a program that will read up to 10 letters (characters) into an array and write the letters back to the screen in the reverse order. The program should prompt the user to input all values and can determine whether the input has ended by a punctuation mark such as ‘.’ (make sure that you’ll include meaningful and intuitive messages to the user). For example, if the input is abcde, the output should be edcba. Include appropriate messaging...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10 cells. Your program should start with * on position (2,3) i.e. 3rd column of 2nd row. After printing the grid and ‘*’, your program will ask user if he wants to move the ‘*’ Up, Down, Left or Right. If user presses ‘U’, ‘D’, ‘L’ or ‘R’, your program will move the ‘*’ one position accordingly. If user presses ‘Q’, your program should terminate....
Write a C++ program that displays the current time
Write a C++ program that displays the current time
Write a method that displays every other element of an array. Write a program that generates...
Write a method that displays every other element of an array. Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.) Write two overloaded methods that return the average of an array with the following headers:      public static int average(int[] intArray)        public static double average(double[] dArray) Also,...
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Develop a python program to - Create a 2D 10x10 numpy array and fill it with...
Develop a python program to - Create a 2D 10x10 numpy array and fill it with the random numbers between 1 and 9 (including 0 and 9). - Assume that you are located at (0,0) (upper-left corner) and want to move to (9,9) (lower-right corner) step by step. In each step, you can only move right or down in the array. - Develop a function to simulate random movement from (0,0) to (9,9) and return sum of all cell values...
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Write a program in c++ that picks four cards from a deck of 52 cards and...
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. You are not allowed to use arrays. Declare four variables to hold the card values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT