Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use C to programing! We want you to create a percentKilled function that, given a 2D array, will print out a percentage (rounded to the

Use C to programing!

We want you to create a percentKilled function that, given a 2D array, will print out a percentage (rounded to the nearest whole number) of how many infected cells were destroyed.

Attached is some starter code. We are going to be busy culturing more of these bacteria to save us from extinction, so we're relying on you to implement this function!

The Bacteria

Here are how the two bacteria strains we have developed can attack infected cells (Xs represent cells we could kill, L and W represent our bacteria, _s represent uninfected cells):

  • Leo coli: they can move exactly 2 spaces up, down, left, right, or diagonally at 45 degrees to consume an infected cell, like so:
X _ X _ X _ _ _ _ _ X _ L _ X _ _ _ _ _ X _ X _ X 
  • Willmonella: they can move up to three steps in any cardinal direction (up, down, left, or right), then up to one step at a right angle, like so:
_ _ X X X _ _ _ _ _ X _ _ _ X _ _ X _ _ X X X X W X X X X _ _ X _ _ X _ _ _ X _ _ _ _ _ X X X _ _ 

Each bacterium can only move from its starting position. They cannot move somewhere, then move again. They can attack as many infected cells as they can reach, only from their starting position.

Note: We have added spaces above to make it more readable. There will be no spaces in the arrays.

Tissue Samples

We have a few patients (Deep and Don) and samples of their tissue that we can test our bacteria on. Each tissue sample is square, perfect for representing as a 2D array (how convenient, right?). We will supply you with these tissue samples as 2D arrays whose values have the following meanings:

  • I: Infected Cell
  • L: A Leo coli bacterium
  • W: A Willmonella bacterium
  • _: An uninfected cell (bacteria will ignore these)

No other value will appear in these arrays.

Your Task

Download the starter code: cease_the_disease_starter.cimage text in transcribed

Implement the functions:

int percentKilled(Tissue tissue_sample) bool canLeoEat(Tissue tissue_sample, int x, int y) bool canWillEat(Tissue tissue_sample, int x, int y) 

The latter 2 functions are designed as helpers for the former. Use them wisely. We may also test them individually.

Implementation Details

A Tissue is a compound data type that is just a 2D char array that has been renamed:

typedef char Tissue[TISSUE_SIZE][TISSUE_SIZE]; 
  • Each tissue sample is exactly TISSUE_SIZE * TISSUE_SIZE in dimensions
    • Each row is TISSUE_SIZE long.
    • There are TISSUE_SIZE columns in every tissue sample array.
    • The indices are, in order, [COLUMN][ROW].
  • Function arguments should not be mutated by any of your functions.
  • The characters representing each type of bacterium, infection, etc. are fixed; they will never change.

Starter code:

#include // Note this is needed to use the bool type

#include

#define TISSUE_SIZE (10)

// We are going to take advantage of the 'typedef' keyword - it can be used to make

// a nickname for any data type, not just compound data types as we have been doing

// in lecture. For this exercise, we're going to be dealing with 2D arrays that contain

// different symbols, we will create a convenient nickname for these 2D arrays.

// The typedef below creates an alias 'Tissue' for a char array of size [TISSUE_SIZE][TISSUE_SIZE].

// Thereafter, we can do this:

//

// Tissue one_tissue;

//

// and this is equivalent to:

//

// char one_tissue[TISSUE_SIZE][TISSUE_SIZE];

//

typedef char Tissue[TISSUE_SIZE][TISSUE_SIZE]; // The syntax is kind of funky, but that's just a

// quirk of C, nothing to worry yourself about :)

/**

* Return true iff there exists a Leo coli within tissue_sample that can attack position x, y.

*

* Legal moves are limited to the pattern (where X is a legal move and L is the bacterium):

* X _ X _ X

* _ _ _ _ _

* X _ L _ X

* _ _ _ _ _

* X _ X _ X

*

* INPUT:

* tissue_sample:

* - an array of arrays of characters with dimensions TISSUE_SIZE * TISSUE_SIZE

* - gives positions of each bacterium and each infected cell

* - each cell can only contain one of the following types:

* - `I`: Infected Cell

* - `L`: A Leo coli bacterium

* - `W`: A Willmonella bacterium

* - `_`: An uninfected cell (bacteria will ignore these)

* x: the row coordinate (starting at 0) of the cell (within tissue_sample) to be checked.

* y: the column coordinate (starting at 0) of the cell (within tissue_sample) to be checked.

*

* RETURN:

* true, if there exists some Leo coli that can make a legal move to position x, y

* false, if no Leo coli can make a legal move to position x, y

*/

bool canLeoEat(Tissue tissue_sample, int x, int y)

{

/** TODO: implement this function, satisfying the description above. */

return false; //

}

/**

* Return true iff there exists a Willmonella within tissue_sample that can attack position x, y.

*

* Legal moves are limited to the pattern (where X is a legal move and W is the bacterium):

* _ _ X X X _ _

* _ _ _ X _ _ _

* X _ _ X _ _ X

* X X X W X X X

* X _ _ X _ _ X

* _ _ _ X _ _ _

* _ _ X X X _ _

*

* INPUT:

* tissue_sample:

* - an array of arrays of characters with dimensions TISSUE_SIZE * TISSUE_SIZE

* - gives positions of each bacterium and each infected cell

* - each cell can only contain one of the following types:

* - `I`: Infected Cell

* - `L`: A Leo coli bacterium

* - `W`: A Willmonella bacterium

* - `_`: An uninfected cell (bacteria will ignore these)

* x: the row coordinate (starting at 0) of the cell (within tissue_sample) to be checked.

* y: the column coordinate (starting at 0) of the cell (within tissue_sample) to be checked.

*

* RETURN:

* true, if there exists some Willmonella that can make a legal move to position x, y

* false, if no Willmonella can make a legal move to position x, y

*/

bool canWillEat(Tissue tissue_sample, int x, int y)

{

/** TODO: implement this function, satisfying the description above. */

return false; //

}

/**

* Computes the percentage of infected cells in the given tissue sample which can be killed

* in a single move by one of the Willmonella or Leo coli bacteria in the sample.

*

* INPUT:

* tissue_sample:

* - an array of arrays of characters with dimensions TISSUE_SIZE * TISSUE_SIZE

* - gives positions of each bacterium and each infected cell

* - each cell can only contain one of the following types:

* - `I`: Infected Cell

* - `L`: A Leo coli bacterium

* - `W`: A Willmonella bacterium

* - `_`: An uninfected cell (bacteria will ignore these)

*

* RETURN:

* The (rounded to nearest integer) percentage of bacteria on the board that are within

* the reach of one of the "good bacteria" (Will or Leo).

*/

int percentKilled(Tissue tissue_sample)

{

/**

* TODO: Implement this function, satisfying the description above.

*

* CRUNCHY: Can you do this without using a loop in a loop?

* -> Think about how this 2D array is stored in memory...

*/

return 0; //

// of infected cells killed by the bacteria.

}

/**

* Prints the tissue sample in a human readable format.

*

* INPUT:

* tissue_sample:

* - an array of arrays of characters with dimensions TISSUE_SIZE * TISSUE_SIZE

* - gives positions of each bacterium and each infected cell

* - each cell can only contain one of the following types:

* - `I`: Infected Cell

* - `L`: A Leo coli bacterium

* - `W`: A Willmonella bacterium

* - `_`: An uninfected cell (bacteria will ignore these)

*

* NOTE: The displayed indices do not match the implementation.

* That is, they start at 1 (when we know array indices start at 0).

* This is more obvious for the column indices, which are A - J.

*/

void printTissueSample(Tissue tissue_sample)

{

printf(" A B C D E F G H I J ");

for (int x = 0; x

{

printf("%2d ", x + 1);

for (int y = 0; y

printf(" ");

}

}

// We NEED this compiler directive to test your code with our own main(). Don't break it.

#ifndef __TESTING // this compiler directive

int main()

{

// Please note that these are JUST EXAMPLES! You will be tested on a wide range of data.

printf("====Deep's Sample==== ");

Tissue deep_sample = {{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', 'L', '_', 'I', '_', '_', '_'},

{'_', '_', '_', '_', 'I', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', 'I', '_', '_', '_', '_', '_'},

{'_', '_', '_', 'W', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'}};

printTissueSample(deep_sample);

printf("Calculated: Killed %03d%% of bacteria ", percentKilled(deep_sample));

printf(" Expected: Killed 067%% of bacteria ");

// Feel free to add your own examples/tests here

printf("====Donnie's Sample==== ");

// To illustrate the utility of the typedef above, let's see how we'd create a tissue sample

// just as an array of chars. It will look like a different type to us, but the compiler

// knows its the same because the typedef at the top says it's the same thing.

// I.E. A char[TISSUE_SIZE][TISSUE_SIZE] is a Tissue, according to C

char donnie_sample[TISSUE_SIZE][TISSUE_SIZE] = {

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', 'I', '_'},

{'_', '_', '_', '_', '_', 'I', '_', '_', 'I', '_'},

{'_', '_', '_', '_', '_', '_', '_', '_', '_', 'I'},

{'_', '_', '_', 'I', '_', '_', '_', '_', 'I', 'W'},

{'_', '_', '_', 'I', '_', '_', '_', '_', '_', '_'},

{'_', '_', '_', 'L', '_', '_', '_', '_', 'L', '_'},

{'_', '_', '_', '_', '_', '_', '_', 'I', 'I', 'I'},

{'_', '_', '_', '_', '_', '_', '_', '_', 'I', '_'}};

printTissueSample(donnie_sample);

printf("Calculated: Killed %03d%% of bacteria ", percentKilled(donnie_sample));

printf(" Expected: Killed 064%% of bacteria ");

}

#endif // and this compiler directive

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions