School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,120 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,946 people online right now. Registration is fast and FREE... Join Now!




Using parallel arrays to determine percentage

 

Using parallel arrays to determine percentage, How do I make it read the string and int at the same time?

randomdane

25 Oct, 2009 - 08:28 PM
Post #1

New D.I.C Head
*

Joined: 4 Oct, 2009
Posts: 8

Hi, Im having trouble using parallel arrays.

I have been given a program written in MasH (documentation for this can be found at http://www.cit.gu.edu.au/~arock/ my lecturer wrote it to teach noobs the basics of java).

I have to modify code put up by my lecturer (shown below) to creat a program that reads the next word (if there is one) and the int that goes with that word. If the word is not in the array add it to it plus add the int value in a parallel array. If the word is in the array already, just add the int value to the position in the parallel array. (i hope that makes sense). I then have to take all the values for each word and work out the total percentage of each word. eg if the total for baked beans was 10 and the total for every int was 100 i have to print "baked beans 10 %"
and do the same for every word in the array.

This is the code my lecturer gave us to modify. It reads a file then prints how many times a word occured in that file in alphabetical order.

CODE

/*
** file:    CountEachWord.mash
** author:  Andrew Rock
** purpose: Program to count the number of times
**          each distinct word in a text occurs
**          using a linear search.
*/

import console;

final int MAX = 10000; // maximum distinct words
String[] word = new String[MAX]; // distinct words
int[] freq = new int[MAX]; // distinct words
int numWords = 0; // number of distinct words

// lettersOnly(s) returns only the letters in
// s in upper case.
String lettersOnly(String s) {
   String t = "";
   for (int i = 0; i < length(s); i = i + 1) {
       char c = charAt(s, i);
       if ('A' <= c && c <= 'Z') {
          t = t + c;
       } else if ('a' <= c && c <= 'z') {
          t = t + (char) (c - 'a' + 'A');
       } else {
       }
   }
   return t;
}

// linearSearch(s, a, n) searches array a (that has
// n elements) for string s. Returning either:
//    the position where s occurs in a, or
//    n if s does not occur in a.
int linearSearch(String s, String[] a, int n) {
   int i = 0;
   int j = n;
   while (i < j) {
      if (equals(a[i], s)) {
         j = i;
      } else {
         i = i + 1;
      }
   }
   return i;
}

// readDistinct() reads standard input and finds
// all of the distinct words.
void readDistinct() {
   while(isNextWord()) {
      String w = lettersOnly(readWord());
      if (length(w) > 0) {
         int p = linearSearch(w, word, numWords);
         if (p == numWords) {
            word[numWords] = w;
            numWords = numWords + 1;
            freq[numWords] = 1;
         } else {
            freq[p] = freq[p] + 1;
         }
      } else {
      }
   }
}

// sort() a selection sort.
void sort() {
  for (int i = 0; i < numWords - 1; i = i + 1) {
     for (int j = i + 1; j < numWords; j = j + 1) {
        if (freq[j] < freq[i]) {
           int ti = freq[i];
           freq[i] = freq[j];
           freq[j] = ti;
           String ts = word[i];
           word[i] = word[j];
           word[j] = ts;
        } else {
        }
     }
  }
}

// writeFrequencies() prints out all the
// words and how often they occurred.
void writeFrequencies() {
   for (int i = 0; i < numWords; i = i + 1) {
      println(word[i] + " " + freq[i]);
   }
}

void main() {
   readDistinct();
   sort();
   writeFrequencies();
}


This is the code i came up with
CODE

/*
** file:    percentage.mash
** purpose: Program to count the number of times
**          each distinct word in a text occurs
**          using a linear search.
*/

import console;

final int MAX = 10000; // maximum distinct words
String[] word = new String[MAX]; // distinct words
int[] freq = new int[MAX]; // distinct words
int numWords = 0; // number of distinct words



// linearSearch(s, a, n) searches array a (that has
// n elements) for string s. Returning either:
//    the position where s occurs in a, or
//    n if s does not occur in a.
int linearSearch(String s, String[] a, int n) {
   int i = 0;
   int j = n;
   while (i < j) {
      if (equals(a[i], s)) {
         j = i;
      } else {
         i = i + 1;
      }
   }
   return i;
}

// readDistinct() reads standard input and finds
// all of the distinct words.
void readDistinct() {
   while(isNextWord()) {
      String w = (readWord());
      if (length(w) > 0) {
         int p = linearSearch(w, word, numWords);
         if(isNextInt())  {
            int n = readInt();
            freq[p]=freq[p] + n;
            if (p == numWords) {
            word[numWords] = w;
            numWords = numWords + 1;
            } else {
            freq[p] = freq[p] + 1;
           }
         }
      } else {
      }
   }
}



// writeFrequencies() prints out all the
// words and how often they occurred.
void writeFrequencies() {
   for (int i = 0; i < numWords; i = i + 1) {
      println(word[i] + " " + freq[i]);
   }
}

void main() {
   readDistinct();
   writeFrequencies();
}


My code doesn't read all the values as I went through the txt file I made this program read and the values were different. I also know I don't have anything in there to get the total for all ints and working the percentage out, I'm just trying to make it read the correct amount first.

Any advice would be greatly appreciated.




User is offlineProfile CardPM
+Quote Post


randomdane

RE: Using Parallel Arrays To Determine Percentage

25 Oct, 2009 - 11:42 PM
Post #2

New D.I.C Head
*

Joined: 4 Oct, 2009
Posts: 8

I managed to figure it out
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:50PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month