Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2 D arrays problem

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
     
    /***********************************************************************/
    /*                                                                     */
    /*	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
    Last edited by Pulse_Irl; March 4th, 2010 at 11:20 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?
    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
    Last edited by Pulse_Irl; March 5th, 2010 at 05:04 PM.

Similar Threads

  1. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  2. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM
  3. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  4. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM
  5. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 04:56 PM