# need a java code for this question!

• 05-07-2008, 04:59 PM
rose
need a java code for this question!
Problem (a) – Football Scores - Two Dimensional Arrays

Declare a multi dimensional array like the one shown to represent the football game scores of 5 football games held between ITB and some other college team. Two columns represent the respective scores for each game:

ITB Other

You are required to set up the scores as random numbers between 1 and 5 for each team in each game. Secondly you must output the scores in the manner shown and determine:
(i) How many games did ITB win
(ii) How many goals were scored in each game

Game 1:
Game 2:
Game 3:
Game 4:
Game 5:

Sample Output:

ITB Other Goals Scored This Game

Game 1: 2 1 3
Game 2: 3 2 5
Game 3: 1 2 3
Game 4: 0 1 1
Game 5: 1 1 2

Problem (b)
Declare a two dimensional array that has 6 rows and 4 columns. Assign random numbers (integers between 1 and 10) to all elements. You are required to find the following:
The total of all elements
The totals of each row
The totals of each column
• 05-07-2008, 06:37 PM
Azzia
It would probably be best if you attempted the assignment rather than asking someone to do it for you.
• 05-07-2008, 06:55 PM
rose
i have wriiten the code for that question...can u plz check if its right or not...

class WS3AQ1{
public static void main(String[]args){
int myArray[][] = new int[5][2];

fillArray(myArray);
// Array is full and changed!"!!! - ref parameter!!!

processCols(myArray);

}

////////////////////////////////////
// Procedure to fill the array
////////////////////////////////////
static void fillArray(int a[][]){ // reference parameter = a[][]

int total =0;
int wins = 0;
// Heading outside of any for loop - once off print only
System.out.print("\tITB\tOther\t\tTotal");

for(int r=0; r<5; r++) // for r = 0 to 4
{
total = 0; // RESET for every column!!
System.out.println();
System.out.print("Game " + (r+1) + "\t: ");
for(int c=0; c<2;c++)
{
a[r][c] = (int)(Math.random()*10000)%5+0;
total = total + a[r][c];
System.out.print(a[r][c] + "\t");
}
if (a[r][0] > a[r][1]) wins++;
System.out.print("\t" + total);
}

System.out.print("\n\n\n");

System.out.println("\nITB wins " + wins + " games ");

}
///////////////////////////////////////////
// PROCESS COL BY COL!!!!!
static void processCols(int a[][]){
int total;

System.out.print("\n\n\n\n");

for(int c=0; c<2; c++)
{
total =0; // reset for each col

for(int r=0; r<5; r++)
{
total = total + a[r][c];
}

System.out.print("\t" +total);

}
System.out.print("\n\n\n\n");

}

}
• 05-07-2008, 08:34 PM
hardwired
Looks okay so far...