Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that creates a 2-D array of characters with size 30 × 40. In

ID: 3721460 • Letter: W

Question

Write a C++ program that creates a 2-D array of characters with size 30 × 40. Initial the whole 2- D array with character ‘a’s. Then program should ask for user input ten times. Each time ask for 3 user input: a row number, a column number, and a character. The program should put the character read from the user into the array at the given row and column position. For example, if user enters 5 9 d, which means put the character ‘d’ at location row 5 column 9 in the array. If the location is out of the bounds for the 30 × 40 array, display error message. The program should ask for user input 10 times (invalid input does not count) which means 10 user picked characters will be stored in the 2-D array. After the 10 input, print the whole 2-D array by calling the print_array() function. (see the requirement of the print_array() function) Then in the main function, call the find() function and print the location of the target character. Before calling the function, ask user to choose the target character. If the target character is not found, print “not found”, otherwise display the location of it. (See the requirement of the find() function.) • Write a function, print_array(), to print the array. The 2-D array should be passed to the print_array() function. • Write a function called find(). The function takes four parameters: a 2-Dimensional array of characters, the target character and 2 integers. The function must check each element of the array to find the target character which was set by the user. If the element contains the specific character, the function should set the two integer parameters to the x and y position of the character. You may assume the size of the array is defined as global variables named Width and Height.

Explanation / Answer

#include <iostream>

using namespace std;

int const Height=30,Width=40;

//function to print 2D array

void print_array(char array[Height][Width])

{ int r,c;

for(r=0;r<Height;r++)

{

for(c=0;c<Width;c++) {

cout<<array[r][c]<<" ";

}

cout<<endl;

}

}

//Function to find the target character in 2D array

void find(char array[Width][Width],char target,int *a, int *b)

{ int flag=0,r,c;

for(r=0;r<Height;r++)

for(c=0;c<Width;c++)

{

if(array[r][c]==target)

{

flag=1;

*a=r; //set the row position where target character if found

*b=c; //set the column position where target character if found

r=Height; //values is set to end the loop if target is found

c=Width; //values is set to end the loop if target is found

}

}

if(flag==0)

{ *a=-1; *b=-1;} //set the value -1 if target character is not found

}

int main()

{ int rowNo,colNo,r,c,i=0,a,b;

char character,target;

char array[Height][Width];

  

// Fill 2D array with 'a'

for(r=0;r<Height;r++)

for(c=0;c<Width;c++)

array[r][c] = 'a';

// Taking the input from user 10 times

while(i<10)

{

cout<<"Enter the row number:";

cin>>rowNo;

cout<<"Enter the row number:";

cin>>colNo;

cout<<"Enter the row number:";

cin>>character;

if(rowNo>Height-1 || colNo > Width-1)

{ cout<<"Array location is out bound"<<endl; }

else

{ array[rowNo][colNo] = character; //set the new character in array

i++;

}

}

print_array(array); // calling function by passing 2D array as parameter

cout<<"Enter the character to be searched in an array:";

cin>>target; //Input the character to be searched

//Calling find() function array,target & two integer as parameter

// a,b is passed as a reference to get the value

find(array,target,&a,&b);

if(a != -1 && b != -1)

cout<<target<<" is found at location row="<<a<<" and Column="<<b<<endl;

else

cout<<target<<" is not found in an array";

return 1;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote