473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

two dimensional arrays

2
I have an assignment that I'm having problems with and I was wondering if you could help me. What I have to do is to generate a 5x5 two-dimmensional array with 25 two-digit numbers, but I have to make sure there are no duplicates. Then I have to cover 10 randomly picked numbers from the array with "XX", but I have to make sure that none of the 10 covered numbers make 5 in a row either horizontally, vertically or diagonally.

So what I'm having problems with is how to make sure there are no duplicates, and how to make sure that none of the 10 covered numbers make 5 in a row.
This is the code, what I did so far.
Thank you.

Expand|Select|Wrap|Line Numbers
  1. import acm.program.*;
  2. import acm.graphics.*;
  3. import acm.io.*;
  4.  
  5. class LingoBoard1 extends ConsoleProgram
  6. {
  7.  
  8.   void main()
  9.   {
  10.  
  11.   int[][] matrix = createArray();
  12.  
  13.   println("The Lingo board is: ");
  14.   displayArray(matrix);
  15.  
  16.   println("The board with 10 elements covered is: ");
  17.   matrix = coverArray(matrix);
  18.   displayArray(matrix);  
  19.   }
  20.   int[][] createArray ()
  21.   {
  22.   int[][] matrix = new int[5][5];
  23.   boolean isDuplicate = true;
  24.   int row, column;
  25.  
  26.    for (row = 0; row < 5; row++)
  27.     for (column = 0; column < 5; column++)
  28.        while(isDuplicate)
  29.       {
  30.          matrix[row][column] = (int)(Math.random() * (99-10+1)) + 10;
  31.            isDuplicate = checkDuplicate(matrix,row,column);
  32.  
  33.        }
  34.   return matrix;
  35.   }
  36.  
  37.   void displayArray (int[][] matrix)
  38.   {
  39.     for (int row = 0; row < 5; row++)
  40.     {
  41.      for (int column = 0; column < 5; column++)
  42.        {
  43.            print(matrix[row][column] + " ");
  44.        }
  45.       println(); 
  46.     }
  47.   } 
  48.  
  49.   boolean checkDuplicate(int[][] matrix,int i, int j)
  50.   {
  51.     boolean b = false;
  52.  
  53.      for (int row = 0; row < 5; row++)
  54.      for (int column = 0; column < 5; column++)
  55.         if(matrix[row][column] == matrix[i][j])
  56.           b = true;
  57.           else b = false;
  58.  
  59.      return b;
  60.     }                     
  61.  
  62.   int[][] coverArray(int[][] a)
  63.   {
  64.  
  65.   //int[][] a;
  66.   int i,j;
  67.  
  68.   for(i = 0; i < 5; i++)
  69.    {
  70.     j = (int)(Math.random()*5);
  71.     a[i][j] = 'x';
  72.    }
  73.   for(j = 0; j < 5; j++)
  74.    {
  75.     i = (int)(Math.random()*5);
  76.     a[i][j] = 'x';
  77.    }  
  78.   return a;
  79.   }   
  80.   public static void main(String[] args) 
  81.   {
  82.     new LingoBoard1().start();
  83.   }
  84.   public void run() { main(); }
  85. }
Sep 24 '07 #1
3 2141
Nepomuk
3,112 Expert 2GB
Hi estera! Welcome to TSDN!

Do I understand correctly, you have to avoid thing like this
Expand|Select|Wrap|Line Numbers
  1. xx  xx  xx  xx  xx
  2. 21  22  23  24  25
  3. 31  32  33  34  35
  4. 41  42  43  44  45
  5. 51  52  53  54  55
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. xx  12  13  14  15
  2. xx  22  23  24  25
  3. xx  32  33  34  35
  4. xx  42  43  44  45
  5. xx  52  53  54  55
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. xx  12  13  14  15
  2. 21  xx  23  24  25
  3. 31  32  xx  34  35
  4. 41  42  43  xx  45
  5. 51  52  53  54  xx
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. 11  12  13  14  xx
  2. 21  22  23  xx  25
  3. 31  32  xx  34  35
  4. 41  xx  43  44  45
  5. xx  52  53  54  55
  6.  
Correct?

If so, you have to check if it would make a row as soon as you're setting #5 - #10. You could have three (or four) methods:
Expand|Select|Wrap|Line Numbers
  1. private boolean checkHorizontal(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. private boolean checkVertical(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. private boolean checkDiagonal(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
and use each of those to check, if setting xx at the positon (x,y) would create a line.

Greetings,
Nepomuk
Sep 24 '07 #2
estera
2
Hi estera! Welcome to TSDN!

Do I understand correctly, you have to avoid thing like this
Expand|Select|Wrap|Line Numbers
  1. xx  xx  xx  xx  xx
  2. 21  22  23  24  25
  3. 31  32  33  34  35
  4. 41  42  43  44  45
  5. 51  52  53  54  55
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. xx  12  13  14  15
  2. xx  22  23  24  25
  3. xx  32  33  34  35
  4. xx  42  43  44  45
  5. xx  52  53  54  55
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. xx  12  13  14  15
  2. 21  xx  23  24  25
  3. 31  32  xx  34  35
  4. 41  42  43  xx  45
  5. 51  52  53  54  xx
  6.  
or this
Expand|Select|Wrap|Line Numbers
  1. 11  12  13  14  xx
  2. 21  22  23  xx  25
  3. 31  32  xx  34  35
  4. 41  xx  43  44  45
  5. xx  52  53  54  55
  6.  
Correct?

If so, you have to check if it would make a row as soon as you're setting #5 - #10. You could have three (or four) methods:
Expand|Select|Wrap|Line Numbers
  1. private boolean checkHorizontal(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. private boolean checkVertical(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
Expand|Select|Wrap|Line Numbers
  1. private boolean checkDiagonal(int x, int y)
  2. {
  3.    // ...
  4. }
  5.  
and use each of those to check, if setting xx at the positon (x,y) would create a line.

Greetings,
Nepomuk
Thanks. One more question. How would I check to see if the setting XX creates a line? Can you give me an example for one of the methods?
Sep 24 '07 #3
Nepomuk
3,112 Expert 2GB
Thanks. One more question. How would I check to see if the setting XX creates a line? Can you give me an example for one of the methods?
I won't write any of the methods for you, but here's how it would work:
Expand|Select|Wrap|Line Numbers
  1. private boolean checkHorizontal(int x, int y, int[][] matrix)
  2. // you can leave out the last part, if your matrix is defined globally
  3. {
  4.    // you have the y-value of the number to be set, so check if there are 5 - 1 = 4 Numbers in that line covered already
  5.    int covered = 0;
  6.    // your code here
  7.    return ...; // What do you return?
  8. }
  9.  
Greetings,
Nepomuk
Sep 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Parrot | last post by:
I am trying to program a function to return a 2 dimensional array, but it's not working. I reduced the return value to 1 dimension and tested that to make sure that the problem wasn't elsewhere. ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
9
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
5
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.