Results 1 to 10 of 10
Thread: parallel arrays
- 02-19-2011, 04:57 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
parallel arrays
Write a program that reads a file consisting of students test scores in the range of 0-200. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)
//here is my code so far
//and my problem is every time i keep track how many scores i store in first range limit it added that //number to next range limit
//im not sure if im doin the right code :(
// hope can somebody help me figure out what im missing or doin wrong :)
// i appreciate the help
import java.util.*;
import java.io.*;
public class studScores
{
public static void main(String[] args) throws FileNotFoundException
{
int score;
int printRangeLimit;
int[] rangeLimit = {24, 49, 74, 99, 124, 149, 174, 200};
int[] inRange = {0, 0, 0, 0, 0, 0, 0, 0};
Scanner inFile = new Scanner(new FileReader("scores.txt"));
while(inFile.hasNext())
{
score = inFile.nextInt();
for(int index = 0; index < rangeLimit.length; index++)
{
if(score < rangeLimit[index])
inRange[index] = inRange[index] + 1;
}
}
printRangeLimit = printArray(rangeLimit, rangeLimit.length);
System.out.println();
printInRange(inRange, inRange.length);
}// end main
//method
// i tried static void but it giving me an error it say exception outbound //something i forgot :(
public static int printArray(int[] list, int numOfElements)
{
for (int index = 0; index < numOfElements; index++)
System.out.println(list[index] + " ");
return 0;
}
public static int printInRange(int[] list, int numOfElements)
{
for (int index = 0; index < numOfElements; index++)
System.out.println(list[index] + " ");
return 0;
}
}// end class
import java.util.*;
import java.io.*;
public class studScores
{
public static void main(String[] args) throws FileNotFoundException
{
int score;
int printRangeLimit;
int[] rangeLimit = {24, 49, 74, 99, 124, 149, 174, 200};
int[] inRange = {0, 0, 0, 0, 0, 0, 0, 0};
Scanner inFile = new Scanner(new FileReader("scores.txt"));
while(inFile.hasNext())
{
score = inFile.nextInt();
for(int index = 0; index < rangeLimit.length; index++)
{
if(score < rangeLimit[index])
inRange[index] = inRange[index] + 1;
}
}
printRangeLimit = printArray(rangeLimit, rangeLimit.length);
System.out.println();
printInRange(inRange, inRange.length);
}// end main
//method
public static int printArray(int[] list, int numOfElements)
{
for (int index = 0; index < numOfElements; index++)
System.out.println(list[index] + " ");
return 0;
}
public static int printInRange(int[] list, int numOfElements)
{
for (int index = 0; index < numOfElements; index++)
System.out.println(list[index] + " ");
return 0;
}
}// end class
- 02-19-2011, 05:16 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
//and my problem is every time i keep track how many scores i store in first range limit it added that //number to next range limit
I think I can guess what you are saying here, but I don't like to guess. Perhaps you could post the contents of a small input file and the output that results (as well saying what you wanted the output to be.)
-----------------------------
Your printArray() and printInRange() methods do exactly the same thing: get rid of one of them - probably printInRange() since it has a name which is very nondescriptive - and deal with any compiler messages that result.
Why does printArray() return zero? Indeed why does it return anything? Unless you have a reason remove the "return 0;" and, again, deal with the compiler messages.
If you end up with a compiler message that you can't understand post the code and the exact and entire message as I'm sure someone can explain what it means.
- 02-19-2011, 05:31 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
i know i shouldn't use return method but everytime we compile it using void method, it's giving me an error so I just tried to return it to see if it'll work.. im still trying to figure out my code and i know that wont be my final code... im still working on it.. im focusing on how many scores stored in every range limit.. and i know that its exactly the same and it works but just like what i said the INRANge method i had, it works but its adding the number next to the number stored in range limit...
this should be my the output if im not blind: 1 2 0 6 1 3 5 8
im sorry if i couldnt explain my work that well... my english is sucks :D trying to explain it in terms where i can understand what im trying to explain :D
- 02-19-2011, 05:41 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
im sorry the output should be: 1 2 0 6 1 4 4 8
- 02-19-2011, 06:10 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
this should be my the output if im not blind: 1 2 0 6 1 3 5 8
OK. First here's your code made readable by using code tags: I've put [code] at the start and [/code] at the end so that the forum software preserves the indents. Also I couldn't resist correcting the class name (in Java they start with a capital letter), adding the missing braces around one line blocks and making the import statements explicitly document the classes you are using.
Java Code:import java.io.FileNotFoundException; import java.util.Scanner; public class StudScores { public static void main(String[] args) throws FileNotFoundException { int score; int printRangeLimit; int[] rangeLimit = {24, 49, 74, 99, 124, 149, 174, 200}; int[] inRange = {0, 0, 0, 0, 0, 0, 0, 0}; //Scanner inFile = new Scanner(new FileReader("scores.txt")); Scanner inFile = new Scanner( "76\n89\n150\n135\n200\n76\n12\n100\n150\n28\n178\n189\n167\n200" + "\n175\n150\n87\n99\n129\n149\n176\n200\n87\n35\n157\n189"); while(inFile.hasNext()) { score = inFile.nextInt(); for(int index = 0; index < rangeLimit.length; index++) { if(score < rangeLimit[index]) { inRange[index] = inRange[index] + 1; } } } printRangeLimit = printArray(rangeLimit, rangeLimit.length); System.out.println(); printInRange(inRange, inRange.length); } public static int printArray(int[] list, int numOfElements) { for (int index = 0; index < numOfElements; index++) { System.out.println(list[index] + " "); } return 0; } public static int printInRange(int[] list, int numOfElements) { for (int index = 0; index < numOfElements; index++) { System.out.println(list[index] + " "); } return 0; } }
(I've also replaced the Scanner constructor with one that allows the data to be specified as part of the code - this is common on forums like this and is done so that others can run the code as is without having to create data files.)
I've done all this in order to answer my own question: "what is the output that results?".
Here it is:
Java Code:24 49 74 99 124 149 174 200 1 3 3 8 10 12 18 23
Its the numbers at the end that are important. As you can see they get bigger and bigger. (The technical name for this is that they are the cumulative frequencies) At the moment they don't have any obvious relationship to what you expect.
------------------------
Have a look at the bit of code where you calculate these values:
Java Code:while(inFile.hasNext()) { score = inFile.nextInt(); for(int index = 0; index < rangeLimit.length; index++) { if(score < rangeLimit[index]) { inRange[index] = inRange[index] + 1; } } }
Part of the problem is with that condition "score < rangeLimit[index]" which means that you are counting the number of data elements that are strictly less than 24, 49, 74, etc. But if you look at the question you are supposed to be looking for values that are less than or equal to these values.
This suggests changing the condition to "score <= rangeLimit[index]".
The numbers you get will still not be what you want, but you will be a lot closer. The number of data elements in a particular group will now be able to be calculated by subtracting one of these cumulative frequencies from another.
-----------------------
Alternatively you could alter the condition so that it exactly expresses what you are trying to count: "if score is strictly bigger than one limit AND less than or equal to the next limit".
- 02-20-2011, 08:16 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
its a headache :(
- 02-21-2011, 12:08 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
My post ended with two suggestions:
(1) Replace the condition of the if statement so that it no longer tests for strict inequality. Then try and see the pattern whereby the frequencies emerge as differences between the cumulative frequencies.
or
(2) Change the condition in that if statement so that it implements "if score is strictly bigger than one limit AND less than or equal to the next limit".
You're free to try either, or anything better you can come up with. But there's not much more I can add without seeing code and the output that results.
- 02-21-2011, 12:23 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
i tried :) i'll try it again tomorrow been workin on it since this morning :) hehe
Java Code:import java.util.*; import java.io.*; public class studScores { public static void main(String[] args) throws FileNotFoundException { int scores; int printInRange; int[] rangeLimit = {24, 49, 74, 99, 124, 149, 174, 200}; int[] inRange = {0, 0, 0, 0, 0, 0, 0, 0}; Scanner inFile = new Scanner(new FileReader("scores.txt")); printArray(rangeLimit, rangeLimit.length); while(inFile.hasNext()) { scores = inFile.nextInt(); for(int index = 0; index < rangeLimit.length; index++) { if(scores <= rangeLimit[index]) inRange[index] = inRange[index] + 1; System.out.println("inrange " + inRange[index]); } }//end while }// end main //method public static void printArray(int[] list, int numOfElements) { for (int index = 0; index < numOfElements; index++) System.out.println(list[index] + " "); } }// end classThat out put is just crazy :)Java Code:----jGRASP exec: java studScores 24 49 74 99 124 149 174 200 inrange 0 inrange 0 inrange 0 inrange 1 inrange 1 inrange 1 inrange 1 inrange 1 inrange 0 inrange 0 inrange 0 inrange 2 inrange 2 inrange 2 inrange 2 inrange 2 inrange 0 inrange 0 inrange 0 inrange 2 inrange 2 inrange 2 inrange 3 inrange 3 inrange 0 inrange 0 inrange 0 inrange 2 inrange 2 inrange 3 inrange 4 inrange 4 inrange 0 inrange 0 inrange 0 inrange 2 inrange 2 inrange 3 inrange 4 inrange 5 inrange 0 inrange 0 inrange 0 inrange 3 inrange 3 inrange 4 inrange 5 inrange 6 inrange 1 inrange 1 inrange 1 inrange 4 inrange 4 inrange 5 inrange 6 inrange 7 inrange 1 inrange 1 inrange 1 inrange 4 inrange 5 inrange 6 inrange 7 inrange 8 inrange 1 inrange 1 inrange 1 inrange 4 inrange 5 inrange 6 inrange 8 inrange 9 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 9 inrange 10 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 9 inrange 11 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 9 inrange 12 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 10 inrange 13 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 10 inrange 14 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 10 inrange 15 inrange 1 inrange 2 inrange 2 inrange 5 inrange 6 inrange 7 inrange 11 inrange 16 inrange 1 inrange 2 inrange 2 inrange 6 inrange 7 inrange 8 inrange 12 inrange 17 inrange 1 inrange 2 inrange 2 inrange 7 inrange 8 inrange 9 inrange 13 inrange 18 inrange 1 inrange 2 inrange 2 inrange 7 inrange 8 inrange 10 inrange 14 inrange 19 inrange 1 inrange 2 inrange 2 inrange 7 inrange 8 inrange 11 inrange 15 inrange 20 inrange 1 inrange 2 inrange 2 inrange 7 inrange 8 inrange 11 inrange 15 inrange 21 inrange 1 inrange 2 inrange 2 inrange 7 inrange 8 inrange 11 inrange 15 inrange 22 inrange 1 inrange 2 inrange 2 inrange 8 inrange 9 inrange 12 inrange 16 inrange 23 inrange 1 inrange 3 inrange 3 inrange 9 inrange 10 inrange 13 inrange 17 inrange 24 inrange 1 inrange 3 inrange 3 inrange 9 inrange 10 inrange 13 inrange 18 inrange 25 inrange 1 inrange 3 inrange 3 inrange 9 inrange 10 inrange 13 inrange 18 inrange 26 ----jGRASP: operation complete.
--thanks for the help i really appreciate it :) i'll let you know if i get it runnin :)
- 02-21-2011, 11:01 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
Java Code:import java.util.*; import java.io.*; public class studScores { public static void main(String[] args) throws FileNotFoundException { int scores; int[] scoreLimit = {24, 49, 74, 99, 124, 149, 174, 200}; int[] inRange = {0, 0, 0, 0, 0, 0, 0, 0}; Scanner inFile = new Scanner(new FileReader("scores.txt")); while(inFile.hasNext()) { scores = inFile.nextInt(); for(int i = 0; i < scoreLimit.length; i++) { if(scores <= scoreLimit[i]) { inRange[i] = inRange[i] + 1; // counter break; } } }//end while for (int i = 0; i < scoreLimit.length; i++) { System.out.println("Scores stored in range " + scoreLimit[i] + " is " + inRange[i]); System.out.println(); }//end for }//end main }// end classlove it <3Java Code:----jGRASP exec: java studScores Scores stored in range 24 is 1 Scores stored in range 49 is 2 Scores stored in range 74 is 0 Scores stored in range 99 is 6 Scores stored in range 124 is 1 Scores stored in range 149 is 3 Scores stored in range 174 is 5 Scores stored in range 200 is 8 ----jGRASP: operation complete.
break statement is what i really need =)) ty for help :* :*:*
- 02-22-2011, 05:35 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
Parallel Arrays
By mwenchong in forum New To JavaReplies: 7Last Post: 11-17-2010, 12:20 AM -
two parallel arrays
By Adomini in forum New To JavaReplies: 12Last Post: 09-07-2010, 01:45 AM -
Sorting Multiple Parallel Arrays
By Pyrexkidd in forum New To JavaReplies: 7Last Post: 05-12-2010, 06:34 AM -
How to create parallel arrays
By Roselicious in forum New To JavaReplies: 6Last Post: 04-18-2010, 12:10 PM -
I need examples using parallel arrays
By dangerzone9k in forum New To JavaReplies: 10Last Post: 04-04-2009, 04:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks