Array iteration question  
Author Message
Kris M





PostPosted: 2005-3-18 10:39:00 Top

java-programmer, Array iteration question /*Can i calculate the page totals, column totals, and rowtotals in one pass
through the array instead of the three passes i have coded? I have already
added an outer for loop to combine the three seperate iterations into one
but it still loops through the array multiple times and i would like to have
it loop through only once*/

import java.io.*;
import java.util.*;
import java.text.NumberFormat;

class P3
{
static PrintWriter screen = new PrintWriter (System.out,true);
public static void main (String [] args) throws IOException
{
final int COLLEGES = 4;
final int GENDERS = 2;
final int LANGS = 4;
int langAccum, genderAccum, collegeAccum, totalAccum = 0;
float percentage;
StringTokenizer data;
File file = new File ("p3input.txt");
FileReader inputFile = new FileReader (file);
BufferedReader inFile = new BufferedReader(inputFile);
String [] languageNames = {"C++" , "Cobol" , "Java" , "VB"};
String [] collegeNames = {"CCSU", "ECSU", "SCSU", "WCSU"};
String [] genderNames = {"Male", "Female"};
int [][][] enrollment = new int[COLLEGES][GENDERS][LANGS];
NumberFormat percent = NumberFormat.getPercentInstance();


// load array with values from file
for (int college = 0; college < COLLEGES; college++)
{
data = new StringTokenizer (inFile.readLine());
for (int gender = 0; gender < GENDERS; gender++)
for (int lang = 0; lang < LANGS; lang++)
{
enrollment[college][gender][lang] = new Integer
(data.nextToken()).intValue();
totalAccum += enrollment[college][gender][lang];
}
}
screen.println ("\nStudent Enrollment Survey Results:\n");

// first iteration to calculate percentage by language
screen.println ("Language\tPercentage");
for (int lang = 0; lang < LANGS; lang++)
{
langAccum = 0;
for (int college = 0; college < COLLEGES; college++)
for (int gender = 0; gender < GENDERS; gender++)
langAccum += enrollment [college][gender][lang];
percentage = (float) langAccum / totalAccum;
screen.println(languageNames[lang] + "\t\t" + percent.format(percentage));

}
screen.println("\n");

// next iteration to calculate gender percentage
screen.println("Gender\t\tPercentage");
for (int gender = 0; gender < GENDERS; gender++)
{
genderAccum = 0;
for (int college = 0; college < COLLEGES; college++)
for (int lang = 0; lang < LANGS; lang++)
genderAccum += enrollment [college][gender][lang];
percentage = (float) genderAccum / totalAccum;
screen.println(genderNames[gender] + "\t\t" + percent.format(percentage));
}

screen.println("\n");

// last iteration to calculate college percentage
screen.println("Colleges\tPercentage");
for (int college = 0; college < COLLEGES; college++)
{
collegeAccum = 0;
for (int lang = 0; lang < LANGS; lang++)
for (int gender = 0; gender < GENDERS; gender++)
collegeAccum += enrollment [college][gender][lang];
percentage = (float) collegeAccum / totalAccum;
screen.println(collegeNames[college] + "\t\t" +
percent.format(percentage));
}

inputFile.close();
}
}