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.
Code :
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++)
{
}
}
Re: How to divide elements in two different arrays
Quote:
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.
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:
Code :
percents[0] = (grade[0] / outOf[0]) * 100;
System.out.println(percents[0]);
I am getting 0 as an output though. Why is this?
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.
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.