# Thread: Having trouble with the bounds of an array.

1. ## Having trouble with the bounds of an array.

Hello everyone I am new to this forum, so please bear with me if I am still learning the rules of the forum and how to program c++.

Right now I am working on a homework assignment that calls for the following:
Write a program that will simulate 40 new states of a finite-state cellular automata In computer science we will find that complicated behavior can arise from very simple rules. One example of this is Cellular Automatons. Use a global integer array of width 80 to represent the current state When the main starts, use a function to populate the array with all 0s, except for the middle (40th) element which should be set to 1 Implement a method that will print the row to the screen. Whenever the state is a 0, output a space (" ") and whenever the state is a 1, output a star("*") Implement a method that will look at every element in the current array, determine the next state, and save it in to a new array. When the method is finished, copy all elements of the new array in to the global array. Run a loop in the main method which will update the state and print out the new state 40 times. When you are determining what is to the left and to the right of the current cell, if it is out of bounds for the array (index "-1" or index "array width + 1"), consider it to be a 0 When you are computing the next row, you should have a temporary array to put the new states in. Do not update the current row as you find new state.

Here is my code

Code:
```#include <iostream>
#include <cstdlib>
using namespace std;

void setupRow();
void printRow();
void updateRow();
int findNextCellState(int left_neighbor, int cell, int right_neighbor);

const int WIDTH=80;
int row[WIDTH];

int main()
{
setupRow();
printRow();

for (int i = 0; i < 40; i++)
{
updateRow();
printRow();
}
}

// The setupRow function creates an initial state for the cellular autonoma
void setupRow()
{
for ( int i = 0; i < WIDTH; i++ ){
if ( i == WIDTH/2 ){
row[i] = 1;
}else{
row[i] = 0;
}
}

}

// The printRow function outputs the state of the row to the user.  For all
// cells that are 0, it outputs a space (" "), for all cells that are 1, it
// outputs a star ("*")
void printRow()//change to if else
{
for ( int i = 0; i < WIDTH; i++ ){
if ( row[i] == 0 ){
cout << " " ;
}else if (row[i] == 1 ){
cout << "*";
}else {
cout << "error you screwed up" << endl;
}
}
cout << endl;

}

// The updateRow function updates the contents of the row after calculating
// the next states using the findNextCellState function
void updateRow()
{
int temp_row[WIDTH];

for (int i = 0; i < WIDTH; i++){
temp_row[i] = row[i];
}

for ( int i = 0; i < WIDTH; i++ ){
int left_neighbor = i-1;
int cell = i;
int right_neighbor = i+1;

if (left_neighbor < 0 ){
left_neighbor = 0;
}

if (right_neighbor > WIDTH ){
right_neighbor = 0;
}

row[i] = findNextCellState(temp_row[left_neighbor], temp_row[right_neighbor], temp_row[cell]);
}

}

// The findNextCellState function implements the Rule 30 cellular autonoma rules.
// The states for this are:
//
// 111 110 101 100 011 010 001 000
//  0   0   0   1   1   1   1   0

int findNextCellState(int left_neighbor, int cell, int right_neighbor)
{
if (0 == cell)
{
if (1 == (left_neighbor + right_neighbor))
{
return 1;
}
else
{
return 0;
}
}
else
{
if (1 == left_neighbor)
{
return 0;
}
else
{
return 1;
}
}
}```

Right now I am getting an odd output. At the end of the array the output is duplicating the main "section" thus creating a two peaked triangle. Originally I set the i to WIDTH - 1 in order to not use the "out of bounds" data below 0 and above 79, but my teacher let me know that I was not doing the project correctly. Since I have change the bounding I have this problem but don't understand why I am having the issue.

I am NOT looking for you to do my homework but guidance and help would be much appreciated!

if you need it this is how I coded it before my teacher asked me to change it.

Code:
```// CSCI 1410 Programming assignment 5
// Written by FIRSTNAME LASTNAME <EMAIL>

#include <iostream>
#include <cstdlib>
using namespace std;

void setupRow();
void printRow();
void updateRow();
int findNextCellState(int left_neighbor, int cell, int right_neighbor);

const int WIDTH=80;
int pRow[WIDTH];

int main()
{
setupRow();
printRow();

for (int i = 0; i < 40; i++)
{
updateRow();
printRow();
}
}

void header() //created this header so it is easier for me to read the output and visualize
{
for ( int i = 0; i < WIDTH; i++ ){
cout << i % 10 ;//prints the remainder 11/10 = 1R1 prints the R1
}
cout << endl;
}

// The setupRow function creates an initial state for the cellular autonoma
void setupRow()
{
for ( int i = 0; i < WIDTH; i++ ){
if ( i == WIDTH/2 ){
pRow[i] = 1;
}else{
pRow[i] = 0;
}
}

}

// The printRow function outputs the state of the row to the user.  For all
// cells that are 0, it outputs a space (" "), for all cells that are 1, it
// outputs a star ("*")
void printRow()
{
for ( int i = 0; i < WIDTH; i++ ){
if ( pRow[i] == 0 ){
cout << " " ;
}else if (pRow[i] == 1 ){
cout << "*";
}else {
cout << "error you screwed up" << endl;
}
}
cout << endl;

}

// The updateRow function updates the contents of the row after calculating
// the next states using the findNextCellState function
void updateRow()
{
int tRow[WIDTH];

//In line style
/*for ( int i = 1; i < WIDTH - 1; i++ ){
int left_neighbor = i-1;
int cell = i;
int right_neighbor = i+1;

pRow[i] = findNextCellState(pRow[left_neighbor], pRow[cell], pRow[right_neighbor]);
}*/

//Temp Array style
for (int i = 1; i < WIDTH - 1; i++ ){
tRow[i] = pRow[i];
}
for ( int i = 1; i < WIDTH - 1; i++ ){
int left_neighbor = i-1;
int cell = i;
int right_neighbor = i+1;

pRow[i] = findNextCellState(tRow[left_neighbor], tRow[cell], tRow[right_neighbor]);
}

}

// The findNextCellState function implements the Rule 30 cellular autonoma rules.
// The states for this are:
//
// 111 110 101 100 011 010 001 000
//  0   0   0   1   1   1   1   0

int findNextCellState(int left_neighbor, int cell, int right_neighbor)
{
if (0 == cell)
{
if (1 == (left_neighbor + right_neighbor))
{
return 1;
}
else
{
return 0;
}
}
else
{
if (1 == left_neighbor)
{
return 0;
}
else
{
return 1;
}
}
}```

pRow does not stand for pointer, but primary and tRow is for temporary row.
Please let me know if you have questions and I can comment. Thank you for your help!