I need to write a program, that if anyone ever played memory when they were younger does exactly that, that asks a user for 8 numbers. the numbers the user inputs need to be scattered across a 4x4 array. for example if a user enters 4 there need to be two of them in different sections of the array. so there will be a total of 16 "cards" or numbers. then the user will enter a array block to check two to check if they are equal - just like the game memory. im having problems knowing how to separate the list of numbers that the user inputs to enter each number into a different array. CAN ANYONE HELP PLEASE!!

Recommended Answers

All 7 Replies

You need to know about
- 2-dimensional arrays
- random number generating
- user input

What do you have so far?

You need to know about
- 2-dimensional arrays
- random number generating
- user input

What do you have so far?

Yes
The two dimensional array can be a pointer pointing to a one dimensional array. So that it will be very easy to allocate.

And use a forever loop with an escape sequnce in the input so the function goes on repeating.

I don't think I really understand. Is it? How could for example the array be?

Will the user enter the coordinates for the numbers they're targeting? So they could for example enter "1,4" and "3,4", and then the program have to find out if char[1][4] and char[3][4] is equal?

How would the array look like?

The array when completed with the user input should be something like this
if the user enters he numbers 1,2,3,4,5,6,7,8
the array should look like...
1 1 2 2
3 3 4 4
5 5 6 6
7 7 8 8

depending on the user input to check a card they need to enter 3,4 and then 5,7 and i need to translate that into char[3][4] and char[5][7]

i dont hav anything completed of the program becasue i need to know how to seperate the numbers. i know that i need to cout<<"What numbers would you like";
But when i cin>>numbers, i dont know how to seperate them

Shouldn't the equal number be placed random, not on the side of each other? It's pretty easy to guess where the other 2 is if you have found the first.. :)

this is the code i have - which works but i think there is an easier way to accomplish this task any suggestions??

#include <cstdlib>
#include <iostream>

using namespace std;
//This program will be used to ask the user for inputted numbers across an array.
//The user will then enter numbers in a coordinate system to see if two numbers are a match

int main(int argc, char *argv[])
{
    int game[4][4];
    int num1, num2, num3, num4, num5, num6, num7, num8;
    int i,j;
    int awsner, column1, row1 , column2, row2;

    i=0;
    j=0;
    cout<<"Enter first number ";
    cout<<endl;
    cin>>num1;
    game[i][j]=num1;
    i++;
    game[i][j]=num1;
    i++;

    cout<<"Enter second number ";
    cout<<endl;
    cin>>num2;
    game[i][j]=num2;
    i++;
    game[i][j]=num2;
    i=0;
    j++;

    cout<<"Enter third number ";
    cout<<endl;
    cin>>num3;
    game[i][j]=num3;
    i++;
    game[i][j]=num3;
    i++;

    cout<<"Enter fourth number ";
    cout<<endl;
    cin>>num4;
    game[i][j]=num4;
    i++;
    game[i][j]=num4;
    i=0;
    j++;

    cout<<"Enter fifth number ";
    cout<<endl;
    cin>>num5;
    game[i][j]=num5;
    i++;
    game[i][j]=num5;
    i++;

    cout<<"Enter sixth number ";
    cout<<endl;
    cin>>num6;
    game[i][j]=num6;
    i++;
    game[i][j]=num6;
    i=0;
    j++;

    cout<<"Enter seventh number ";
    cout<<endl;
    cin>>num7;
    game[i][j]=num7;
    i++;
    game[i][j]=num7;
    i++;

    cout<<"Enter eighth number ";
    cout<<endl;
    cin>>num8;
    game[i][j]=num8;
    i++;
    game[i][j]=num8;

  cout<<"Enter the row of the number you want to flip over ";
  cin>>row1;
  row1=row1-1;
  cout<<"Enter the column of the number you want to flip over ";
  cin>>column1;
  column1=column1-1;

  cout<<"Enter the row of the second number you want to flip over ";
  cin>>row2;
  row2=row2-1;
  cout<<"Enter the column of the second number you want to flip over ";
  cin>>column2;
  column2=column2-1;

  if (game[row1][column1]==game[row2][column2])
  {
        cout<<"Congrats you foound a match";
        cout<<endl;
  }
  else
  {
        cout<<"You suck, try again";
        cout<<endl;
  }

    system("PAUSE");
    return EXIT_SUCCESS;
}

One of the ideas you should adopt early is "avoid copy and paste" as much as possible. You have the following blocks repeated, with minor changes, many times.

i=0;
j=0;
cout<<"Enter first number ";
cout<<endl;
cin>>num1;
game[i][j]=num1;
i++;
game[i][j]=num1;
i++;

cout<<"Enter second number ";
cout<<endl;
cin>>num2;
game[i][j]=num2;
i++;
game[i][j]=num2;
i=0;
j++;

//etc etc etc

First of all it's tedious, secondly it's hard to update if you need to change the scope of the program, and, it's easy to make a mistake when you modify each repeated block.
Solution - use loops!

for( i = 0; i < 4; i++ )
  for( j = 0; j < 4; j+=2 )
  {
      cout << "enter a number: ";
      cin >> num;
      game[i][j] = num;
      game[i][j+1] = num
   }

This replaces the 8 chunks you have. Now you don't need those eight input variables (actually, you didn't need all eight anyway, you could have reused the input variable), your code is much smaller, and it's easier to verify you've not made any mistakes.

Once you've loaded the game array, you should put the playing part in a loop so the player can make multiple guesses. You have many options on how to know when the player is done - he gets some number of turns, or continues till he finds all pairs.

Get this much working, then attack how to make the game array randomly arranged rather than paired as is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.