Plotting Coordinates from a .txt File - c++

I am finishing the second half of a two-part assignment on Conway's Game of Life. I created a function to generate a random array of 1s and 0s; the 1s represent a living cell and the zero an empty space. I created a separate function to inspect the neighborhood and make a count to determine how the game progresses. The rules: if a cell has 2 or 3 neighbors it survives, more than 3 or less than 2 it dies, and if an empty space has 3 neighbors it is "born". I even got help from you guys to wrap the screen using the modulus, but I am having trouble importing a .txt file to finish part two. Here is the code for part one:
#include <iostream> //includes input-output stream
#include <time.h> //includes time function
#include <iomanip> //includes setprecision function
#include <unistd.h> //includes sleep function
#include <fstream> //includes ifstream function
using namespace std; //using standard library
int master[24][79]; //initializes primary data array
int h = 24; // initializes height variable
int w = 79; // initialises width variable
int noOfCycles; //initialize cycles variable
void gen0 (int master[24][79]); // creates initial generation
void life(int master[24][79]); //initializes life function
void copy(int arrayX[24][79], int arrayY[24][79]); //initializes cycle update function
void print(int master[24][79]); //initializes print function
void fillPercent (int master[24][79]); //initializes percentage calculating function
int main() //initialize main function
{
cout << "How many cycles would you like to run?"; //prompt user to input cycles
cin >> noOfCycles; //user inputs cycles
srand (time(0)); //creates initial randomness
gen0(master); //creates initial generation
for (int k = 0; k <= noOfCycles; k++) //prints gen0 and cycles 50 times
{
print(master); //prints current array
fillPercent(master); //calculates/prints fill percentage
cout << " Cycle #" << k << " Author: Mikhail Morgan" << endl << endl;
//prints cycle number and signature
life(master); //calls life function
sleep(1); //delays output by 1 second
} //end width loop
} //end main function
void gen0 (int master[24][79])
{
for(int j = 0; j < h; j++) //height loop
{
for (int i = 0; i < w; i++) //width loop
master[j][i] = rand() % 2; //creates random generation 0
} //end height loop
}
void print(int master[24][79]) //Prints array
{
for(int j = 0; j < h; j++) //height loop
{
for(int i = 0; i < w; i++) //width loop
{
if(master[j][i] == 1)
cout << '0'; //print living cells as zeros
else
cout << ' '; //print dead cells as spaces
} // end width loop
cout << endl;
} // end height loop
} //end print function
void fillPercent (int master[24][79]) // calculates fill percentage
{
double fillNumber = 0; //resets every cycle
for (int i = 0; i < h; i++ ) //width loop
{
for (int j = 0; j < w; j++ ) //height loop
{
fillNumber += master[i][j]; //increments fill number
} //end height loop
} //end width loop
cout << endl << fixed << setprecision(2) << (fillNumber/(w*h))*100; //print percentage
} //end fillPercent function
void life (int master[24][79]) //generates/kills cells based on neighborhood
{
int temp[24][79]; //temporary array for manipulating data
copy (master, temp); //copy array onto temp
for(int j = 0; j < h; j++) //height loop
{
for (int i = 0; i < w; i++) //width loop
{
int count = 0; //intialize neighbor count variable
count = master[(j-1+h) % h][i % w] + //searches down
master[(j-1+h) % h][(i-1+w) % w] + //down left
master[j % h][(i-1+w) % w] + //left
master[(j+1+h) % h][(i-1+w) % w] + //up left
master[(j+1+h) % h][i % w] + //up
master[(j+1+h) % h][(i+1+w) % w] + //up right
master[j % h][(i+1+w) % w] + //right
master[(j-1+h) % h][(i+1+w) % w]; //down right
//cell dies if count falls below 2 or rises above 3
if(count < 2 || count > 3)
temp[j][i] = 0;
//cell stays alive if it has two neighbors
if(count == 2)
temp[j][i] = master[j][i];
//cell either stays alive or gets born if three neighbors
if(count == 3)
temp[j][i] = 1;
} //end width loop
}//end height loop
copy(temp, master); //copy temp back to main array
} //end life function
void copy(int arrayX[24][79], int arrayY[24][79]) //Copy machine
{
for(int j = 0; j < h; j++) //height loop
{
for(int i = 0; i < w; i++) //width loop
arrayY[j][i] = arrayX[j][i]; //temporary arrays used for copying
} //end height loop
} //end copy function
I know, using namespace std is lame af but it's mandated by my dinosaur-of-a-professor.
My problem is that for part two he wants us to stream the coordinates for the initial generation from a file that he supplied called GliderGun.txt. I am using Xcode and Im 99% sure that I have the file in the right location: I can see it in the right hand menu inside the same folder as main.cpp, and I can see the original copy in the Finder menu next to main.cpp. The first line is not a coordinate pair, it is the total number of coordinates in the file... Im not sure what the purpose of that is, and I suspect its whats messing me up. Here is the text from the file itself:
36
1 25
2 23
2 25
3 13
3 14
3 21
3 22
3 35
3 36
4 12
4 16
4 21
4 22
4 35
4 36
5 1
5 2
5 11
5 17
5 21
5 22
6 1
6 2
6 11
6 15
6 17
6 18
6 23
6 25
7 11
7 17
7 25
8 12
8 16
9 13
9 14
Here is the code for the function that replaces gen0. All I did was replace the call for gen0 with getPattern and altered the definition to look like this:
void getPattern (int master[24][79]) //generates Glider Gun
{
ifstream infile("GliderGun.txt", ios::in);
infile.open("GliderGun.txt", ios::in);//opens .txt file
int numOfCoordinates; // number of coordinate pairs
int i, j; // x, y coordinates
infile >> numOfCoordinates;
for (int a = 0; a < numOfCoordinates; a++)
{
infile >> i >> j;
master[j][i] = 1;
}
infile.close(); // closes .txt file
}
The console produces a blank 24x79 array. I sense that I have a looping problem but I dont know enough about how ifstream works to fix it. The coordinates are listed as (x y) or as defined by my other loops, (j i). I don't need the console to print the file I just need it to write 1s in the coordinates that are listed. Thanks for any advice!

Just from trying to reason about your code, there are two problems that arise from a drop-in replacement of getPattern where gen0 used to be:
You don't initialize any of the slots of your array to zero.
You're reading in the number of lines as an x-coordinate for one of the cells, all of the x-coordinates as y-coordinates, most of the y-coordinates as x-coordinates, and not reading the last y-coordinate.
There are two simple enough changes you can make to fix this.
Before reading from the file set the value in all cells of your 2D-array to 0.
Even if you're not doing anything with it, read in the number of lines from the file before looping over the coordinates.
Unrelated to your code, the text file does not need to be in the same directory as the source file, it needs to be in the same directory that the program is executed from. I don't know where the execution will be done from in XCode, but it might be worth testing directly from your computer's shell, if you know how.
Edit:
The part of getPattern where you read from the file could look something like this:
int num_coordinates; // This will be the number of coordinate pairs your file says it has.
int i, j; // These will be your x, y coordinates.
infile >> num_coordinates;
for (int loop_ct = 0; loop_ct < num_coordinates; loop_ct++) {
infile >> i >> j;
master[j][i] = 1;
}

Related

2016 CCC S4 Combining riceball code?? C++

the quesiton is like this :
Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possible for his friend to eat. Alphonse can perform the following operations:
If two adjacent rice balls have the same size, Alphonse can combine them to make a new rice ball. The new rice ball's size is the sum of the two old rice balls' sizes. It occupies the position in the row previously occupied by the two old rice balls.
If two rice balls have the same size, and there is exactly one rice ball between them, Alphonse can combine all three rice balls to make a new rice ball. (The middle rice ball does not need to have the same size as the other two.) The new rice ball's size is the sum of the three old rice balls' sizes. It occupies the position in the row previously occupied by the three old rice balls.
Alphonse can perform each operation as many times as he wants.
Determine the size of the largest rice ball in the row after performing 0 or more operations.
Input Specification
The first line will contain the integer, N (1≤N≤400).
The next line will contain N space separated integers representing the sizes of the riceballs, in order from left to right. Each integer is at least 1 and at most 1000000.
For 1 of the 15 available marks, N=4.
For an additional 2 of the 15 available marks, N≤10.
For an additional 2 of the 15 available marks, N≤50.
Output Specification
Output the size of the largest riceball Alphonse can form.
Sample Input 1
Copy
7
47 12 12 3 9 9 3
Output for Sample Input 1
Copy
48
Explanation for Sample Input 1
One possible set of moves to create a riceball of size 48 is to combine 12 and 12, forming a riceball of size 24. Then, combine 9 and 9 to form a riceball of size 18. Then, combine 3, 18 and 3 to form a riceball of size 24. Finally, combine the two riceballs of size 24 to form a riceball of size 48.
My code is this:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int num;
cin >> num;
vector<int> rice;
int temp;
for(int i = 0; i< num; i++)
{
cin >> temp;
rice.emplace_back(temp);
}
int change = 0;
int chain = 0;
for(int i = 0; i > -1; i++)
{
for(int j = 0; j < num; j++)
{
for(int k = 1; k < num-j; k++)
{
if(rice[j] == rice [j+k])
{
chain++;
}
else
{
k = num-j;
}
}
if(chain != 0)
{
for(int m = 1; m <= chain; m++)
{
rice[j] = rice[j] + rice[j+1];
rice.erase(rice.begin()+j+1, rice.begin()+j+2);
change = 1;
if(m == chain)
{
num -= chain;
change = 1;
chain = 0;
j=-1;
continue;
}
}
}
if(j < num-2)
{
if (rice[j] == rice[j+2])
{
rice[j] = rice[j] + rice[j+1] + rice[j+2];
rice.erase(rice.begin()+j+1, rice.begin()+j+3);
change = 1;
j=-1;
num-=2;
continue;
}
}
}
if(change == 0)
{break;}
change = 0;
}
sort(rice.begin(),rice.end());
cout << rice.back() << endl;
return 0;
}
the test case that doesn't work right now is input: 10 1 2 1 2 1 2 1 2 1 2 and the output should be 14, but the only way to get that is to ignore the 1 at the beginning which i got no idea how to do / thks a lot

i want to show the sequence ftom 16 to 31 decimal number but its not showing :\ could anyone help me out here

#include <iostream>
using namespace std;
void bi(int a);
int main()
{
// here is the issue how do start a loop, where i want the answer from 16 to 31 numbers
int a=0;
cout<<"Baum-Sweet Sequence From 16 to 31 \n";
for(int j=a;j>16 && j<31;j++)
{
cout<<j;
}
bi(a);
system("Pause");
}
// Rest is working properly
void bi(int a)
{
int myArr[15],i=0,f=0,n=0;
for (int h = 0 ; h <= a; h++)
{
int num = h;
for (i = 0 ; i < 4 ; i++)
{
myArr[i] = num%2;
num = num/2;
}
for (int t = 0 ; t < 4 ; t++)
{
if (myArr[t]%2==0)
f++;
}
if (f%2==0)
cout << " = " << 1;
else
cout << " = " << 0;
cout <<endl;
}
}
i want to show the sequence from 16 to 31 decimal number but its not showing :\ could anyone help me out here
There is an error in the for loop.
The for loop has three parts separated by a semicolon.
for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
{
// Source code for the for-loop's body
}
The first part initializes the variable (e.g. "int j = 16;" means that through the variable j you begin counting by 16);
The second part checks a condition and it quits the loop when false (e.g. j <=31 means that it quits the loop when j will have value 31);
The third one is performed once each time the loop ends and then repeats (e.g. j++ means that at each iteration of the loop j will be incremented by 1).
Each iteration will execute the code in the body of the for loop.
Considering that you want to call the bi function for each value from 16 to 31 your for loop body should include bi(j). Your main should be modified like the code below:
int main()
{
cout<<"Baum-Sweet Sequence From 16 to 31 \n";
for(int j=16;j<=31;j++)
{
cout<<j;
bi(j);
}
system("Pause");
return 0;
}
Your problem is that you set j to 0, but then make a condition for the loop that it will only execute if j (which is set to a), is bigger than 16.
Your first thing to do is to make the loop conditions this:
for (int j = 16; j <= 32; j++)

2d array finished mine sweeper board program

Hello I am attempting to write a program and I seem to be stick. This program is in the end supposed to display two variants of a single 10 by 10 mine sweeper board. One of which where if there is a mine in that location it displays an asterisk(*) and if there is no mine it displays a period. The second variant also needs to display an asterisk where the mines are located on the board. But instead of a period it has to display the number of mines located 1 space away in any direction. I can't seem to think of how to easily write a program that will add up each of the mines located around each point. I also can't figure out why my program asks the initial question twice even if it meets the criteria for stopping the loop the first time around.
/*
Program: minesweeper(sort of)
The intention of this program is to display essentially two mine sweeper game boards one of which displays a *
where all of the mines would be located and a . where no mines are located. Then the second display
should display a * where all of the mines are and then display a number based on how many mines surround that space.
*/
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
double get_probability();
int plant_mines(bool mineCells[10][10], double prob);
int print(bool mineCells[10][10]);
int count_mines(bool mineCells[10][10], int mineCounters[10][10]);
int main()
{
//used to hold true if there is a mine located at said location or false if there is no mine
bool mineCells[10][10];
//used to hold a value of -5 if the corrosponding value on mineCells is true and if the corrosponding value on
//minecells is false it should add up and hold the value of how many mines are surroudning it.
int mineCounters[10][10];
get_probability();
double prob = get_probability();
plant_mines(mineCells, prob);
print(mineCells);
int stop;
cin >> stop;
return 0;
}
//asks the user for a number between 0 and 1 to use as the probability of mines apearing laster on.
double get_probability()
{
double prob;
bool repeat;
do
{
cout << "Please enter a number between 0 and 1: ";
cin >> prob;
//should execute once asking the user for a input between 0 and 1 and if the input is not between those two it shoud
//then continue to repeat untill the permaiters set in the question are met.
if (prob >= 0 && prob <= 1)
{
break;
}
else
{
repeat = true;
}
}while (repeat = true);
return prob;
}
//takes the probability given by the user and then uses it to generate that percentage of mines on the field
int plant_mines(bool mineCells[10][10], double prob)
{
srand((unsigned int)time(NULL));
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
//generates a random number between 0 and 1 and sets it equal to the variable random
double random = static_cast<double>(rand()) / RAND_MAX;
//is the variable random is less than or equal to the probability then the array is set to true meaning
//there is a mine located at that position
if (random <= prob)
{
mineCells[count][counter] = true;
}
//iff the random number is greater than the user input than there is no mine located at said position
else
{
mineCells[count][counter] = false;
}
}
}
return mineCells[10][10];
}
//Should count up the mines surronding the location to be output later on
//(the mines surrounding the location do include all mines 1 space diagonal, vertical and horizontal from said location)
int count_mines(bool mineCells[10][10], int mineCounters[10][10])
{
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
if (mineCells[count][counter] == 1)
{
mineCounters[count][counter] = 0;
}
else
{
}
}
cout << endl;
}
return 0;
}
//displays a * where ever a mine is locate and a . where ever a mine is not located
int print(bool mineCells[10][10])
{
for (int count = 0; count < 10; count++)
{
for (int counter = 0; counter < 10; counter++)
{
if (mineCells[count][counter] == 1)
{
cout << "*";
}
else
{
cout << ".";
}
}
cout << endl;
}
return 0;
}

Tracking where a user has landed in a board game

The idea of the program is that it simulates a game board with a user-inputted number of sides, and cells per side. The program then needs to simulate the user rolling two six-sided dice continuously until the starting tile is landed on or passed.
The problem I'm having is with a required function that records the spot the user landed on the most, per side of the game board (like a Monopoly board has 4 sides). For example, a Monopoly board with 4 sides, 10 cells on each side.
Side 1 has Cells 1 - 10
Side 2 has Cells 11 - 20
Side 3 has Cells 21 - 30
Side 4 has Cells 31 - 40
I'd need to report which cells I landed on the most for each of those four sides, or however many sides the gameboard has.
I have no idea how to start this function. I don't want the code to be written out for me, just a nudge in the right direction. As for the rest of the program, this is what I have:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int rollNDice(int nDice, int nSides) {
int diceSum = 0;
for (int i = 0; i < nDice; i++) {
int randnum = 1 + (rand() % (nSides - 1 + 1));
diceSum = diceSum + randnum;
}
return diceSum;
}
int mostLandings(const int boardVector, int startInterval, int endInterval) {
}
int main() {
srand(333);
int boardSides = 0;
int boardSpotsPerSide = 0;
int numberOfSims = 0;
int diceMove = 0;
int startInterval = 0;
int endInterval = 0;
const int boardVector = 0;
cout << "How many sides of the board are there? ";
cin >> boardSides;
cout << "How many spots on each side? ";
cin >> boardSpotsPerSide;
cout << "How many simulations? ";
cin >> numberOfSims;
for (int i = 0; i < boardSides;) {
const int boardVector = boardSides * boardSpotsPerSide;
int spotsPerInterval = boardVector / boardSides;
startInterval = spotsPerInterval / spotsPerInterval + (10 * i);
endInterval = spotsPerInterval * (i + 1);
mostLandings(boardVector, startInterval, endInterval)
i++;
}
}
One possible approach would be to start by calculating the total amount of squares on the board. The amount of squares q can be given by
q = 2s + (s-1)(n-1) = sn + s- n + 1
where s is the amount of squares on each side and n is the amount of sides. Next, store each roll value and loop over the sum of the roll value and the player's previous position, and store those values in a separate array. Take each element in that array modulo q and store in a separate array. This array should be the index of each square landed on after each subsequent turn, which you can manipulate and read from as you like.

I tried coding my own simple moving average in C++

I want a function that works.
I believe my logic is correct, thus my (vector out of range error) must be coming from the lack of familiarity and using the code correctly.
I do know that there is long code out there for this fairly simple algorithm.
Please help if you can.
Basically, I take the length as the "moving" window as it loops through j to the end of the size of the vector. This vector is filled with stock prices.
If the length equaled 2 for a 2 day moving average for numbers 1 2 3 4. I should be able to output 1.5, 2.5, and 3.5. However, I get an out of range error.
The logic is shown in the code. If an expert could help me with this simple moving average function that I am trying to create that would be great! Thanks.
void Analysis::SMA()
{
double length;
cout << "Enter number days for your Simple Moving Average:" << endl;
cin >> length;
double sum = 0;
double a;
while (length >= 2){
vector<double>::iterator it;
for (int j = 0; j < close.size(); j++){
sum = vector1[length + j - 1] + vector1[length + j - 2];
a = sum / length;
vector2.push_back(a);
vector<double>::iterator g;
for (g = vector2.begin(); g != vector2.end(); ++g){
cout << "Your SMA: " << *g;
}
}
}
}
You don't need 3 loops to calculate a moving average over an array of data, you only need 1. You iterate over the array and keep track of the sum of the last n items, and then just adjust it for each new value, adding one value and removing one each time.
For example suppose you have a data set:
4 8 1 6 9
and you want to calculate a moving average with a window size of 3, then you keep a running total like this:
iteration add subtract running-total output average
0 4 - 4 - (not enough values yet)
1 8 - 12 -
2 1 - 13 13 / 3
3 6 4 15 15 / 3
4 9 8 16 16 / 3
Notice that we add each time, we start subtracting at iteration 3 (for a window size of 3) and start outputting the average at iteration 2 (window size minus 1).
So the code will be something like this:
double runningTotal = 0.0;
int windowSize = 3;
for(int i = 0; i < length; i++)
{
runningTotal += array[i]; // add
if(i >= windowSize)
runningTotal -= array[i - windowSize]; // subtract
if(i >= (windowSize - 1)) // output moving average
cout << "Your SMA: " << runningTotal / (double)windowSize;
}
You can adapt this to use your vector data structure.
Within your outermost while loop you never change length so your function will run forever.
Then, notice that if length is two and closes.size() is four, length + j - 1 will be 5, so my psychic debugging skills tell me your vector1 is too short and you index off the end.
This question has been answered but I thought I'd post complete code for people in the future seeking information.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<double> vector1 { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
double length;
cout << "Enter number days for your Simple Moving Average:" << endl;
cin >> length;
double sum = 0;
int cnt = 0;
for (int i = 0; i < vector1.size(); i++) {
sum += vector1[i];
cnt++;
if (cnt >= length) {
cout << "Your SMA: " << (sum / (double) length) << endl;
sum -= vector1[cnt - length];
}
}
return 0;
}
This is slightly different than the answer. A 'cnt' variable in introduced to avoid an additional if statement.