Conway's Game of Life C++ Code check - c++

I'll start by saying that I'm a begginer. I've started learning 2 weeks ago. Today I found some video on YT about this Conway's Game of Life. With things I've learned so far I tought I'll give it a go. So I've made this(code below). It doesn't work like it should be and I'm out of ideas why. Could someone check this code below and tell me what's wrong? I don't want a complete solution, just a tip what I should check.
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
int neighbours=0;
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false;
}
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
while(!(kbhit())) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
for(int pozX=x-1, pozI=i-1; pozI<=i+1; pozX++) {
//if(pozX!=x && pozI!=i) { //this "if" doesn't work, don't know why
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50) {
if(positions[pozI][pozX]==true) neighbours++;
}
//}
if(pozX==x+1) {
pozX=x-1;
pozI++;
}
}
if(neighbours==1) neighbours=0;//had to use this instead of previously mentioned if
if(positions[i][x]==true) {
if((neighbours>3) || (neighbours<2)) new_positions[i][x]=false;
}
if(positions[i][x]==false && neighbours==3) new_positions[i][x]=true;
neighbours=0;
}
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
if(new_positions[i][x]==true) cout << "X";
else cout << " ";
}
cout << endl;
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=false; //clears all cells
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) positions[i][x]=new_positions[i][x]; //sets cell status from this cycle, I know I could do this in one loop, but it is more clear to me this way
}
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) new_positions[i][x]=false; //clears this cycle "buffor"
}
Sleep(3000);
system("cls");
}
}

How I debugged the code was by using https://www.onlinegdb.com/
Learning to use a debugger is really valuable...
What I have done includes:
I removed the kbhit() test and the Sleep() call and cls because I am not using a windows compiler. You will have to put those back if you use my code.
I changed the bit that displays the board to give it line numbers and to display something in the blank cells and I changed it to avoid calling endl to flush the buffer for every single line.
I changed the way neighbours were calculated. I think you were subtracting 1 to take care of the extra add that was done - I think it is better to just use an if statement to avoid adding it in the first place. Here is my version:
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
I changed the way the new board was calculated. You weren't ever setting the new position to true if it should stay alive. Here is my version:
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
What I have not done includes:
There are a lot of stylistic issues and whether they are changed is debateable depending on what you consider best practice. Things like the variable names and having everything in main() and having a global using namespace std; and using a C style array instead of a std::array are all things that we could argue about but that belongs on codereview.stackexchange.com so I haven't changed any of that.
Here is the full program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
bool positions[50][50], new_positions[50][50];
// clear the board
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=false;
//This is some patterns i've put manually into array just for testing,
//they should "be alive" at least for 3-4 cycles
positions[5][5]=true;
positions[5][6]=true;
positions[5][7]=true;
positions[6][5]=true;
positions[5][8]=true;
positions[5][9]=true;
positions[5][10]=true;
positions[5][11]=true;
positions[5][5]=true;
positions[6][5]=true;
positions[7][5]=true;
positions[5][6]=true;
positions[8][5]=true;
positions[5][7]=true;
positions[6][8]=true;
positions[7][9]=true;
positions[8][10]=true;
positions[9][5]=true;
positions[11][11]=true;
positions[11][12]=true;
positions[12][11]=true;
positions[12][12]=true;
// print the initial board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
for(int steps=0; steps<15; steps++) {
for(int i=0; i<50; i++) {
for(int x=0; x<50; x++) {
// count the neighbours
int neighbours=0;
for(int pozI=i-1; pozI<=i+1; pozI++)
for(int pozX=x-1; pozX<=x+1; pozX++)
if(pozX!=x || pozI!=i) // don't check position[i][x]
if(pozX>=0 && pozX<50 && pozI>=0 && pozI<50)
if(positions[pozI][pozX]==true)
neighbours++;
// set the new board
new_positions[i][x]=false;
if(positions[i][x]==true)
if(neighbours==2 || neighbours==3)
new_positions[i][x]=true; // staying alive
if(positions[i][x]==false)
if(neighbours==3)
new_positions[i][x]=true; // being born
}
}
// print the old board and the new one
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << " ";
for(int x=0; x<50; x++)
cout << (new_positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
// save the generation
for(int i=0; i<50; i++)
for(int x=0; x<50; x++)
positions[i][x]=new_positions[i][x];
}
// print the final board
for(int i=0; i<50; i++) {
cout << setw(4) << i << setw(1) << " ";
for(int x=0; x<50; x++)
cout << (positions[i][x]==true?"X":".");
cout << "\n";
}
cout << endl;
return 0;
}

Related

Program not giving any output, when I use Continue

I am a beginner to C++ Programming. I tried to make a simple program to learn the continue statement. But, when I use continue statement for skipping an iteration of the loop, the program runs but doesn't show any output. Any Help?
#include<bits/stdc++.h>
using namespace std;
int main() {
for (int j=1; j < 13; j++)
{if(j=8) continue;
for (int i=1; i<j+1; i++)
{cout << setw(4) << i*j;}
cout << endl;
}
}
We use == instead of = for comparision.
The = is an assignment operator we generally use it to initialize values.
int main() {
for (int j=1; j < 13; j++){
if(j==8) continue;
for (int i=1; i<j+1; i++){
cout << setw(4) << i*j;
}
cout << endl;
}
}

How do I set up a 5 X 5 two-dimensional array that allows for the input of all numbers?

I'm new to programming and am struggling with the idea of setting up a two dimensional array that is 5x5 and allows for the input of the numbers 0-10. I've scratched down a few things but need some advice or guidance. The book and youtube videos can't help me any further.
#include <iostream>
using namespace std;
int main()
{
int numbers[5][5];
for (int i = 0; i < 5; i++)
{
cout << "Please enter the five scores for Contestant #1: " << i << endl;
cin >> numbers[i]
}
int *array;
cout << "Please enter the contestants 5 scores: ";
int n;
cin >> n;
array = new int[n];
for (int i = 0; i < n; i++)
try this:
include
using namespace std;
int main()
{
int numbers[5][5];
//Input Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Please enter Contestant # " << i <<"score for Test # "<<j<<" :"<<endl;
cin>>numbers[i][j];
}
}
//Print Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Contestant # " << i <<"score for Test # "<<j<<" :"<< numbers[i][j]<<endl;
}
}
return 0;
}

cpp pass by reference

i wrote a simple sorting code on pass by reference. here i am passing an array to function and then performing the sorting operation. after passing the array i am printing the entire array as entered by user then performing the sorting operation(in descending order) but after sorting when i print the sorted array i get an output that the array index '0' contains the value '41'. if i enter the numbers less than '41' then the sorted array is displayed as '41',and then the other numbers in sorted manner. Please explain why i am getting such an output.
#include<iostream>
using namespace std;
int sort_array(int *p);
int main() {
int arr[10];
for (int i=0; i<10; i++) {
cout << "enter " << (i+1) << " value:";
cin >> arr[i];
cout << "\n";
}
sort_array(arr);
return 0;
}
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:"<<p[0];
cout<<"\n";
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (p[j] < p[j+1]) {
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << p[0];
}
It appears that you are trying to do a bubble sort on your array in sort_array(), but the logic is wrong. Try using this code instead:
int sort_array(int *p) {
int c=0;
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout << "arr:" << p[0];
cout << "\n";
for (int i=0; i < 10; i++) {
for (int j=1; j < (10-i); j++) {
if (p[j-1] > p[j]) {
c = p[j-1];
p[j-1] = p[j];
p[j] = c;
}
}
}
cout << "\n";
for (int i=0; i<10; i++) {
cout << p[i];
cout << "\n";
}
cout<<p[0];
}
The problem is in your sorting. j is from 0 to 9, and than you access p[j+1] when j = 9, p[10] is outside your array boundaries.
So fix your following part to proper sorting.
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
Clarification: the code above is the problematic part of the original code posted. This is NOT the fixed sorting. That is the part to be fixed.
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
if(p[j]<p[j+1])
{
c=p[j];
p[j]=p[j+1];
p[j+1]=c;
}
}
}
the sorting works fine once i changed the limit of inner loop. the problem was accessing the array index [10] but i had it declared till index [9].

How to add user input into a 2D array

I'm trying to get get user input, which is stored in an array (eightBit[]), and then add that to a 2D array (board). The user is supposed to enter 8 numbers, for an example:
Byte 1: 1
Byte 2: 2
etc...
and the output is supposed to look like:
1 2 3 4
5 6 7 8
however this is the output I get:
8 8 8 8
8 8 8 8
Any idea why its repeating only the last numbered entered? Part of my code is below, any help would be appreciated.
cout << "Enter a pattern of eight bits:" << endl;
for(i = 0; i < 8; i++){
cout << "Byte " << i+1 << ": ";
cin >> eightBit[i];
}
int board[2][4];
for(i = 0; i<8; i++){
for(int j=0; j<2; j++){
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i];
}
}
for(int j=0; j<2; j++)
{
for(int k=0; k<4; k++)
{
cout << board[j][k] << " ";
}
cout << endl;
}
That's because of your outer loop with i which is basically overwriting every element in your 2D array.
A solution would be to drop that outer loop entirely, like so:
int i = 0;
for(int j=0; j<2; j++) {
for(int k=0; k<4; k++) {
board[j][k] = eightBit[i++];
}
}
also you have bracket mismatch in your code snippet.
That's natural. In the second for when the i gets at last 8, then the board gets filled with the current i (i=8).
Try this, and next time be more careful with your code :).
#include <iostream>
using namespace std;
int eightBit[2][4];
int main()
{
cout << "Enter a pattern of eight bits:" << endl;
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << "Byte " << (j+1)+4*i << ": "; //4 = # of columns,i=row,j=column.
cin >> eightBit[i][j];
}
}
int board[2][4];
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
board[i][j] = eightBit[i][j];
}
}
for(int i = 0; i <2; i++){
for (int j=0 ; j<4 ; ++j) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

LU decomposition in c++

I am currently having some probs with my code to decompose an array into an upper(u) and lower(l) array.
I am using the doolittle method
My code:
#include <iostream>
using namespace std;
int main(){
double a[10][10];
double l[10][10];
double u[10][10];
double som=0;
int RANG;
cin >> RANG;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cin >> a[i][j];
}
}
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
u[i][j]=0;
l[i][j]=0;
}
}
for(int i=0;i<RANG;i++) {
l[i][i]=1;
for(int j=i;j<RANG;j++) {
for(int s=0;s<i-1;s++) {
som+= l[i][s]*u[s][j];
}
u[i][j]=a[i][j]-som;
}
for(int k=i+1;k<RANG;k++) {
double som=0;
for(int s=0;s<i-1;s++) {
som+=l[k][s]*u[s][i];
}
l[k][i]=(a[k][i]-som)/u[i][i];
}
}
cout << "l:" << endl;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cout << l[i][j] << "\t";
}
cout << endl << endl;
}
cout << "u: " << endl;
for(int i=0; i<RANG; i++){
for(int j=0; j<RANG;j++){
cout << u[i][j] << "\t";
}
cout << endl << endl;
}
return 0;
}
plz help if you can...
PS: not sure if it belongs here, might be better on the mathematics site
Have a look at http://download.intel.com/design/PentiumIII/sml/24504601.pdf it got complete solution along with source code!
You might want to check out QuantLib. http://quantlib.org/index.shtml
Here's an example of code that uses the QuantLib library to decompose a 3x3 array. It uses a Cholesky decomposition, but maybe it'll help. http://quantcorner.wordpress.com/2011/02/20/matrix-decomposition-with-quantlib/
Check your 's' iteration it should be
for(int s=0;s