Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is a C++ question involving strings and rule 90. A few of the functions are completed but the last few functions are not. The

Here is a C++ question involving strings and rule 90. A few of the functions are completed but the last few functions are not. The purpose is to implement the functions and have them connect to give specific output.

One character means on, the other means off. In this homework, we're going to use `#` to represent on, and ` ` (space character) for off. This makes the output a bit more interesting because there's a lot of contrast. You could use dashes and pipes like I did above.

Anyway, based on an initial generation and a set of rules, we can build later generations that can form interesting patterns. For our purposes, we're displaying each generation as a new line directly below the earlier one.

**In Rule 90, a cell in the next generation is on if and only if exactly one of the cell's neighbors from the previous generation is on.**

So to calculate (say) the next value of cell 8, we have to look at the current value of cells 7, 8, and 9. Actually, the current value of cell 8 isn't needed, but those three (cells 7, 8, 9) form the "neighborhood" of interest.

The tricky thing is that we will treat the generation strings as circular. That means the element to the left of cell zero is the last cell; the element to the right of the last cell is at index zero.

#include

// Note that the header file doesn't have 'using namespace std', so

// all references to string have to be qualified using std::string.

// prev returns the index before i. If i is 0, it should return

// (string_size - 1).

int prev(int i, int string_size);

// next returns the index after i. If i is (string_size - 1), it

// should return 0.

int next(int i, int string_size);

// get_neighborhoold creates a three-element string that should be

// equivalent to the characters in[previous_index], in[i], and

// in[next_index] (in that order). This function should use the prev

// and next functions, because we want to treat the string as being

// circular, where the last element and the first element are

// adjacent.

//

// Example: if the input string is "abcdef", and we want the

// neigborhood around index 5, the return value should be "efa".

//

// Example using sharp signs and spaces: if the input string is

// " # # # ## # # ### #", here's the expected output in

// table form:

//

// index output remark

// 0 "# #" The string is circular. The ends meet.

// 1 " # "

// 2 "# "

// 25 " # " Index 25 is the last # sign in the input.

std::string get_neighborhood(std::string in, int i);

// rule90 is a function that looks at the small neighborhood of the

// input string centered about index i, and returns a character for

// the next generator. See https://en.wikipedia.org/wiki/Rule_90 for

// more than you probably want to know on the subject.

//

// Short version is: if the neighborhood is X, return Y, according to

// this table:

//

// "###" --> ' '

// "## " --> '#'

// "# #" --> ' '

// "# " --> '#'

// " ##" --> '#'

// " # " --> ' '

// " #" --> '#'

// " " --> ' '

//

// There are several ways to implement this, and we're not placing any

// restrictions on how you go about it.

//

// Just to be super clear, the input to rule90 will be something like:

// " # # # ## # # ### #"

char rule90(std::string in, int i);

// make_next_generation uses the current generation (the `in`

// parameter) and uses the `rule90` function for each cell of the

// current generation to produce a next generation string.

std::string make_next_generation(std::string in);

// generate runs make_next_generation several times using the

// initial_state as the first state; for subsequent calls it uses the

// return value from the previous invocation of

// `make_next_generation`.

//

// `num_generations` is the number of times to invoke

// `make_next_generation`.

//

// This should print the initial state and every following generation

// to the console using cout.

//

// Look at the example_output.txt file to see what it might look

// like. The example was generated with a string that looks like the

// following (a bunch of spaces, a # sign, and then the same number of

// spaces).

//

// " # ".

void generate(std::string initial_state, int num_generations);

#include

#include

#include "automata.h"

using namespace std; // yes using namespace std

// NOTE: for directions on what to do with all of these TODOs below,

// please refer to the text above taken from the header file.

//***Here is the completed code so far. The function get_neighborhood does NOT work. ***

int prev(int i, int string_size)

{

if (i>0)

{

i=(i-1);

return i;

}

else if (i==0)

{

i=string_size-1;

return i;

}

}

int next(int i, int string_size)

//// next returns the index after i. If i is (string_size - 1), it

// should return 0

{

if (i==(string_size-1))

{

return 0;

}

else

{

i=i+1;

return (i);

}

}

string get_neighborhood(string in, int i)

{

//THIS DOES NOT WORK!!

prev(i, string_size)

next(i, string_size)

char x=in[prev(i, int string_size)];

char y=in[i];

char z=in[next(i, int string_size)];

std::string msg=std::to_string(x)+std::to_string(y)+std::to_string(z);

return msg;

}

char rule90(string in, int i) {

// TODO

}

string make_next_generation(string in) {

// TODO

}

void generate(string initial_state, int num_generations) {

// TODO

}

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

What is the purpose of the trial balance?

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago