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

Write a C++ program called coin collection.cpp to collect maximum number of coin

ID: 3717606 • Letter: W

Question

Write a C++ program called coin collection.cpp to collect maximum number of coins on an n x m board with inaccessible cells Your program should ask a user for a text filename to read a 2-D array of a sequence of numbers. In the array, 1 indicates that there's a coin on the cell, while 0 means no coin. A cell with the number 2 indicates that the cell is not accessible. Your program should display maximum possible coins and path to collect it. If you have more than one path, you only need to display one. You can assume that the board size is less than or equal to 25 x 25 This is a sample run of the program $ gt+-o coin collection coin collection.cpp $ . /coin collection Enter a file name: ./t1.txt Max coins: 3 Path: (1,1)-> (2,1)-> (2,2) -> (3,2)- (3,3)-> (4,3) -> (4,4) For the sample run, t1.txt has the following context 1 2 01 The first line (= 4 and 4 in the example) indicates the numbers of rows and columns in the file. One blank space is used to delimiter the data. Note that there's no blank space at the end of each line. If your program does not read the file properly, your program will get no credit. In the sample execution, the starting indexes of the path are 1, not 0 This is another sample run $ gtt -o coin_collection coin_collection.cpp $ . /coin collection Enter a file name: ./t2.txt Max coins: 1 Path: (1,1)-> (1,2) -> (1,3)-> (1,4)-> (2,4) For the sample run, t2.txt has the following context

Explanation / Answer

/******************************************************************************

Online C Compiler.

Code, Compile, Run and Debug C program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

#include <iostream>

#include <fstream>

#include <sstream>

using namespace std;

int

main ()

{

string fileName;

cout << "Please Enter the File name with complete path: ";

cin >> fileName;

ifstream in (fileName);

if (!in)

{

cout << "Cannot open input file. ";

return 1;

}

char str[255];

in.getline (str, 255); // delim defaults to ' '

int row = 0;

int col = 0;

//read the row and col from first row

if (in)

{

//split the line

std::istringstream is (str);

int n;

while (is >> n)

{

if (row == 0)

row = n;

else

col = n;

}

}

//default next column is always first column of the first row

int nextCol = 1, prevRow = 0, coinCollected = 0;

//loop through each row

for (int r = 1; r <= row; r++)

{

//read line by line

in.getline (str, 255); // delim defaults to ' '

if (in)

{

//split the line

std::istringstream is (str);

int n;

int c = 1;

//loop through each column

while (is >> n)

{

//consider only if have not traversed till this column

if (c >= nextCol)

{

//jump to next row if the cell contains 2

if (n == 2)

break;

//ignore arrow for the first coin

if (coinCollected > 0)

cout << "->";

//else collect the coin

cout << "(" << r << "," << c << ")";

//increase the coins collected

coinCollected++;

//increase the nextCol only if previous row was the same when we last collected the coin

if (prevRow == r)

nextCol++;

//set the previous row

prevRow = r;

}

c++;

}

}

//cout << str << endl;

}

in.close ();

return 0;

}

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