mardi 3 mars 2015

Random Generated Numbers Multi-Dimensional Arrays

I want the user to input 2 rows and 2 columns. Then the program will generate a random number in that certain place and if both of the are equal, you win and if it's not equal, you lose.


|* * * *

|* * * *

|* * * *

|* * * *


Please enter row

3

Please enter column

3

Please enter row

2

Please enter column

1


|* * * *

|7 * * *

|* * 2 *

|* * * *

You lose!

Enter 1 to play again. Enter 0 to quit.


or


|* * * *

|* * * *

|* * * *

|* * * *


Please enter row

3

Please enter column

3

Please enter row

2

Please enter column

1


|* * * *

|2 * * *

|* * 2 *

|* * * *

You win!

Enter 1 to play again. Enter 0 to quit.


My output:

|* * * *

|
* * * *

|
* * * *

|
* * * *


Please enter row

3

Please enter column

3

Please enter row

2

Please enter column

1


|6

|9

|8

|5

Enter 1 to play again. Enter 0 to quit.



My code:
#include<iostream>
using namespace std;

int main()
{
int table[4][4], row1, row2, column1, column2, choice;

do
{
system("cls");
cout << " 1 2 3 4 \n\n";
cout << "----------" << endl;
for (int i = 0; i < 4; i++)
cout << "|* * * *" << endl;

cout << "Please insert row \n";
cin >> row1;
cout << "Please insert column \n";
cin >> column1;
cout << "Please insert row \n";
cin >> row2;
cout << "Please insert column \n";
cin >> column2;

cout << endl;

cout << " 1 2 3 4 \n\n";
cout << "----------" << endl;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
{
cout << "|";
if (table[i][j] == table[row1][column1])
cout << 1 + (rand() % 9) << endl;
else if (table[i][j] == table[row2][column2])
cout << 1 + (rand() % 9) << endl;
else if (table[i][j] != table[row1][column1])
cout << "*";
else if (table[i][j] != table[row2][column2])
cout << "*";
}
}
cout << "Enter 1 to play again. Enter 0 to quit. ";
cin >> choice;

} while (choice == 1);

system("pause>0");
return 0;


}





Aucun commentaire:

Enregistrer un commentaire