Question

Write a program that, given a starting location in a 2D grid of characters, will count how many contiguously connected @ symbols there are. Cells have to be direct neighbors either horizontally or vertically to count as contiguous. However, the right/left and top/bottom edges ARE considered to be connected – you can “wrap around” from one side of the matrix to the opposite one.

The grid of chars will always be 10x10. Your program should load the grid of chars from a file called “grid.txt”. A sample grid.txt is included.

Your program MUST make use of a recursive function for the core problem.

You may use loops to read in the data from the file.

Do NOT use a global variable to keep track of the count.

Sample Runs – User input in BOLD – all row/col are zero-indexed

See the image below for an example of how the characters are counted.

Run 1:

Enter row and column:

0 0

5 cells

Run 2:

Enter row and column:

1 7

5 cells

Run 3:

Enter row and column:

3 1

11 cells

Run 4:

Enter row and column:

2 0

0 cells

nitial Matrix C-CG@C 3@G-@@-G Checking 0, 0 C-GG-@C @@G-@- Ce-@e@-@@@ Ce-@ee-@@@ C-C-@- C@-@eC-@ C-C-C- GC-@@G-@C Gee-@ C@C@C-C-@ e@-a C@@eee- Checking 1, 7 Checking 3, 1 C-GG-@C G@C-GC- G-G-C Ge-@@- Ce-@e@-@@@ GC-@@C-@@C C-C-@- C@-@eC-@ C-C-@-G G@-GCe-@C Gee-@ C@C@C-C-@ e@-a C@@@e-e-

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sol -pile.tx1 a ㅟ Cory h。Iow _ @--@-@日 @@ _ @@ @一日@@ @ _ ๑๑--@囚ㄧ includeく.ps1Y earn > 따 ?nclude <string> using name space Std, class Ma Poblic: Map) Cannected Cell ㄷ。

Create a file with name "file.txt" and copy below content

@-@--@-@@-

@@@-@@-@--

---@------

@@-@@@-@@@

-@-@-@-@--

@@-@@@-@@-

----------

-@@@-@----

-@@@@@-@-@

-------@-@

create below file to your main cpp file

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

class Map

{

public:

Map()

{

connectedCell=0;

}

void print()

{ cout<<endl;

for(int i=0;i<10;i++)

{

for(int j=0;j<10;j++)

cout<<map[i][j]<<" ";

cout<<endl;

}

}

void input()

{

char ch;

int i=0;

fstream fin("file.txt");

string line;

while (getline(fin,line))

{

for(int j=0;j<10;j++)

map[i][j]=line.at(j);

i++;

}

fin.close();

}

int floodFill(int row, int col, char orgin, char target)

{

here is the picture of file .txt

here is the output result proof

Add a comment
Know the answer?
Add Answer to:
Write a program that, given a starting location in a 2D grid of characters, will count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Write the following C++ program that searches for specified words hidden within a matrix of letters....

    Write the following C++ program that searches for specified words hidden within a matrix of letters. 1) Read a file that the user specifies. The file will first specify the dimensions of the matrix (e.g., 20 30). These two numbers specify the number of rows in the matrix and then the number of columns in the matrix. 2) Following these two numbers, there will be a number of rows of letters. These letters should be read into your matrix row...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Character Count Write a program that reads a file and counts the number of occurrences of...

    Character Count Write a program that reads a file and counts the number of occurrences of each character in the file (case sensitive). The file name is "char.txt". You should use a dictionary to hold the number of occurrences of each character, and print out that dictionary. For example, if the file contains only six characters, "tactic", your dictionary should contain the following key-value pairs (order can be different): {'a': 1, 'c': 2, 'i': 1, 't': 2} Sample Input: Follow...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT