user12065626
user12065626

Reputation:

Using a 2D array to display data that is populated by a method

I am trying to use a 2D array to store voting data, and use a one dimensional array to store names of candidates, names of subdivisions, and hold the total amount of votes for candidates and subs. From the main method i need initialize these arrays using initialization lists: 2D array with all the voting data (but not the totals), candidate names, subdivision names then calculate the total votes for each candidate and subdivision then print out the results

I have began to create the methods needed in order to initialize and fill the arrays, but i am stuck on what i need to do in order to continue

{
   private static int[][] Data;
   private static String[] names;
   private static String[] SubDivs;
   private static int[] totalvoteCand;
   private static int[] totalvoteSub;


   // method to initialize data array
   public static void InitData()
   {  
      int[][] tempData = {{600, 800, 800, 800},
         {700, 700, 700, 900},
         {800, 700, 800, 700},
         {400, 450, 300, 1300},
      {900, 900, 900, 1000}};
      Data = tempData;

      String[] tempNames = { {Audrey, Brian, Elizabeth, Peter, Zachary} };
      tempNames = names;

      String[] tempSub = { {Aberdeen, Brock, Sahali, Valleyview} };
      tempSub = SubDivs;
   }// end of init


   //*************************************************************//
   public static void calcCandVotes() // calculates candidate votes
   {
      totalvoteCand = new int[data[0].length];
      int tempTotal;

      for(int i = 0; i<data.length;i++) {
         tempTotal = 0;
         for(x = 0; x<data[0].length;x++){
            tempTotal += tempTotal + data[i][x];
         }
         totalVoteCand[i] = tempTotal;
      }
   }//end of calc cand

   //*************************************************************//
   public static void calcSubVotes()  // calculates subdivision votes
   {
      totalvoteSub = new int[data[0].length];
      int tempTotal;

      for(int i = 0; i<data[0].length; i++) {
         tempTotal = 0;
         for(x = 0;  x<data.length; x++){
            tempTotal += tempTotal + data[x][i];
         }
         totalvoteSub[x] = tempTotal;
      }
   }// end of calc sub ******************************************//

   public static void printArray() // print method, hopefully.
   {
      for(int i = 0; i<data.length;i++)
      {
         for (int x=0; x<data[0].length;x++)
            System.out.print(Data[i][x] + "\t");
         System.out.println();
      }
   }
   //***************************end of print array****************************************

Upvotes: 0

Views: 159

Answers (1)

Andreas
Andreas

Reputation: 159086

Do you mean you want to print like this?

int[][] votes = {{600, 800, 800,  800},
                 {700, 700, 700,  900},
                 {800, 700, 800,  700},
                 {400, 450, 300, 1300},
                 {900, 900, 900, 1000}};
String[] names = {"Audrey", "Brian", "Elizabeth", "Peter", "Zachary"};
String[] subs = {"Aberdeen", "Brock", "Sahali", "Valleyview"};
System.out.printf("%-10s ", "Candidate");
for (int col = 0; col < subs.length; col++) {
    System.out.printf("%10s ", subs[col]);
}
System.out.printf("%10s%n", "Total");

for (int row = 0; row < names.length; row++) {
    int total = 0;
    System.out.printf("%-10s ", names[row]);
    for (int col = 0; col < subs.length; col++) {
        total += votes[row][col];
        System.out.printf("%10d ", votes[row][col]);
    }
    System.out.printf("%10d%n", total);
}

int grandTotal = 0;
System.out.printf("%-10s ", "Total");
for (int col = 0; col < subs.length; col++) {
    int total = 0;
    for (int row = 0; row < names.length; row++) {
        total += votes[row][col];
    }
    grandTotal += total;
    System.out.printf("%10d ", total);
}
System.out.printf("%10d%n", grandTotal);

Output

Candidate    Aberdeen      Brock     Sahali Valleyview      Total
Audrey            600        800        800        800       3000
Brian             700        700        700        900       3000
Elizabeth         800        700        800        700       3000
Peter             400        450        300       1300       2450
Zachary           900        900        900       1000       3700
Total            3400       3550       3500       4700      15150

Upvotes: 1

Related Questions