WithoutNameZin
WithoutNameZin

Reputation: 107

Bingo test with c++

I was testing after some years without code, to make a mini bingo test...

In the end, I had:

#include <iostream>

using namespace std;

int main()
{
 int cartela[5][3] = { {1, 2, 3}, {6,7,8}, {11,12,13}, {14,15,16}, {17,18,19} } ;
 int sorteado[8];
 int detectado[5];
 cout << "Insira os 8 numeros sorteados: ";
 cin >> sorteado[0] >> sorteado[1] >> sorteado[2] >> sorteado[3] >> sorteado[4] >> sorteado[5] >>sorteado[6] >> sorteado[7];

for (int tsort=0; tsort<9; tsort++) {
    for (int i=0; i<6; i++) {
        for (int j=0; j<4; j++) {
        if (cartela[i][j] == sorteado[tsort])  {
                  detectado[i]++;
                                    }
    }
}
}
cout << "cartela 1: " << detectado[0]<<"\n";
cout << "cartela 2: " << detectado[1]<<"\n";
cout << "cartela 3: " << detectado[2]<<"\n";
cout << "cartela 4: " << detectado[3]<<"\n";
cout << "cartela 5: " << detectado[4]<<"\n";
return 0;
}

or pastebin:http://pastebin.com/TLYAZTtE

And if you note, the objective is get the number of numbers that the user typed that is in the mounted games (cartela[5][3]).

And, the result is reached only in the game 2 and 3. In the rest, the result is INCREDIBLE. LOL

Anyone may help me find what was my error?

THANKS!

Upvotes: 0

Views: 923

Answers (1)

Amadeus
Amadeus

Reputation: 10685

Your indexes are wrong. For example, look at this:

int sorteado[8];

i.e. an index of 8 values, but in the for loop:

for (int tsort=0; tsort<9; tsort++)

you are looping 9 elements, from 0 to 8. To correct it:

for (int tsort=0; tsort< 8; tsort++) {
    for (int i=0; i< 5; i++) {
        for (int j=0; j< 3; j++) {
        if (cartela[i][j] == sorteado[tsort])  {
              detectado[i]++
        }
}

}

Now, really, what is your question?

UPDATED

You forgot to initialize "detectado", so, update your code to:

int detectado[5] = {0};
                ^^^^^^

Upvotes: 1

Related Questions