help help help with arrays!

please help me with the 2d arrays

Page 1 of 1

13 Replies - 1176 Views - Last Post: 30 October 2008 - 03:44 AM Rate Topic: -----

#1 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

help help help with arrays!

Posted 29 October 2008 - 08:27 PM

I have this assignment that consist of creating a memory matching game with arrays, but without using the java swing or awt. This is the topic:

"Write a program that plays the memory matching game. use sixteen cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8.your program should allow the player to specify the cards that he/she would like to select trough a coordinate system"

I know you guys dont do homework, and I think thats really cool, but I really really dont know anything about arrays and I have a really bad teacher. If you guys at least can give me a direction or a pseudocode that I can use as a guide, I would really appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: help help help with arrays!

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: help help help with arrays!

Posted 29 October 2008 - 08:39 PM

The policy sure is that we don't do homework however I understand how tough it is to learn this stuff on your own.... for an intro to arrays try this....


http://www.dreaminco...wtopic27806.htm

For a first project in arrays that is pretty tough... try doing some easier ones to start and get comfortable with them first then try to tackle that code
Was This Post Helpful? 0
  • +
  • -

#3 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

Re: help help help with arrays!

Posted 29 October 2008 - 08:47 PM

View Postmarkhazlett9, on 29 Oct, 2008 - 08:39 PM, said:

The policy sure is that we don't do homework however I understand how tough it is to learn this stuff on your own.... for an intro to arrays try this....


http://www.dreaminco...wtopic27806.htm

For a first project in arrays that is pretty tough... try doing some easier ones to start and get comfortable with them first then try to tackle that code



thanks, I've been working on that link for several hours, im doing my best
Was This Post Helpful? 0
  • +
  • -

#4 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: help help help with arrays!

Posted 29 October 2008 - 08:50 PM

View Postisale8888, on 29 Oct, 2008 - 08:47 PM, said:

View Postmarkhazlett9, on 29 Oct, 2008 - 08:39 PM, said:

The policy sure is that we don't do homework however I understand how tough it is to learn this stuff on your own.... for an intro to arrays try this....


http://www.dreaminco...wtopic27806.htm

For a first project in arrays that is pretty tough... try doing some easier ones to start and get comfortable with them first then try to tackle that code



thanks, I've been working on that link for several hours, im doing my best



just as a tip when you start the program, you will need a 2 dimensional array to hold the numbers. Also try to code it up when you feel comfortable and then if you're having trouble post what you have on here and we can help you out a little more. The members here always seem to want to help more when they can look at some code.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:03 PM

View Postisale8888, on 29 Oct, 2008 - 08:27 PM, said:

"Write a program that plays the memory matching game. use sixteen cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8.


View Postisale8888, on 29 Oct, 2008 - 08:27 PM, said:

I have a really bad teacher.

I beleive you... How can you match a 4 X 4 array with pairs of numbers 1 to 8 ?
Was This Post Helpful? 0
  • +
  • -

#6 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:05 PM

View Postpbl, on 29 Oct, 2008 - 09:03 PM, said:

View Postisale8888, on 29 Oct, 2008 - 08:27 PM, said:

"Write a program that plays the memory matching game. use sixteen cards that are laid out in a 4x4 square and are labeled with pairs of numbers from 1 to 8.


View Postisale8888, on 29 Oct, 2008 - 08:27 PM, said:

I have a really bad teacher.

I beleive you... How can you match a 4 X 4 array with pairs of numbers 1 to 8 ?



good call I didn't even see that! very confusing
Was This Post Helpful? 0
  • +
  • -

#7 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:12 PM

so a 2 dimensional arrays will give me something like rows and columns?


what will be the output for this example

int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];

for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
{
for (int colIndex = 0; colIndex < cols; colIndex ++)
{ tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }
}
Was This Post Helpful? 0
  • +
  • -

#8 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:14 PM

There will be not output as there is no System.out.println to display the contents of the array...

to output the arrays you will need to follow the same procedure as filling them except for output
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:23 PM

Seriously here is a good start.... but now you have to show some efforts

import java.util.Scanner;

// memory matching game
public class Memory {

	// OK wont teach you how to use a random generator lets "hard code" the values
	// we will make them 3 digits long for console display
	int[][] initialValue = {{100, 200, 300, 400}, {400, 500, 600, 700}, {400, 300, 200, 100}, {700, 600, 500, 400}};
	// boolean to see if a value is diplayed or not
	boolean[][] displayed = new boolean[4][4];
	Scanner scan;
	// constructor
	Memory() {
		boolean gameIsOver = false;
		scan = new Scanner(System.in);
		while(!gameIsOver) {
			displayCard();			
			System.out.print("Enter X of card to show: ");
			int x = scan.nextInt();
			System.out.print("Enter Y of card to show: ");
			int y = scan.nextInt();
			displayed[x][y] = true;
		}
	}
	
	// display the array of cards
	void displayCard() {
		System.out.println("The actual game:");
		// display the value of the card showed or its coordinate if not showed
		for(int j = 0; j < 4; j++) {
			for(int i = 0; i < 4; i++) {
				if(displayed[i][j])
					System.out.print(" " + initialValue[i][j] + "   ");
				else
					System.out.print(" " + i + "," + j + "   ");
			}
			System.out.println();
		}
	}
	
	public static void main(String[] arg) {
		new Memory();
	}
}



I haven't tried to compile and run it but it should work
Was This Post Helpful? 0
  • +
  • -

#10 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:43 PM

so how can I make this
public class practicearray
{

public static void main(String [] args)

{
int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];
tableArray [0][0]=20;
tableArray [0][1]=15;
tableArray [0][2]=8;
tableArray [0][3]=45;
tableArray [1][0]=40;
tableArray [1][1]= 10;
tableArray [1][2]=12;
tableArray [1][3]=85;
tableArray [2][0]= 40;
tableArray [2][1]=30;
tableArray [2][2]=78;
tableArray [2][3]=63;


for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
{


for (int colIndex = 0; colIndex < cols; colIndex ++)
{ tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }


}
}
}

outputs like a table???

so how can I make this
public class practicearray
{

public static void main(String [] args)

{
int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];
tableArray [0][0]=20;
tableArray [0][1]=15;
tableArray [0][2]=8;
tableArray [0][3]=45;
tableArray [1][0]=40;
tableArray [1][1]= 10;
tableArray [1][2]=12;
tableArray [1][3]=85;
tableArray [2][0]= 40;
tableArray [2][1]=30;
tableArray [2][2]=78;
tableArray [2][3]=63;


for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
{


for (int colIndex = 0; colIndex < cols; colIndex ++)
{ tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }


}
}
}

outputs like a table???
Was This Post Helpful? 0
  • +
  • -

#11 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

Re: help help help with arrays!

Posted 29 October 2008 - 09:49 PM

so how can I make this
public class practicearray
{

public static void main(String [] args)

{
int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];
tableArray [0][0]=20;
tableArray [0][1]=15;
tableArray [0][2]=8;
tableArray [0][3]=45;
tableArray [1][0]=40;
tableArray [1][1]= 10;
tableArray [1][2]=12;
tableArray [1][3]=85;
tableArray [2][0]= 40;
tableArray [2][1]=30;
tableArray [2][2]=78;
tableArray [2][3]=63;


for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
{


for (int colIndex = 0; colIndex < cols; colIndex ++)
{ tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }


}
}
}

outputs like a table???
Was This Post Helpful? 0
  • +
  • -

#12 isale8888  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 45
  • Joined: 29-October 08

Re: help help help with arrays!

Posted 29 October 2008 - 10:12 PM

so how can I make this
public class practicearray
{

public static void main(String [] args)

{
int rows = 3, cols = 4;
int[][] tableArray = new int[rows][cols];
tableArray [0][0]=20;
tableArray [0][1]=15;
tableArray [0][2]=8;
tableArray [0][3]=45;
tableArray [1][0]=40;
tableArray [1][1]= 10;
tableArray [1][2]=12;
tableArray [1][3]=85;
tableArray [2][0]= 40;
tableArray [2][1]=30;
tableArray [2][2]=78;
tableArray [2][3]=63;


for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
{


for (int colIndex = 0; colIndex < cols; colIndex ++)
{ tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }


}
}
}

outputs like a table???
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help help help with arrays!

Posted 29 October 2008 - 10:19 PM

Please if you want us to cut & paste your code

:code:

it is part of his forum rules

A lot of hardcoding
what was wrong with my solution ?

View Postisale8888, on 29 Oct, 2008 - 09:43 PM, said:

so how can I make this
public class practicearray
{
	
	public static void main(String [] args)
	
	{
		int rows = 3, cols = 4;
		int[][] tableArray = new int[rows][cols];
		tableArray [0][0]=20;
		tableArray [0][1]=15;
		tableArray [0][2]=8;
		tableArray [0][3]=45;
		tableArray [1][0]=40;
		tableArray [1][1]= 10;
		tableArray [1][2]=12;
		tableArray [1][3]=85;
		tableArray [2][0]= 40;
		tableArray [2][1]=30;
		tableArray [2][2]=78;
		tableArray [2][3]=63;
		

		 for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
		 {
		 	
		 	
		 for (int colIndex = 0; colIndex < cols; colIndex ++)
		  {   tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }
		  
	   
		   }
		  }
}

outputs like a table???

so how can I make this
public class practicearray
{
	
	public static void main(String [] args)
	
	{
		int rows = 3, cols = 4;
		int[][] tableArray = new int[rows][cols];
		tableArray [0][0]=20;
		tableArray [0][1]=15;
		tableArray [0][2]=8;
		tableArray [0][3]=45;
		tableArray [1][0]=40;
		tableArray [1][1]= 10;
		tableArray [1][2]=12;
		tableArray [1][3]=85;
		tableArray [2][0]= 40;
		tableArray [2][1]=30;
		tableArray [2][2]=78;
		tableArray [2][3]=63;
		

		 for (int rowIndex = 0; rowIndex < rows; rowIndex ++)
		 {
		 	
		 	
		 for (int colIndex = 0; colIndex < cols; colIndex ++)
		  {   tableArray[rowIndex][colIndex] = rowIndex * colIndex + 1; }
		  
	   
		   }
		  }
}


outputs like a table???


You hoo: :code:
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,299
  • Joined: 20-September 08

Re: help help help with arrays!

Posted 30 October 2008 - 03:44 AM

Please don't keep reposting the same question. You can output the array like this:

				for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
						for (int colIndex = 0; colIndex < cols; colIndex++) {
								System.out.printf("%2d",  tableArray[rowIndex][colIndex]);
								if (colIndex < (cols - 1)) {
									System.out.print(",");
								}
						}
						System.out.println();
				}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1