I'm writing a code to swap integers in an array and I want to know how I can exit a loop without using a break statement and keeping my logic consistent. Here is my code below:
int swapped = 0;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
break;
}
}
if (swapped) {
break;
}
Use goto [I'll be bashed because of this].
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value "
<< arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
goto done;
}
}
done:
something;
for (int i = 1; i < arraycount && !swapped; ++i)
{
for (int j = 1; j < arrays[i][0] + 1 && !swapped; ++j)
{
if(arrays[i][j] % 2 != 0)
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with " << "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
}
}
}
this will do the same thing you have in inner loop.
Using a Break statement does not necessarily make your codes logic inconsistent and breaks are often useful to improve the readability of your code. But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. A modified version of your code is below, I have tried to modify it as little as possible so you can still see your code within the example. There are a few logical errors in your code that I have left in the example below that you might want to look into. In particular the line below will print "is odd" when in fact the number would be even. If you where wanting to check if the number arrays[0][first] is odd then the following if statement would be needed if (arrays[0][first] % 2 != 0) instead of if (arrays[0][first] % 2 == 0).
Logical Error
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
This is the code without using breaks.
bool swapped = true;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
int i = 1;
while ( (i < arraycount) && swapped)
{
int j = 1;
bool if_odd = true;
while ((j < arrays[i][0] + 1) && if_odd)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = false;
if_odd = false;
}
j++;
}
i++;
}
}
Related
I am working on an assignment for school and of course I receive very vague feedback on our code. The code I am working on is for Conway's Game of Life. I know I am super close. I have code that prints out the new generation but it's definitely not the correct one. It seems it is not counting the neighbors correctly - what should be identified as an alive neighbor doesn't seem to happen.
From our assignment as well (seeing examples of generations being formed) I notice the border cells do change which means I have to access them without going out of bounds. I feel I have been fruitless in my attempts to do this and I think I'm just missing something super obvious.
Please, any feedback would be amazing.
I have several print lines in attempts of debugging.
void gameOfLife(vector<vector<string>> &originalGrid, vector<vector<string>> &grid, int row, int col,
int Rows, int Cols){
//counts # of alive neighbors
int aliveNeighbors = 0;
string alive = "*";
for(int posX = row-1; posX <= row+1; posX++){
for(int posY = col-1; posX <= col+1; posX++){
std::cout << "I am in function - nested loop " << row << " " << col << std::endl;
if(posX == row && posY == col){
continue;
}
else if((posX >= 0 && posX < Rows) && (posY >= 0 && posY < Cols)){
std::cout << "I am in function - nested loop - else if " << row << " " << col << std::endl;
if(grid[posX][posY] == alive){
aliveNeighbors++;
std::cout << "alive neighbors: " << aliveNeighbors << std::endl;
}
}
}
}
/*
//top cell
if(grid[row][col-1] == "*"){
std::cout << "top cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom cell
if(grid[row][col+1] == "*"){
std::cout << "bottom cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//left cell
if(grid[row-1][col] == "*"){
std::cout << "left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//right cell
if(grid[row+1][col] == "*"){
std::cout << "right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//top left
if(grid[row-1][col-1] == "*"){
std::cout << "top left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//top right
if(grid[row+1][col-1] == "*"){
std::cout << "top right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom left
if(grid[row-1][col+1] == "*"){
std::cout << "bottom left cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
//bottom right
if(grid[row+1][col+1] == "*"){
std::cout << "bottom right cell " << row << " " << col << std::endl;
aliveNeighbors++;
}
*/
//test cases
//test case 1: Any live cell with fewer than two live neighbors dies (as if by underpopulation).
if(grid[row][col] == alive && aliveNeighbors < 2){
originalGrid[row][col] = ".";
}
//test case 2: Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
if(grid[row][col] == alive && aliveNeighbors > 3){
originalGrid[row][col] = ".";
}
//test case 3: Any live cell with two or three live neighbors lives, unchanged, to the next generation.
if(grid[row][col] == alive && (aliveNeighbors == 3 || aliveNeighbors == 2)){
originalGrid[row][col] = grid[row][col];
}
//test case 4: Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
if(grid[row][col] == "." && aliveNeighbors == 3){
originalGrid[row][col] = alive;
}
//prints updated grid
for(int i = 0; i < Rows; i++){
for(int j = 0; j < Cols; j++){
std::cout << originalGrid[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
return;
}
int main() {
int rows, col, numOfGen;
std::cin >> rows >> col >> numOfGen;
string cell;
vector<vector<string>> game;
for(int i = 0; i < rows; i++){
vector<string> temp;
for(int j = 0; j < col; j++){
std::cin >> cell;
temp.push_back(cell);
}
game.push_back(temp);
}
vector<vector<string>> firstGen;
firstGen.insert(firstGen.end(),game.begin(),game.end());
if(numOfGen == 0){
std::cout << "numOfGen == 0" << std::endl;
for(int i = 0; i < rows; i++){
for(int j = 0; j < col; j++){
std::cout << game[i][j] << " ";
}
std::cout << std::endl;
}
}
for(int g = 0; g <= numOfGen; g++){
for(int i = 1; i < rows; i++){
for(int j = 1; j < col; j++){
gameOfLife(game, firstGen, i, j, rows, col);
}
}
if(g == numOfGen){
for(int i = 0; i < rows; i++){
for(int j = 0; j < col; j++){
std::cout << game[i][j] << " ";
}
std::cout << std::endl;
}
}
}
return 0;
}
Looks like firstGen never gets updated, so you're just computing the first generation over and over. So your output is probably correct for a single generation, but it's the same for any number of generations. Also, check the conditions on your main driver loop: with for(int g = 0; g <= numOfGen; g++) the loop executes numOfGen+1 times.
I am trying to make a simple tic-tac-toe game. Right now I am trying to make it so the computer can't place inside of certain places in a 2d array. The computer is set to place randomly, and I am using loops and goto to make it random until it gets a suitable spot.
#include <iostream>
#include <string>
#include <ctime>
#include <Windows.h>
using namespace std;
string team;
string a = " ";
const int rows = 5;
const int elements = 5;
string Board[rows][elements] = { a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a,
"- ", "+ ", "- ", "+ ", "- ",
a, "| ", a, "| ", a };
void showBoard()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
cout << Board[i][j];
}
cout << endl;
}
}
int main()
{
int nonFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == " ")
nonFilled++;
}
}
int circleFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "0 ")
circleFilled++;
}
}
int crossFilled = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < elements; j++) {
if (Board[i][j] == a && a == "X ")
crossFilled++;
}
}
cout << "Welcome to tic-tac-toe game." << endl;
cout << endl;
showBoard();
cout << "Please select team if circle or cross: (0/X)" << endl;
cin >> team;
if (team == "0") {
int ifCircle = 1;
cout << "You have selected circle." << endl;
while (nonFilled > circleFilled + crossFilled) {
srand(time(NULL));
int x, y = 0;
int xx, xy = 0;
cout << "Select square: " << endl;
cout << "Y cord (0-4): ";
cin >> x;
cout << endl;
cout << "X cord (0-4): ";
cin >> y;
if (x == 0 || y == 0)
return 0;
/*if (Board[x - 1][y - 1] = "| ", "+ ", "- ")
{
cout << "You cannot place one there. " << endl;
nonFilled = 100;
}*/
int b = 1;
if (Board[x - 1][y - 1] == a)
b = 1;
else
b = 2;
switch (b) {
case 1:
Board[x - 1][y - 1] = "0 ";
break;
case 2:
cout << "You cannot place one there. " << endl;
continue;
}
/* if (Board[x - 1][y - 1] == a)
{
Board[x - 1][y - 1] = "0 ";
}
else
{
cout << "You cannot place one there." << endl;
}*/
cout << endl;
showBoard();
cout << "The opponent will now pick a square:" << endl;
system("pause");
xx = rand() % rows;
xy = rand() % elements;
int c = 1;
if (Board[xx][xy] == a)
c = 1;
else if (Board[xx][xy] == "| ", "+ ", "- ")
c = 2;
switch (c) {
case 1:
Board[xx][xy] = "X ";
break;
case 2: {
LOOP: // here is my label for the goto statement
while (true) // this loop
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| ", "+ ", "- ") {
goto LOOP; // goto statement
}
else {
Board[xx][xy] = "X ";
}
}
}
}
cout << endl;
cout << "The opponent has picked:" << endl;
showBoard();
}
}
else if (team == "X") {
int ifCircle = 0;
}
system("pause");
return 0;
}
The loop in question is at the very bottom, I am not sure if it is a problem with how I placed the label or how I am using the statement or if it is a problem with a different part of the code.
I have looked at loads of questions on goto statements inside of while loops and I couldn't find anything.
The immediate problem lies neither with the placement of your LOOP: label nor with the use of the goto statement (and I am not here going to get involved in the argument over whether or not that keyword should ever be used in a C++ program).
The problem is in the following line:
if (Board[xx][xy] == "| ", "+ ", "- ") {
(and the similar if else... statement a few lines earlier).
This does not do what you may want it to! In fact, it will always return a true value, as the nett result of the expression is simply, if ("- ") - which will always be a non-null (i.e. non-zero) address of the string literal.
What you need (if you are looking to match the indexed Board[][] string to any one of the three literals) is the following:
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
//...
The code you have, using the comma operator, evaluates each of the comma-separated expressions (in left-to-right order), discarding each but the last (right-most) value; the overall result is just that of the right-most expression.
EDIT: Having applied the fixes I suggested above, I then noticed another problem: Your while loop, as it stands, will never exit (either it will goto to the LOOP: or just keep running). Here is one (quick) way to fix the loop, which also removes the need for the goto statement:
case 2:
{
bool done = false;
while (!done)
{
xx = rand() % rows;
xy = rand() % elements;
if (Board[xx][xy] == "| " || Board[xx][xy] == "+ " || Board[xx][xy] == "- ") {
continue;
}
else {
Board[xx][xy] = "X ";
done = true;
}
}
}
There are more 'elegant' ways to achieve the same result but, hopefully, you will at least be able to follow (and understand) the fairly minor changes I have made to your code. Please feel free to ask for any further clarification and/or explanation.
So, i am currently doing a project. And while debuging i noticed that sometimes, a variable from the array changes value without me changing it.
The array is created to hold some values from tree structure.
int* tablica_synow1;
tablica_synow1 = (int*)malloc(sizeof(int) * number_of_sons(wskaznik_wierzcholki[zmienna1][i]));
//cout << "Initializing values to tablica_synow1" << endl;
for (int k = 0; k < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); k++) {
if (k == 0)
tmp = wskaznik_wierzcholki[zmienna2][j]->son;
tablica_synow1[k] = tmp->key;
//cout << "tablica_synow1 [" << k << "]" << tablica_synow1[k] << endl;
tmp = tmp->brother;
}
//cout <<"end of initialization"<< endl;
after this I do some other things in my code that don't involde tablica_synow1
and then when i want to use it
for (int m = 0; m < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); m++) {
int x = tablica_synow1[m];
cout << "tablica synow1 [" << m << "]" << tablica_synow1[m] << endl;
int y = i;
int a;
if (x > 0)
a = tablica2[y][zwroc_indeks(wartosci_tab[zmienna2], x)];
else
a = tablica4[y][x * (-1)];
if (a > najwieksza)
najwieksza = a;
}
the last element goes from 2 to -123781237 and it breaks my code
image
Okay so I keep getting this error when I try to run my code and can't figure out how to fix it.
(Unhandled exception at 0x75195608 in hw6.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0101F850.)\
I have included my source code below. Also the file that I am reading from is not long at all so I don’t think that’s the problem.
int main() {
//Initializes all of the variables, strings,boolean, and vectors
ifstream inFS;
int count = 1;
int i = 0;
int j = 0;
int location = 0;
char ch;
bool marker = 0;
string filename = "hw6-Fall2017.txt";
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
vector<int> locations;
vector<int> find_Locations;
vector<char> notFound;
vector<char> Found;
//Iterates through the file searching for each of the characters
while (count <= 62) {
inFS.open(filename);
if (!inFS.is_open()) {
cout << "Could not open the file: " << filename << endl;
return 1;
}
while (inFS.get(ch) && marker == 0) {
location++;
if (ch == list[i]) {
marker = 1;
}
}
inFS.close();
//Sets characters not found to have a location of 0
if (marker == 0) {
location = 0;
}
locations.push_back(location);
marker = 0;
location = 0;
i++;
count++;
}
//Creates a table printing out the characters and their susequent locations
for (i = 0;i < list.size();i++) {
if (locations.at(i) == 0) {
cout << list[i] << " " << setw(6) << "NotFnd"<< " ";
notFound.push_back(list[i]);
}
else {
cout << list[i] << " " << setw(6) << locations.at(i) << " ";
find_Locations.push_back(locations.at(i));
}
j++;
if (j == 5) {
cout << endl;
j = 0;
}
}
cout << endl << endl << endl;
//Sorts the characters in the order that they were found
sort(find_Locations.begin(), find_Locations.end());
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]);
j = locations.size();
}
}
}
count = 0;
j = 0;
//Creates a table printing out the characters in the oreder they were found
//in the text file along with their locations. Characters not found are
//displayed first with a location of "NotFnd".
for (i = 0;i < (Found.size() + notFound.size());i++) {
if (i < Found.size()) {
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";
}
else {
cout << notFound.at(j) << " " << setw(6) << "NotFnd" << " ";
j++;
}
count++;
if (count == 5) {
cout << endl;
count = 0;
}
}
system("pause");
return 0;
}
The answer is not easy to find with code review.
But this line looks nice in itself
string list = "ABCDEFGHIJKEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
but together with this
while (count <= 62) {
It looks suspicious, I think it should have been
while (count < list.size()) { // 2*26+10==62
An "Off by one error" which could cause a problem here
for (i = 0;i < find_Locations.size();i++) {
for (j = 0;j < locations.size();j++) {
if (find_Locations.at(i) == locations.at(j) && marker == 0) {
Found.push_back(list[j]); // <--- if J>=list.size()
j = locations.size();
}
}
}
And a potential crash at the marked line.
But the real error is here
Found.push_back(list[j]); // j should have been i
Which should cause a crash at
cout << Found.at(i) << " " << setw(6) << find_Locations.at(i)<< " ";
while (deckSize > 2)
{
one_Card = card_deck.back();
card_deck.pop_back();
two_Card = card_deck.back();
card_deck.pop_back();
three_Card = card_deck.back();
card_deck.pop_back();
oneCard_Name = card_name(one_Card);
twoCard_Name = card_name(two_Card);
threeCard_Name = card_name(three_Card);
oneCard_Suit = card_suit(one_Card);
twoCard_Suit = card_suit(two_Card);
threeCard_Suit = card_suit(three_Card);
oneCard_Rank = card_rank(one_Card);
twoCard_Rank = card_rank(two_Card);
threeCard_Rank = card_rank(three_Card);
bool between1 = (oneCard_Rank < threeCard_Rank && threeCard_Rank < twoCard_Rank);
bool between2 = (twoCard_Rank < threeCard_Rank && threeCard_Rank < oneCard_Rank);
cout << "Here are your two cards: " << endl;
cout << setw(10) << oneCard_Name << " of " << oneCard_Suit << setw(20) << twoCard_Name << " of " << twoCard_Suit << endl;
cout << "Do you think the next card will lie between these? (y/n): ";
cin >> user_input;
cout << endl << endl;
cout << "Here is your next card: " << endl;
cout << setw(10) << threeCard_Name << " of " << threeCard_Suit << endl << endl << endl;
count++;
if(user_input == "y" || user_input == "yes" || user_input == "Yes" || user_input == "Y")
{
if(between1 || between2)
{
cout << "You win!" << endl;
win++;
}
else
{
cout << "You lose!" << endl;
lose++;
}
}
else
{
if(between1 || between2)
{
cout << "You lose!" << endl;
lose++;
}
else
{
cout << "You win!" << endl;
win++;
}
}
}
cout << "You have played this game " << count << " times and you have won: " << win << " and lost " << lose << endl;
return 0;
}
These are the two subprograms that shuffle and initialize the deck
void initDeck(vector<int> &card_deck)
{
int i;
for(i = 0; i <= 51; i++)
{
card_deck[i] = i;
}
}
void shuffleDeck(vector<int> & card_deck)
{
int n;
for(n = 51; n >= 0; n--)
{
int i = randomize();
int temp = card_deck[i];
int temp2= card_deck[n];
card_deck[n] = temp;
card_deck[i] = temp2;
}
}
After when I run the program it allows me to run it, but when I reach to the number less than the condition in the while loop it just gives me an error, and does not finish the program. I had this error earlier and fixed it, so I have a basic understanding of what the error means. From my knowledge it is trying to collect numbers past the vector length. However this time I don't see my error at all.
deckSize is not being set/updated anywhere. It should rather be card_deck.size()
You should use push_back and emplace for the type vector like this:
void initDeck(vector<int> &card_deck){
int i;
for(i = 0; i <= 51; i++)
{
card_deck.push_back(i);
}
}
Take a see to this link
Try this:
void initDeck(vector<int> &card_deck)
{
int i;
for(i = 0; i <= 51; i++)
{
card_deck.push_back(i);
}
}
void shuffleDeck(vector<int> & card_deck)
{
int n;
for(n = 51; n >= 0; n--)
{
int i = randomize();
int temp = card_deck[i];
int temp2= card_deck[n];
card_deck[n] = temp;
card_deck[i] = temp2;
}
}
For generating random number see this and this. Or you can find other solution that is more reliable.