Jump to content

Stuck on something "simple"

Go to solution Solved by Mr_KoKa,

Then remember your position, you have your width which is cols and height which is rows

You start from x = 0 and y = 0;

 

You get value from user and check if x < width and y < height if it is you save the value you got from user to cell x,y and then increase x

now x = 1 and again, you do the check, if x < width

eventually x wont be < width the you zero x and increase y (you do this after you chec x < width and before checking y < height) and now you check y < height, if it is then yous ave value in x, y if it is not then it means you're out of array space.

I'm on 3rd semester at my university and up until now we were just programming on java using eclipse or netbeans, but now all of a sudden a teacher told us that if we want a 100% on her class we have to make an app for android about her class, the thing is, while learning how to use android studio I wanted to try to add values to a matrix (an array of 2 dimensions, idk how you guys call them in your countries, that's why I clarified), normally this is very simple but I don't know how to make an edit text box add or ask for another value until the matrix is full. the most I have gotten to work is a simple addition and subtraction using edit text boxes :(

i would really appreciate some help.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not a android guy, but if you;re able to create an input box and button that does work somehow, then what is to do is just logic how to insert next value at right index, is your matrix constant size? Or it grows dynamically? (How do you declare your matrix?).

 

If it is constant then you would need to remember a row and col starting both at 0, and after you insert value at matrix[col][row] you need to increase col, and then check if col is < then cols count, if it is not you col = 0 and increase row, if row is not < then rows count you are finished as all cells are filled.

 

It would be similar if you grow your arrays at runtime, you would need to check if you don't grow your array to much, if it has certain length just go to another col/row.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Mr_KoKa said:

I'm not a android guy, but if you;re able to create an input box and button that does work somehow, then what is to do is just logic how to insert next value at right index, is your matrix constant size? Or it grows dynamically? (How do you declare your matrix?).

 

If it is constant then you would need to remember a row and col starting both at 0, and after you insert value at matrix[col][row] you need to increase col, and then check if col is < then cols count, if it is not you col = 0 and increase row, if row is not < then rows count you are finished as all cells are filled.

 

It would be similar if you grow your arrays at runtime, you would need to check if you don't grow your array to much, if it has certain length just go to another col/row.

the user is supposed to set the size of the matrix but the thing is, i want to get all the input from one edit text box and no one in my team knows how to xD

Link to comment
Share on other sites

Link to post
Share on other sites

You mean all data at once, like delimited with for example by comma?

How does user specify size, does it uses some other two inputs or size of matrix is based on data input or maybe size is part of data input as one/two first numbers?

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, angelmartin11 said:

the user is supposed to set the size of the matrix but the thing is, i want to get all the input from one edit text box and no one in my team knows how to xD

Many programming competition problems use text files for input. When they require a 2d array as the input, some of them use a format where the first line is the number of rows and columns, and the remaining lines are the values in the matrix. Here are two examples. The first of a 3x3 matrix, and the second of a 5x5 matrix.

3 3
1 1 1
0 0 0
4 4 4
5 5
10 -2 3 4 199
7 2 -32 5 -12
32 32 -4 8 -499
0 3 1 4 2
1092 3 5 34 -3

 

You could require a user enter the information in this kind of format in a multi-line edit text box.

Link to comment
Share on other sites

Link to post
Share on other sites

Two clarify what the others have said, If you split the string from the text box line by line  How
You now have X arrays of lines, split each array again, and you will have access to each value.

 

Ie :

String : 	
	"1 2 3 4
	 5 6 7 8
	 9 8 7 6"

Split once :
Array = 
	[1 2 3 4,
	 5 6 7 8,
	 9 8 7 6]

Split each location again
	
	Array1 = [1,2,3,4]
	Array2 = ... etc

 

You will need a nested loop to this. But you can figure that on your own (You wont learn anything if we do it all for you)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 21/10/2016 at 7:58 AM, Mr_KoKa said:

You mean all data at once, like delimited with for example by comma?

How does user specify size, does it uses some other two inputs or size of matrix is based on data input or maybe size is part of data input as one/two first numbers?

Sorry i forgot i made this xD...

no, i mean the user does: "inserts value"+enter or click button (add), the app clears the previous text and asks again for the next value.

the user specifies size in a previous activity with two edit text boxes one for rows and another one for columns and just clicks a button to create the matrix.

Link to comment
Share on other sites

Link to post
Share on other sites

On 23/10/2016 at 4:29 PM, Isvan said:

Two clarify what the others have said, If you split the string from the text box line by line  How
You now have X arrays of lines, split each array again, and you will have access to each value.

 

Ie :


String : 	
	"1 2 3 4
	 5 6 7 8
	 9 8 7 6"

Split once :
Array = 
	[1 2 3 4,
	 5 6 7 8,
	 9 8 7 6]

Split each location again
	
	Array1 = [1,2,3,4]
	Array2 = ... etc

 

You will need a nested loop to this. But you can figure that on your own (You wont learn anything if we do it all for you)

Thats not what i wanted to do.

Link to comment
Share on other sites

Link to post
Share on other sites

Then remember your position, you have your width which is cols and height which is rows

You start from x = 0 and y = 0;

 

You get value from user and check if x < width and y < height if it is you save the value you got from user to cell x,y and then increase x

now x = 1 and again, you do the check, if x < width

eventually x wont be < width the you zero x and increase y (you do this after you chec x < width and before checking y < height) and now you check y < height, if it is then yous ave value in x, y if it is not then it means you're out of array space.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×