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.