Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Coppied from alt.comp.lang.java

Thread view: 
Kris M - 19 Mar 2005 02:27 GMT
I posted this in the wrong group.

/*Can i calculate the page totals, column totals, and row totals in one pass
through the array instead of the three passes i have coded? I would like to
have
it loop through the array once to calculate and display average. I dont even
think its possible*/

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();
}
}
Boudewijn Dijkstra - 20 Mar 2005 13:55 GMT
>I posted this in the wrong group.
>
[quoted text clipped - 3 lines]
> it loop through the array once to calculate and display average. I dont even
> think its possible*/

I think it is.  Make langAccum, genderAccum and collegeAccum two-dimensional
arrays.  This will allow you to pass over your enrollment once.  E.g.

int item = enrollment[college][gender][lang]
totalAccum += item;
langAccum[college][gender] += item;
genderAccum[college][lang] += item;
collegeAccum[gender][lang] += item;

But then you need extra passes for each of your accums.  E.g.

screen.println ("Language\tPercentage");
for (int lang = 0; lang < LANGS; lang++)
{
 int langTotalAccum = 0;
 for (int college = 0; college < COLLEGES; college++)
  for (int gender = 0; gender < GENDERS; gender++)
   langTotalAccum += langAccum[college][gender];
 percentage = (float) langTotalAccum / totalAccum;
 screen.println(languageNames[lang] + "\t\t" + percent.format(percentage));
}

Both methods make the same amount of iterations.  Your method makes 1 addition
and 1 3d array lookup per iteration.  My method makes 4 additions, 1 3d array
lookup and 3 2d array lookups per iteration for the first loop; and 1 addition
and 1 2d array lookup per iteration for the last 3 loops.  Average over all 4
loops, my method takes 7/4 additions, 1/4 3d array lookup and 1 2d array
lookup.

So, assuming that a 2d or 3d array lookup takes a few additions and
multiplications, and also assuming that a 2d array lookup is an order of
magnitude faster than a 3d array lookup, my method must be faster.
Kris M - 20 Mar 2005 14:34 GMT
thank you for the thoughtful reply.

>>I posted this in the wrong group.
>>
[quoted text clipped - 40 lines]
> multiplications, and also assuming that a 2d array lookup is an order of
> magnitude faster than a 3d array lookup, my method must be faster.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.