# 2 D arrays problem

• March 4th, 2010, 10:15 AM
Pulse_Irl
2 D arrays problem
Hi ive just started semester two and my new programming teacher is crap, can any1 please help me with this here's the question.

Part 1.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:
Part.2You are required to set up yhe scores as random numbers between 1 and 5 for each team in each game.Secondely you must output the results in the manner showen and determine

i.how many games itb win
ii.and how many goals were scored in each game

SAMPLE OUTPUT:
Itb Other Score
Week1 : 3 4 7
Week 2: 5 1 6
Week 3: 2 2 4
Week4: 3 1 4
Week5 0 1 1

^^ this output is all squashed up on the thread but itb, other and score should all be to the right of the weeks in colum form,pretty much like a table
Code :

```  /***********************************************************************/ /* */ /* Name: Craig */ /* Date: 1st Mar 2010 */ /* Program Name: WS4b */ /* */ /***********************************************************************/ class WS4bQ1 { public static void main(String[] args) { int [] [] gameScore = new int [5] [2];   for(int week=1; week<=5; week++) { for(int itb=1; itb < gameScore[0].length; itb++) { gameScore[week-1] [itb-1] = (int)(Math.random()*10000000)%5+1; }//end inner loop }//end outer for loop for(int week = 1; week < gameScore.length; week++) { System.out.println("\n\n\n"); } for(int itb = 1; itb < gameScore[1].length; itb++) { System.out.println("\n " + gameScore[1][1]); } } }```

I know my code compiles but it isn't doing what i want it too, ive spent the last two days trying to fix this with no success please can some 1 help me in going mad looking at this code x-(
• March 4th, 2010, 10:55 AM
helloworld922
Re: 2 D arrays problem
There's no need to multiply Math.random() by such a large amount if you're just going to modulus it. Simply multiply it by 5, add one and cast to an it.

Also, in the loop where you print out the itb scores, you're repeatedly printing out gameScore[1][1], instead of the scores for that week. You also need to advance the weeks in another for loop (similar to how you have your first nested for-loop) to print out the scores as the week progresses.
• March 5th, 2010, 03:49 PM
Pulse_Irl
Re: 2 D arrays problem
Cheers world,

Im still having trouble with this tho, I cant get the week to start at week one without losing the values in row zero of the array, any ideas?
Code :

```class WS4bQ2 { public static void main(String[] args) { int gameScore [] [] = new int [5] [2]; //declaring and setting up array int total=0,totalcol1 = 0, totalcol2 = 0,total3= 0, wins = 0; for(int week=0; week<5; week++) { for(int col=0; col<2; col++) { gameScore[week] [col] = (int)(Math.random()*100%5+1); //random number generator } }//end outer loop System.out.println(" ITB Other Total Goals");//output table header for(int week=0;week<gameScore.length;week++) { System.out.print("Week " + week +": "); for(int col=0; col<gameScore[week].length; col++)//loop to output results and keep track of total for each week { System.out.print(gameScore[week] [col] + " "); total = total + gameScore[week][col]; } System.out.print(" " + total); System.out.println(); total=0;//reset total to 0 } // close outer loop for(int week=0;week<gameScore.length; week++)// loop to check how many games ITB won { for(int col=1; col<gameScore[week].length; col++) { if(gameScore[week][0] > gameScore[week][col]) { wins++; } } //close out loop }// close inner loop for(int week=0;week<gameScore.length;week++)//loop to count total goals scored by ITB { totalcol1 = totalcol1 + gameScore[week][0]; }// close loop for(int week=0;week<gameScore.length;week++)//loop to count goals scored by other teams { totalcol2 = totalcol2 + gameScore[week][1];//total goals in 5 weeks }// clos loop total3 = totalcol1 + totalcol2; System.out.println("ITB wins: " + wins); System.out.println("ITB goals: " + totalcol1); System.out.println("Other goals: " + totalcol2); System.out.println("Total goals: " + total3); }//end main }//end class```