2 Replies - 677 Views - Last Post: 09 October 2010 - 04:55 AM Rate Topic: -----

#1 jvelasco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-October 10

C++ programming with arrays?

Posted 09 October 2010 - 02:13 AM

So I've been working on this problem and I got it to compile but it's not showing what I want. Instead of showing a pyramid of "*" it's just showing a one "*" on each line. Anybody know what I'm doing wrong?

// Program that mimicks the finite-state cellular autonoma for Rule 30.




#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()
{
int row[ WIDTH ] = {};
row[39]=1;
}

// 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 ( i == 0 )
cout << " ";

else if ( i == 1 )
cout << "*"<< endl;
}
}

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

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

for ( int i = 0; i < WIDTH; i++)
{
int left = 0;
int cellt = 0;
int right = 0;

if ( i > 0 )
{
left = tempRow[i-1];
}
cellt = tempRow[i+1];
if (i < WIDTH - 1 )
{
right = tempRow[i+1];
}

row[i] = findNextCellState (left, cellt, right );

}


}

// The findNextCellState function implements the Rule 30 celular 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;
}
}
} 


I posted this earlier, but it was in the wrong forum. Sorry

This post has been edited by macosxnerd101: 09 October 2010 - 03:36 AM
Reason for edit:: Added end code tag.


Is This A Good Question/Topic? 0
  • +

Replies To: C++ programming with arrays?

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++ programming with arrays?

Posted 09 October 2010 - 02:21 AM

Your code tags are messed up, you're missing the closing tag.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: C++ programming with arrays?

Posted 09 October 2010 - 04:55 AM

Your printRow() function only prints a single '*' at the second column.

// 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 ( i == 0 )
cout << " ";

else if ( i == 1 )
cout << "*"<< endl;
}
}



Read the function spec again, and see how your implementation does not fit the spec.

NOTE: Whatever you are using to edit your code sucks...you NEED indentation for easy-to-read code, and you don't have any.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1