Results 1 to 10 of 10

Thread: parallel arrays

  1. #1
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    //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.

  3. #3
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    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

  4. #4
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    im sorry the output should be: 1 2 0 6 1 4 4 8

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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".

  6. #6
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    its a headache :(

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  8. #8
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    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 class
    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.
    That out put is just crazy :)

    --thanks for the help i really appreciate it :) i'll let you know if i get it runnin :)

  9. #9
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    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 class
    Java 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.
    love it <3

    break statement is what i really need =)) ty for help :* :*:*

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Sorry - I haven't been able to connect for a bit. Well done!

Similar Threads

  1. Parallel Arrays
    By mwenchong in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 01:20 AM
  2. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 02:45 AM
  3. Sorting Multiple Parallel Arrays
    By Pyrexkidd in forum New To Java
    Replies: 7
    Last Post: 05-12-2010, 07:34 AM
  4. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 01:10 PM
  5. I need examples using parallel arrays
    By dangerzone9k in forum New To Java
    Replies: 10
    Last Post: 04-04-2009, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •