Results 1 to 19 of 19
 03272012, 02:09 AM #1Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
give a number x and y coordinates
hi, i have this code and i was wondering how can i give a random number from a matrix, x an y coordinates. for example say i have a matrix with dimensions of 8 by 8 containing numbers from 1 to 64. and i took the number 31, how can i give it x and y coordinates, can anyone help me with this ?
 03272012, 02:20 AM #2
Re: give a number x and y coordinates
Is your question how to convert a number in the range 063 into the row and column values to index a 2D array?
To go the other way, given row and column: row*row_length+column
Use some algebra on that equation to get the other one. Look at using % and /If you don't understand my response, don't ignore it, ask a question.
 03272012, 01:05 PM #3Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
hi sorry about the delay it was late last night when i posted and just checked in the morning to see if i had any replies,
yeah so my question to make it clearer. just say i have colums and rows 4 * 4 and you populate it with the numbers 116 .
1***
****
****
**16
in the first row and colum is the number 1 and the last row and colum is the number 16. so just say the x y coordinates for the number 1 are 0,0 . and the the x y coordinate for the number 16 are 4, 4. i was just wondering if i take a random number how could i automatically give it x y coordinates using java
 03272012, 01:15 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,519
 Blog Entries
 7
 Rep Power
 20
Re: give a number x and y coordinates
Are the numbers in that matrix in proper ascending order?
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03272012, 01:18 PM #5Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
 03272012, 01:24 PM #6
Re: give a number x and y coordinates
Using the formula I suggested you compute, you'd subtract 1 from the numbers to make them 0 based.
If you don't understand my response, don't ignore it, ask a question.
 03272012, 01:26 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,519
 Blog Entries
 7
 Rep Power
 20
Re: give a number x and y coordinates
The problem is easy then (aamof, you don't need a matrix at all). For any number i in an n*n matrix, that number is stored in row #(i1)/n and column #(i1)%n. That's all there is to it.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03272012, 01:36 PM #8Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
great guys thanks for help i appreciate, sorry if question was a bit dumb i just new to java
regards
Derek
 03312012, 01:56 AM #9Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
 03312012, 01:59 AM #10
Re: give a number x and y coordinates
Your y axis is the opposite direction of how it is used in java programming (java is top down). You will have to convert the java coordinates to your coordinates.
If you don't understand my response, don't ignore it, ask a question.
 03312012, 02:05 AM #11Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
my code to assign x y coordinates to positions on the grid is not working properly
any ideas why this would be ?
x = (number1)%9;
y = (number1)/9;
 03312012, 02:08 AM #12
Re: give a number x and y coordinates
Can you show some examples?
Have several values of number and show the x,y values that are generated by your code.
Also show how the squares are numbered. Were are square 1 and square 2If you don't understand my response, don't ignore it, ask a question.
 03312012, 02:15 AM #13Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
my code is below , basically i want to take 2 numbers from the user to create a grid, then take a random number from this grid and give it corresponding x y coordinates.
import java.util.Scanner;
import java.util.Random;
public class baby {
public static void main(String args[])
{
System.out.println("enter first number for room length: ");
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
System.out.println("enter second number for room length: ");
int num2 = scan.nextInt();
int room_length = num1 * num2;
System.out.println("The room length is :"+num1 +" x "+num2+" = "+room_length);
Random furniture = new Random();
int table = furniture.nextInt(room_length);
int chair = furniture.nextInt(room_length);
System.out.println("table random number is: "+table);
System.out.println("chair random number is: "+chair);
int x_coordinate = (table 1)%num1;
int y_coordinate = (table 1)/num2;
System.out.println("x coordinate is: "+ x_coordinate +" y coordinate is :" +y_coordinate);
}
}
 03312012, 02:21 AM #14
Re: give a number x and y coordinates
Please execute the program and copy and post here the output showing the value of the number and the values of x and y for several values. Add comments to the output that show what the x, and y values should be.
How are the squares in the grid numbered? Where is square # 1 and square #2?If you don't understand my response, don't ignore it, ask a question.
 03312012, 02:33 AM #15Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
i want grid to be populated as so
1 2 3 4
5 6 7 8
9........
so 1 xy coordinates to be 0,3
2 xy coordinates to 1,3
3 xy coordinates to be 2,3
and so on
////////////////////////
examples of program run
enter first number for room length:
4
enter second number for room length:
4
The room length is :4 x 4 = 16
table random number is: 4
x coordinate is: 3 y coordinate is :0
///////////////////////////////////////////
enter first number for room length:
4
enter second number for room length:
4
The room length is :4 x 4 = 16
table random number is: 9
x coordinate is: 0 y coordinate is :2
///////////////////////////////////////
enter first number for room length:
4
enter second number for room length:
4
The room length is :4 x 4 = 16
table random number is: 7
x coordinate is: 2 y coordinate is :1
 03312012, 02:47 AM #16
Re: give a number x and y coordinates
You want> square 1 (top left corner) xy coordinates to be 0,3
That is not what you said in post#3???
In your post you needed to add comments to the output to show what the values of x,y should be.
7 gives x coordinate: 2 y coordinate: 1 >>> What are the correct values?
Take a piece od paper and draw a grid with the squares. In each square write the square number and the x,y values it should have. Then look at the drawing and find the formulas that will generate the desired values from the square number.
Post the square contents for a couple of squares. Say the top left and the bottom right squares.If you don't understand my response, don't ignore it, ask a question.
 03312012, 03:02 AM #17Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 = 0,3
2= 1,3
3 = 2,3
4 = 3,3
5 = 0,2
6 = 1,2
7 = 2,2
8 = 3,2
9 = 0,1
10 = 1,1
11 = 2,1
12 = 3,1
13 = 0,0
14 = 1,0
15= 2,0
16 = 3,0
 03312012, 03:05 AM #18Member
 Join Date
 Mar 2012
 Posts
 10
 Rep Power
 0
Re: give a number x and y coordinates
thats all i can post for tonight, i'll reply to any futher post tomorrow
regards
derek
 03312012, 03:10 AM #19
Re: give a number x and y coordinates
Now you need to find the formulas to compute those values.
Your x,y values appear to be in column, row order which is the reverse of what I have seen used before.
Normally x is the row number and y is the column number. Are you reversing their common usage also?
You want> x is the column and y is the row?If you don't understand my response, don't ignore it, ask a question.
Similar Threads

Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
By LogicalOutlier in forum New To JavaReplies: 8Last Post: 01212012, 01:14 AM 
Dice help. posting the number of times a number is rolled.
By cookiejarvus in forum New To JavaReplies: 13Last Post: 12042011, 11:08 PM 
Code to convert binary number to base 10 number
By j@y in forum New To JavaReplies: 1Last Post: 10282011, 08:18 AM 
Coordinates
By Witik in forum New To JavaReplies: 3Last Post: 09062010, 07:05 AM 
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02052010, 10:42 PM
Bookmarks