Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to divide elements in two different arrays

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to divide elements in two different arrays

    I am using an array to store 5 test scores. Then I have another array for the total possible score the test could have. I just need to find out what percent the grade is out of the total possible score.

    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int[] grade = new int[5];
            int[] outOf = new int[5];
            int[] percents = new int[5];
     
     
            int total = 0;
            int average;
            int percent;
     
            grade[0] = 70;
            grade[1] = 65;
            grade[2] = 120;
            grade[3] = 150;
            grade[4] = 30;
     
            outOf[0] = 210;
            outOf[1] = 100;
            outOf[2] = 150;
            outOf[3] = 200;
            outOf[4] = 50;
     
            for(int i=0;i<grade.length;i++)
            {
                total += grade[i];
     
            }
     
            average = total / 5;
     
            System.out.println("The total is " + total + "\nThe"
                    + " average is " + average);
            }
     
            for(int j=0;j<grade.length;j++)
            {
     
     
            }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to divide elements in two different arrays

    I just need to find out what percent the grade is out of the total possible score.
    Divide one by the other and multiply by 100.

    Just a recommendation, but using an Object Oriented approach you could get the results fairly easily. For example, if you created a class called Test that contains the score and total possible score - the data is inherently packaged together in a way than can be easily understood and manipulated.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to divide elements in two different arrays

    Yeah I know thats the formula on how to get the percent, but I dont know how to implement that. I have tried:
             percents[0] = (grade[0] / outOf[0]) * 100;
     
            System.out.println(percents[0]);

    I am getting 0 as an output though. Why is this?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to divide elements in two different arrays

    grade[0] = 70;
    outOf[0] = 210;
    As you are using Integers:

    70/210 will give you 0.

    0 * 100 will give you 0.
    Last edited by newbie; February 11th, 2011 at 05:00 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to divide elements in two different arrays

    You have to use floats or doubles instead. Integer doesn't provide you anything after decimal point.

Similar Threads

  1. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 02:24 PM
  2. How can i divide a string?
    By noFear in forum Java Theory & Questions
    Replies: 9
    Last Post: September 1st, 2010, 08:45 AM
  3. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  4. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  5. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM