Click to See Complete Forum and Search --> : Dimensional Matrix to output Array


m1pui2
December 17th, 2003, 10:18 AM
Hi,
I have a question on a mock exam paper that i need to know the answer so I can learn it for an exam I am doing in a few weeks.

This is it in a shorter format below; for the actual exam I will only need to put in bare bits of code not the whole thing but if possible could someone implement it in as much code as possible so it will make me understand it better.

Whats Required:

Sequential program that receives as input a single Y x Z dimensional matrix (A) with a large amount of unsorted integer numbers.

The program then has to produce an N-dimensional array B containing the sum of the numbers in each A column(for exmaple Bi = Ai1 + Ai2 + etc + AiN
(i = 1 .....M)


I just can't get my head round it, even though it is probably a pretty simple program :( so could someone at least do part of the program for me, or all of it if you find it easy
Thanks for any help.

cjard
December 17th, 2003, 11:07 AM
its the terminology used. if i told you:

"write a program that adds up all the numbers in an array"

you could probably do it

so i tell you:

"write a program that adds up all the numbers in an array of arrays, of numbers"

-

you take your first program:


public int summateArray(int[] toAddUp){
int result=0;
for(int idx=0; idx<toAddUp.length; idx++){
result += toAddUp[idx];
}
return result;
}


that adds an array up, yur?

now we make it capable of adding up an array of arrays. there are 2ways of doing this (left right, top to bottom) and (up down, left to right) that will give very different answers:

{1, 2, 3}
{4, 5, 6}
{7, 8, 9}

this program summates 1+2+3, then 4+5+6, etc
it does not summate 1+4+7, then 2+5+8 ...


public int[] summateArray(int[][] toAddUp){
//make as many spaces for results as there are arrays to add up
int results[]=new int[toAddUp.length];
//int arrays in java are cleared to 0 but you could do this anyway:
for(int idx=0;idx<results.length; idx++){
results[idx] = 0;
}

//make a loop that accesses the outer array
//everything in bold is an addition to the previous program
for(int outerIndex = 0; outerIndex < toAddUp.length; outerIndex++){
//this loop adds up the inner array
for(int idx=0; idx<toAddUp[outerIndex].length; idx++){
results[outerIndex] += toAddUp[outerIndex][idx];
}
}
return results;
}


now, do you see how we address the outer array (the int[OUTER ONE][]) to get access to the inner one (the int[])

we basically say to java "i want access to the inner array that is stored at outer-array-position referenced by outerIndex"
-- thats the toAddUp[outerIndex].length

you see, toAddUp[some_number] is essentially the name of an array. in the following example, "myArray" is an array - easy to understand:

int[] myArray = new int[2];


these are ALSO individual arrays, only they are within an array themselves (an array of arrays):

int[][] myArrays = new int[2][2];
myArrays[0]
myArrays[1]

(2 arrays, of 2 elements each)

so the outerIndex, when put together with the name toAddUp, basically gets us access to an array, and the program then adds that up. there is no conceptual difference between the longer code i wrote above, and this:


public int[] summateArray(int[][] toAddUp){
//make as many spaces for results as there are arrays to add up
int results[]=new int[toAddUp.length];
//int arrays in java are cleared to 0 but you could do this anyway:
for(int idx=0;idx<results.length; idx++){
results[idx] = 0;
}

int[] tempArray;
int tempResult;

//make a loop that accesses the outer array
//everything in bold is an addition to the longer program
for(int outerIndex = 0; outerIndex < toAddUp.length; outerIndex++){
tempArray = toAddUp[outerIndex];
tempResult=0;

//this loop adds up the temp array
for(int idx=0; idx<tempArray.length; idx++){
tempResult += tempArray[idx];
}

//store our tempResult in the results array
results[outerIndex] = tempResult;
}
return results;
}


can you see why? i added a couple of variables in there to demonstrate how these 2 arrays:
[i]myArrays[some_number]
[i]tempArray

can refer to the same thing.. dont be put off by the [some_number] part of the name above confusing you into thinking it is something special..
just treat it as a normal 1 dimensional array, where the name is the italic bit, and the index is the bold bit:
[i] NAME[some_number][INDEX]