double[arrays of doubles][doubles] matrix;
but it doesn't really matter which way you do it anyways (except for possible performance losses caused by paging).
>>1 how do i tell which [] has the rows and which has the columns?
In a real language:
1) Clear the array if necessary
2) Poke [0][1] of the array with a value
3) Look at the address of [0][0] +1 (or plus however many bytes wide a single element is)
4) Is it still clear or is it the value you put in [0][1] ???
Your language may not even allow you to do something like this but I know it would work in say, C, or most any language that lets you use pointers in any way.
memset(myArray, 0, 2*2); /* set all elements to 0 */
*(&myArray[0][0] +1) = 1;
if (myArray[0][1])
printf("Row-major order");
else
printf("Column-major order");
}
The result in C is that the second dimension is that [0][1] = 1, indicating that you begin counting with the second dimension.
I have no idea if you can do anything like this in Java. As far as I know, pointers are forbidden or something like that.
Name:
Anonymous2009-10-27 23:27
you look like you need a hand from someone who isn't a progtard.
I think you're referring to the mathematical matrix construct?
let's say you had these two:
| 9 8 7 |
| 6 5 4 |
so, if you've got a matrix that's 2 rows of three, you can still handle it in Java with one primitive construct; it's called a multidimensional array (or, an array of arrays). if double[] is an array of doubles, then know now that double[][] is an array of double arrays.
from my example:
double[][] matrixA =
{{9,8,7},
{6,5,4}};
still two rows, still three columns per row; only now we call them one array of two double-type arrays.
you can call values the same way you did for single-dimension arrays. look back at matrixA; the formatting I used is legal in the compiler and might help you to organize your thoughts. for matrixA[i][j], i= the index of the single-dimension array you want to call, and j= the index of the element in that array.
think of it this way: if you have a single-dimension array, you would call the fifth element by array[5]. I haven't told you what type this array is; suppose its type is, in fact, also array. array[5] will return another array, and you call this new array's elements with the same syntax appended to the original call: the 2nd element in the fifth array is array[4][1]. and that syntax expands forever: there is no limit to the number of array dimensions other than your own processor speed.
So, doing your homework for you now, using the example above.
as someone learning, you might benefit from defining a Matrix object class, but I suspect you haven't been introduced to multiple-class structuring, and I think it would be a lot of hassle. so we'll define a matrix as a double[][] (a double-type two-dimensional array). we'll use the one I made up at the top of this post.
I imagine you have exposure to some loops by now; to multiply a whole matrix by a scalar, you multiply every element by the scalar. an idiom to loop through every element in a 2d array is
public static double[][] multiplyMatrixByScalar(double[][] arr, double scalar){
for(int i=0; i<arr.length; i++) //for every index in the top array object
{ for(int j=0; j<arr[i].length; j++) //for every valid index j in the ith array
{ arr[i][j] *= scalar;} //multiply the jth element of the ith array by scalar.
}
}
now you tell me if that helped.
Name:
Anonymous2009-10-27 23:28
>>7
Edit: almost on the top damn line, that's one matrix, not two. sorry.
Name:
Anonymous2009-10-27 23:34
>>7
also edit: the fifth element is array[4].
also edit: add the return statement to multiplyMatrixByScalar. or make it return type void in the method declaration.
damn my inability to proofread. I also haven't slept in a few days.
>>10
You'll be lucky, and more to the point why did you help him?
Name:
Anonymous2009-10-27 23:49
>>7
I figured it out and submitted my own version already before seeing your reply, but thanks anyway, I will look at this later to enforce my understanding of matrices and and multi dimensional arrays even though the assignment is over..
>>15
It's the period after midterms where they have time to dick around because tests are over and a bit of ego because they got decent grades on easy tests.