• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

How to store the results of an array that is updated by a random number generator

I'm looking to store the results (the totals that are put in the rows and columns) of a multidimensional array, and then be able to display those results when the user has finished updating the array with the random number generator. The array is 4X20. I think the output would have to be stored (appended to a variable) by a while or a for loop. I would appreciate any thoughts on what the algorithm to do this might look like. Can post my existing program code if needed.
0
computerese
Asked:
computerese
  • 3
  • 2
1 Solution
 
CEHJCommented:
for(int i = 0;i < matrix.length;i++) {
      System.out.println(Arrays.toString(matrix[i]));
 
}
0
 
Tommy BraasCommented:
You need one loop per dimension of your matrix. In your case two.

In each loop you would accumulate the values for that dimension, and store the result in for instance a 1-dimensional array.
0
 
computereseAuthor Commented:
@CEHJ
@orangehead911

Thanks for your fast replies. I will work on incorporating your suggestions & report the results or problems. I'm happy with how my "sports fantasy" program is progressing. If the above suggestions work OK, I have some code already to get an average of the cells, a median and generate a histogram.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
The code i posted requires >= 1.5. Otherwise, you'll need to do a bit of 'manual' work
0
 
computereseAuthor Commented:
@CEHJ
Could you elaborate on your comment, "The code i posted requires >=1.5?" I am not sure what you mean.

In the meantime, I've added your suggestions by creating a one dimensional array for the matrix method. I have to go back to work now from my lunch hour but I will check here soon and I think I have one or two more questions I might post as new questions for points.

Once again, thanks to all at EE for the continued and professional suggestions.

-----------------------------------PROGRAM CODE-----------------------------------------------

import java.lang.Math; //use to avoid generating negative numbers
import java.util.*;
import java.io.*;

// classname
public class FootballScoresArray6
{
     public static void main(String[] args) throws IOException
     {
     //hard code constants and declare variables
     int userInputGameNumber = 0; // number of the game  user enters into keyboard
     int QUARTERS_4 = 5;  //4 quarters per game
     int GAMES_20 = 20;  //20 games per season
     int oneGameQuartersTotal = 0;//Total=1 game 4 quarter results
     int allGameQuartersFinalTotal = 0;//Final Totals=20 games results
     double average = 0; // computes average of the scores
     String Totals;
     String Games;
     String Final_Totals;
     //New variable
     int[] sums = new int[QUARTERS_4 ];
     for(int i = 0; i < sums.length; i++)
     {
          sums[i] = 0;
     }
        int[] matrix = new int[GAMES_20];
        for(int i = 0; i < matrix.length; i++)
        {
                    matrix[i] = 0;
        }

     //parameterless constructor creates instance of random class
     Random generator = new Random();

     //user inputs game number
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Enter the number of the game you want to update:  ");
     userInputGameNumber = keyboard.nextInt();
     System.out.println("The game that will be updated is: " + userInputGameNumber);
     System.out.println();
     if (userInputGameNumber > 0) //start loop
     {
          // create array header
                                                   
                              /**************************************************************/
     System.out.println("     \tC O L L E G E  F O O T B A L L  T E A M  G A M E  S C O R E S");                        
                              /**************************************************************/
     System.out.println();
     System.out.println("_____________________________________________________________________________________ ");
     System.out.println("    Games                                            Quarters                                   Subtotals");
     System.out.println("_____________________1______________2______________3______________4__________________ ");
     System.out.println();

          //update array with user input
     int [][] scores = new int[GAMES_20][QUARTERS_4];
     for(int g = 0; g < scores.length; g++)
     {
       //moved outside of loop
       System.out.print("      \t" +((g + 1) +                      "            \t"));  
       //get a valid integer for random number generator
        scores[userInputGameNumber - 1][0]= Math.abs (generator.nextInt() % 33);
        scores[userInputGameNumber - 1][1]= Math.abs (generator.nextInt() % 33);
        scores[userInputGameNumber - 1][2]= Math.abs (generator.nextInt() % 33);
        scores[userInputGameNumber - 1][3]= Math.abs (generator.nextInt() % 33);

        //To sum a row
       scores[userInputGameNumber - 1][4] = scores[userInputGameNumber - 1][0]
       + scores[userInputGameNumber - 1][1] + scores[userInputGameNumber - 1][2]
       + scores[userInputGameNumber - 1][3];
      for(int q = 0; q < scores[g].length; q++)
      {
       //Removed unnecesary loop

       //To sum the columns:
       sums[q] += scores[g][q];

       //Moved to after the values are set
       System.out.print("      \t" + (scores[g][q] +                       "            \t"));  

       } //don't delete
       //move outside of loop
       // create array footer
       System.out.println();
     } //don't delete
     } //don't delete

     System.out.println("_____________________________________________________________________________________ ");
     System.out.print("TOTALS");
      //To display totals:
       for(int i=0;i<sums.length; i++)
      {
        System.out.print("                        \t"            +                              String.valueOf(sums[i]));
      }
     
     System.out.println("Would you like to update another game?");
     // put some code here for the user's reply (if no or if yes)
         
     //To get Final Totals
     for(int i = 0; i < matrix.length; i++)
      {
     System.out.println(Arrays.toString(matrix[i]));
              }
     
     System.out.println();
     System.out.println("_____________________________________________________________________________________ ");
     System.out.println();

          }
     }
0
 
CEHJCommented:
>>Could you elaborate on your comment, "The code i posted requires >=1.5?" I am not sure what you mean.

Requires Java version 1.5 or above. If it compiled, you've got it
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now