How do I loop through 2d arrays in c++? - c++

I am trying to figure out how to loop through a 2d array horizontally vertically, diagonally and sub diagonally to see if the array is equal to a char.if the array is equal to the char then a bool should return true, but my problem is the bool randomly returns true. the bool only returns true if the char is equal to array at a certain column and row. Here i am hard coding it in but i want to do it with for loops.
Let me know if you need any more info or if something needs to be explained
if (mCard[0][0] == '*' && mCard[0][1] == '*'&& mCard[0][2] == '*'&& mCard[0][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
if (mCard[1][0] == '*' && mCard[1][1] == '*'&& mCard[1][2] == '*'&& mCard[1][3] == '*' && mCard[1][4] == '*') {
CheckBingo = true;
}
if (mCard[2][0] == '*' && mCard[2][1] == '*'&& mCard[2][2] == '*'&& mCard[2][3] == '*' && mCard[2][4] == '*') {
CheckBingo = true;
}
if (mCard[3][0] == '*' && mCard[3][1] == '*'&& mCard[3][2] == '*'&& mCard[3][3] == '*' && mCard[3][4] == '*') {
CheckBingo = true;
}
if (mCard[4][0] == '*' && mCard[4][1] == '*'&& mCard[4][2] == '*'&& mCard[4][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
if (mCard[0][0] == '*' && mCard[1][0] == '*'&& mCard[2][0] == '*'&& mCard[3][0] == '*' && mCard[4][0] == '*') {
CheckBingo = true;
}
if (mCard[0][1] == '*' && mCard[1][1] == '*'&& mCard[2][1] == '*'&& mCard[3][1] == '*' && mCard[4][1] == '*') {
CheckBingo = true;
}
if (mCard[0][2] == '*' && mCard[1][2] == '*'&& mCard[2][2] == '*'&& mCard[3][2] == '*' && mCard[4][2] == '*') {
CheckBingo = true;
}
if (mCard[0][3] == '*' && mCard[1][3] == '*'&& mCard[2][3] == '*'&& mCard[3][3] == '*' && mCard[4][3] == '*') {
CheckBingo = true;
}
if (mCard[0][4] == '*' && mCard[1][4] == '*'&& mCard[2][4] == '*'&& mCard[3][4] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for daigonal
if (mCard[0][0] == '*' && mCard[1][1] == '*'&& mCard[2][2] == '*'&& mCard[3][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for sub diagonal
if (mCard[4][0] == '*' && mCard[3][1] == '*'&& mCard[2][2] == '*'&& mCard[1][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
here I tried using for loops but it does int always return true if the array is equal to the char.
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[row][col] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[col][row] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}

mCard[col][row]
...
row and col have to be flipped.
You then start the loop assuming that match is set. If one of the cells is not matched, then match is false. See example below.
Make sure the values are initialized.
int main()
{
char mCard[5][5] = {
'*','x','*','*','*',
'*','*','*','*','*',
'*','*','*','*','*',
'*','*','x','*','*',
'*','*','*','*','*',
};
bool CheckBingo = true;
for(int row = 0; row < 5; row++)
{
CheckBingo = true;
for(int col = 0; col < 5 && CheckBingo; col++)
{
if(mCard[row][col] != '*')
CheckBingo = false;
}
if(CheckBingo)
cout << "bingo on row " << row << "\n";
}
bool match = true;
for(int row = 0; row < 5 && match; row++)
for(int col = 0; col < 5 && match; col++)
if(mCard[row][col] != '*')
match = false;
if(match) cout << "bingo for all lines\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][i] != '*')
match = false;
if(match) cout << "top-left to bottom-right match\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][4-i] != '*')
match = false;
if(match) cout << "top-right to bottom-left match\n";
return 0;
}

Related

Error handling exponent ("e") preceding digit

I'd like to create a function that checks whether a number input with an exponent such as 1.32e2, 1e3, +1.32e+2 indeed has the e in the correct position, after a number, not before it (such as e1 or .e4)
I'm a beginner so it's just trial and error at this point. This is what I have so far:
bool validate_alpha_e(string op) {
if (count_alpha(op) <= 1) {
if (count_es(op) == 1) {
//return true;
int length = op.length();
for (int i = 0; i < length; i++) {
char ch = op[i-1];
if (ch == 'e' || ch == 'E') {
if (i-1 != isdigit(i)) {
return false;
}
}
}
return true;
}
Worked it out. Thanks for the comments but I wasn't able to figure it out.
So I had to create 3 separate functions (see below)
First function validates for correct input before the e.
Second function validates for correct input after the e.
Third function validates that the first character is not an e.
Put all these together in a bool if statement and if it returns false then the input is not correct.
bool validate_e_after(string op) {
int length = op.length();
for (int i = 1; i < length; i++) {
char ch = op[i-1];
if (ch == 'e' || ch == 'E') {
char ch2 = op[i];
if (isdigit(ch2) || ch2 == '+' || ch2 == '-') {
return true;
}
else {
return false;
}
}
}
}
bool validate_e_before(string op) {
int length = op.length();
for (int i = 1; i < length; i++) {
char ch = op[i+1];
if (ch == 'e' || ch == 'E' && i != 0) {
char ch2 = op[i];
if (isdigit(ch2) || ch2 == '+' || ch2 == '-') {
return true;
}
else {
return false;
}
}
}
}
bool validate_ezero(string op) {
int length = op.length();
for (int i = 0; i < length; i++) {
char ch = op[i];
if (ch == 'e' or ch == 'E') {
if (i == 0) {
return false;
}
}
}
return true;

C++ Minimax Algorithm for Connect Four stuck in infinite loop (I think)

I am currently attempting to code a connect 4 AI that uses the minimax algorithm to play optimally. However, it seems to currently be stuck in an infinite loop, definitely being caused by the minimax function shown here.
Each recursive call is closer to the base case since every time the minimax function is called, one more cell in the grid will be filled in. This makes the grid one step closer to either having a winner, or being filled up with no winner. And, after the for loop iterates and calls minimax through all cells of the grid, the function should return.
Things I have tried:
Reducing the depth variable
Reducing board size (it was previously 5x5, now it is 4x4)
Changing values of variables 'best' and 'worst'
Changing the function that checks if there is a winner
My guess is that for some reason, some of the calls to minimax are never reaching their base case. Any help as to what is going on would be greatly appreciated.
Code here (fixed formatting):
#include <iostream>
#include <vector>
using namespace std;
pair<bool, char> isWinner(std::vector<std::vector<char> >& grid, char aiMark, char hMark) {
//bool = whether game is concluded, char = winner of game,
'-' if none. An unfinished game will always be {false, '-'}.
pair<bool, char> temp;
temp.first = true;
temp.second = '-';
//checks whether board is full, if it is full, temp is changed to {true, '-'} until one of the other for loops is triggered
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(grid[i][j] != '-') {
temp.first = (temp.first && true);
} else {
temp.first = (temp.first && false);
}
}
}
//horizontal 4-in-a-row check
for (int i = 0; i < 4; i++){
if (grid[i][0] == aiMark && grid[i][1] == aiMark && grid[i][2] == aiMark && grid[i][3] == aiMark){
temp.first = true;
temp.second = aiMark;
return temp;
} else if (grid[i][0] == hMark && grid[i][1] == hMark && grid[i][2] == hMark && grid[i][3] == hMark) {
temp.first = true;
temp.second = hMark;
return temp;
}
}
//vertical 4-in-a-row check
for (int j = 0; j < 4; j++){
if (grid[0][j] == aiMark && grid[1][j] == aiMark && grid[2][j] == aiMark && grid[3][j] == aiMark){
temp.first = true;
temp.second = aiMark;
return temp;
} else if (grid[0][j] == hMark && grid[1][j] == hMark && grid[2][j] == hMark && grid[3][j] == hMark){
temp.first = true;
temp.second = hMark;
return temp;
}
}
//upwards diagonal check
if (grid[3][0] == aiMark && grid[2][1] == aiMark && grid[1][2] == aiMark && grid[0][3] == aiMark) {
temp.first = true;
temp.second = aiMark;
return temp;
} else if (grid[3][0] == hMark && grid[2][1] == hMark && grid[1][2] == hMark && grid[0][3] == hMark) {
temp.first = true;
temp.second = hMark;
return temp;
}
//downwards diagonal check
if (grid[3][3] == aiMark && grid[2][2] == aiMark && grid[1][1] == aiMark && grid[0][0] == aiMark) {
temp.first = true;
temp.second = aiMark;
return temp;
} else if (grid[3][3] == hMark && grid[2][2] == hMark && grid[1][1] == hMark && grid[0][0] == hMark) {
temp.first = true;
temp.second = hMark;
return temp;
}
return temp;
}
int minimax(std::vector<std::vector<char> >& grid, int depth, bool maxim, char aiMark, char hMark) {
//grid = connect-4 board, depth = current depth of the minimax call, maxim = if the current player is maximizing, aiMark = X, hMark = O
pair<bool, char> result = isWinner(grid, aiMark, hMark);
if(result.first != false) { //if the game is concluded
if(result.second == aiMark) { //maximizing player wins
return depth;
} else if (result.second == hMark) { //max. player loses
return -depth;
} else { //tie
return 0;
}
} else {
if(maxim == true) { //maximizing player's turn
int best = -17;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(grid[i][j] == '-') { //check if space is empty
grid[i][j] = aiMark;
int score = minimax(grid, depth - 1, !maxim, aiMark, hMark); //call minimax with new grid + other player's turn
best = max(best, score); //update best score
grid[i][j] = '-'; //backtrack
}
}
}
return best; //return the best possible score
} else {
int worst = 17;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(grid[i][j] == '-') {
grid[i][j] = hMark;
int score = minimax(grid, depth - 1, !maxim, aiMark, hMark);
worst = min(worst, score);
grid[i][j] = '-';
}
}
}
return worst; //return worst score for maximizing player
}
}
}
void bestMove(std::vector<std::vector<char> >& grid, char aiMark, char hMark) {
int best = -17;
int finalRow;
int finalCol;
pair<bool, char> result = isWinner(grid, aiMark, hMark);
if(result.first != false) { //if game is concluded
return;
}
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(grid[i][j] == '-') { //check if space is empty
grid[i][j] = aiMark; //place mark in row/column
int score = minimax(grid, 16, true, aiMark, hMark); //the 'score' player will get from placing their mark here
if(score > best) {
best = score;
finalRow = i;
finalCol = j; //update best score and best row/column accordingly
}
grid[i][j] = '-'; //backtrack
}
}
}
grid[finalRow][finalCol] = aiMark; //change the best row + best column square to the player's mark
return;
}

Program for NFA to accept strings that has atleast one character occurring in a multiple of 3

So there this c++ code for the following question: NFA to accept strings that have at least one character occurring in a multiple of 3.
The problem is it's showing "accepting" as the output for all inputs.
for example: bbaacc output should be "not accepting" but this code is showing excepting
I am not able to figure out where I went wrong. Can somebody help me find where?
#include <bits/stdc++.h>
int nfa = 1;
int flag = 0;
using namespace std;
void state1(char c)
{
if (c == 'a')
nfa = 2;
else if (c == 'b' || c == 'c')
nfa = 1;
else
flag = 1;
}
void state2(char c)
{
if (c == 'a')
nfa = 3;
else if (c == 'b' || c == 'c')
nfa = 2;
else
flag = 1;
}
void state3(char c)
{
if (c == 'a')
nfa = 1;
else if (c == 'b' || c == 'c')
nfa = 3;
else
flag = 1;
}
void state4(char c)
{
if (c == 'b')
nfa = 5;
else if (c == 'a' || c == 'c')
nfa = 4;
else
flag = 1;
}
void state5(char c)
{
if (c == 'b')
nfa = 6;
else if (c == 'a' || c == 'c')
nfa = 5;
else
flag = 1;
}
void state6(char c)
{
if (c == 'b')
nfa = 4;
else if (c == 'a' || c == 'c')
nfa = 6;
else
flag = 1;
}
void state7(char c)
{
if (c == 'c')
nfa = 8;
else if (c == 'b' || c == 'a')
nfa = 7;
else
flag = 1;
}
void state8(char c)
{
if (c == 'c')
nfa = 9;
else if (c == 'b' || c == 'a')
nfa = 8;
else
flag = 1;
}
void state9(char c)
{
if (c == 'c')
nfa = 7;
else if (c == 'b' || c == 'a')
nfa = 9;
else
flag = 1;
}
bool checkA(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 1)
state1(s[i]);
else if (nfa == 2)
state2(s[i]);
else if (nfa == 3)
state3(s[i]);
}
if (nfa == 1) {
return true;
}
else {
nfa = 4;
}
}
bool checkB(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 4)
state4(s[i]);
else if (nfa == 5)
state5(s[i]);
else if (nfa == 6)
state6(s[i]);
}
if (nfa == 4) {
return true;
}
else {
nfa = 7;
}
}
bool checkC(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 7)
state7(s[i]);
else if (nfa == 8)
state8(s[i]);
else if (nfa == 9)
state9(s[i]);
}
if (nfa == 7) {
return true;
}
}
int main()
{
string s = "bbbca";
int x = 5;
if (checkA(s, x) || checkB(s, x) || checkC(s, x)) {
cout << "ACCEPTED";
}
else {
if (flag == 0) {
cout << "NOT ACCEPTED";
return 0;
}
else {
cout << "INPUT OUT OF DICTIONARY.";
return 0;
}
}
}

Probleem with substr in c++

I made binary calculator. It works, but only when numbers doesnt start with 0. if yes, it shows this:
How I should repair the program, please? (vysledek means result)
Part of code with substr:
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
Whole program:`
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
int main()
{
cout << "Zadejte dve binarni cisla:" << endl;
string cislo1, cislo2;
cin >> cislo1 >> cislo2;
int c = 0;
for (c; c < cislo1.length(); c++)
{
if ((cislo1[c] == '0') || (cislo1[c] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
int d = 0;
for (d; d < cislo2.length(); d++)
{
if ((cislo2[d] == '0') || (cislo2[d] == '1'))
{
continue;
}
else
{
cout << "Nespravny vstup.";
return 0;
}
}
string stringS2 = "";
for(unsigned i = 0; i < cislo1.length(); i++)
{
stringS2 += cislo1[cislo1.length()-1-i];
}
cislo1 = stringS2;
string stringS3 = "";
for(unsigned i = 0; i < cislo2.length(); i++)
{
stringS3 += cislo2[cislo2.length()-1-i];
}
cislo2 = stringS3;
/*
cout << cislo1 << endl;
cout << cislo2 << endl;
cout << cislo1.length() << endl;
cout << cislo2.length() << endl;
*/
int p;
if (cislo1.length() > cislo2.length())
{
p = cislo1.length();
int f = cislo2.length();
for(f; f < cislo1.length(); f++)
{
cislo2[f] = '0';
}
}
else
{
p = cislo2.length();
int f = cislo1.length();
for(f; f < cislo2.length(); f++)
{
cislo1[f] = '0';
}
}
int e = 0;
//cout << p << endl;
string vysledek;
int t;
t = (p - 1);
cislo1[p];
cislo2[p];
for(e; e < p; e++)
{
/*nasledujici podminky jsou pro pripad, kdy cisla maji odlisny pocet znaku
if(cislo1[e] == NULL)
{
cislo1[e] == '0';
}
if(cislo2[e] == NULL)
{
cislo2[e] == '0';
}*/
if (cislo1[e] == '0' && cislo2[e] == '0')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1')|| ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1') )
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
else if ((cislo1[e] == '1' && cislo2[e] == '0') || (cislo1[e] == '0' && cislo2[e] == '1') )
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '0';
}
else
{
vysledek[t] = '1';
}
// cout << vysledek[t] << endl;
t--;
}
else if (cislo1[e] == '1' && cislo2[e] == '1')
{
if ((cislo1[e-1] == '1' && cislo2[e-1] == '1') || ((cislo1[e-1] == '0' && cislo2[e-1] == '1')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '0')&& vysledek[t+1] == '0') || ((cislo1[e-1] == '1' && cislo2[e-1] == '1')&& vysledek[t+1] == '1'))
{
vysledek[t] = '1';
}
else
{
vysledek[t] = '0';
}
//cout << vysledek[t] << endl;
t--;
}
}
cout << "Soucet: ";
int u;
u = (p - 1);
if(((cislo1[u] == '0' && cislo2[u] == '1')&& vysledek[0] == '0') || ((cislo1[u] == '1' && cislo2[u] == '0')&& vysledek[0] == '0') || (cislo1[u] == '1' && cislo2[u] == '1') )
cout << '1';
int o = 0;
int z;
int w = 0;
while(vysledek[0] == '0')
{
vysledek = vysledek.substr(1, vysledek.length());
w++;
}
int aa;
aa = p-w;
for(o; o < aa; o++)
{
cout << vysledek[o];
}
return 0;
}
`
string vysledek; creates an empty string. It is never given any other size.
All following uses of vysledek[t] and vysledek.substr are all out of range.

How to ignore certain letters and spaces in character arrays

Trying to make an else statement that get rid of all other letter and spaces then the ones i want. This function is to change user inputted letters into other letters
using namespace std;
void dna_to_rna(char rna[])
{
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
rna[i] = 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
rna[i] = 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
rna[i] = 'C';
else if (rna[i] == 't' || rna[i] == 'T')
rna[i] = 'A';
}
What should the else statement look like in order to drop all other chars?
If the input parameter can be changed to std::string, then you can use one of the following implementation:
void dna_to_rna(std::string& rna)
{
auto it = rna.begin();
while (it != rna.end())
{
if (*it == 'a' || *it == 'A') *it = 'U';
else if (*it == 'c' || *it == 'C') *it = 'G';
else if (*it == 'g' || *it == 'G') *it = 'C';
else if (*it == 't' || *it == 'T') *it = 'A';
else
{
it = rna.erase(it);
continue; // it already "points" to the next element
}
++it;
}
}
std::string dna_to_rna(const std::string& dna)
{
std::string rna;
for (auto c : dna)
{
if (c == 'a' || c == 'A') rna += 'U';
else if (c == 'c' || c == 'C') rna += 'G';
else if (c == 'g' || c == 'G') rna += 'C';
else if (c == 't' || c == 'T') rna += 'A';
}
return rna;
}
Maybe like this:
using namespace std;
void dna_to_rna(char rna[])
{
string s = "";
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
s += 'U';
else if (rna[i] == 'c' || rna[i] == 'C')
s += 'G';
else if (rna[i] == 'g' || rna[i] == 'G')
s += 'C';
else if (rna[i] == 't' || rna[i] == 'T')
s += 'A';
}
strcpy(rna, s.c_str());
}
The idea is simply to use a std::string as a temporary buffer. The string is empty to start with. Then you add the characters you want one-by-one. When done with the loop, copy the content of the std::string back to the rna-array.
To make you code much simpler, and easier to read:
using namespace std;
void dna_to_rna(char rna[]) {
int arrLength = sizeof(rna)/sizeof(rna[0]); // Get size of array
for (int i = 0; i < arrLength; i++){
if (toupper(rna[i]) == 'A'){
rna[i] = 'U';
}
else if (toupper(rna[i]) == 'C') {
rna[i] = 'G';
}
else if (toupper(rna[i]) == 'G'){
rna[i] = 'C';
}
else if (toupper(rna[i]) == 'T'){
rna[i] = 'A';
}
}
}
I created a second array and as long as the information that I was looking for met the criteria that was necessary I placed it into the second array making sure that the position that I was placing it in the array was always in the right spot by creating a second variable that would count the the proper position in the array then just cout array
using namespace std;
void dna_to_rna(char rna[])
{
int x = 0;
char newrna[100];
for (int i = 0; i < 100; i++)
{
if (rna[i] == 'a' || rna[i] == 'A')
{
newrna[x] = 'U';
x++;
}
else if (rna[i] == 'c' || rna[i] == 'C')
{
newrna[x] = 'G';
x++;
}
else if (rna[i] == 'g' || rna[i] == 'G')
{
newrna[x] = 'C';
x++;
}
else if (rna[i] == 't' || rna[i] == 'T')
{
newrna[x] = 'A';
x++;
}
}