Ian
Ian

Reputation: 3898

Calculate the percentage between 2 arrays and output a new array

For most of you, this will be very simple, but I'm a newbie- so don't hate too much! Ok, so I have two arrays, one showing 'correct scores' and another showing 'incorrect scores'. I want to create a new array using these which shows the % correct.

I have written :

int[] correct1 = {20, 20, 13, 15, 22, 18, 19, 21, 23, 25};
int[] incorrect1 = {2, 1, 5, 2, 2, 5, 8, 1, 0, 0, 1};

for(int a = 0; a < correct1.length; a++ ){ 
        int[] percentage1 = ((correct1[a] / (correct1[a] + incorrect1[a]))*100);
    }

But this gives me a type mismatch between int and int[]. Any help would be appreciated, thanks.

Upvotes: 1

Views: 1076

Answers (1)

Achintya Jha
Achintya Jha

Reputation: 12843

for(int a = 0; a < correct1.length; a++ ){ 
        int[] percentage1 = ((correct1[a] / (correct1[a] + incorrect1[a]))*100);
    }

Above should be:

double[] percentage1 = new double[correct1.length];
for(int a = 0; a < correct1.length; a++ ){ 
    percentage1[a] = (double)((correct1[a] / (correct1[a] + incorrect1[a]))*100);
}

Upvotes: 4

Related Questions