Sunny303
Sunny303

Reputation: 1

Using info from two arrays to calculate contents of third array

I have an array with the total population of a country and another array with the total number of cases of COVID for each country. I need to divide the number of cases by the total population and store the percentage in a third array. I'm stuck on the syntax though and no one from my class is available for outreach until tomorrow. Can anyone please help me get past this step? I've tried lots of different ways of getting the new percentage array, but nothing works. I can't use int for the percentage because my professor wants it with four places after the decimal. No references in the examples or the book match what I'm trying to do.

Thanks in advance for your advice!

int[] cases = {10_036_282, 8_553_657, 5_675_032, 1_856_292, 1_781_997, 1_381_218, 1_250_499, 1_216_747, 1_149_068, 967_825};

int[] population = {327_096_265, 1_352_647_786, 209_469_323, 64_990_511, 145_734_038, 46_692_858, 44_361_150, 67_141_684, 49_661_048, 126_190_788};
    

//You must calculate the percentage of cases based on the number of cases and the population.

for (int i = 0; i < countries.length; i++){
    double percentage[i] = ((cases[i] / population[i]) * 100);  
}

Upvotes: 0

Views: 73

Answers (3)

Mustafa Poya
Mustafa Poya

Reputation: 3027

all of your number is in integer and division of integer by integer is an integer, at first you need to cast one or all of the values to double, and secondly you said with 4 decimal points so you need to format your decimal number as below:

int[] cases = { 10_036_282, 8_553_657, 5_675_032, 1_856_292, 1_781_997, 1_381_218, 1_250_499, 1_216_747,
        1_149_068, 967_825 };

int[] population = { 327_096_265, 1_352_647_786, 209_469_323, 64_990_511, 145_734_038, 46_692_858, 44_361_150,
        67_141_684, 49_661_048, 126_190_788 };

double[] average = new double[cases.length];

for (int i = 0; i < average.length; i++) {
    average[i] = ((cases[i] / (double)population[i]) * 100.0);
    average[i] = Double.parseDouble(String.format("%.4f", average[i]));
}

Upvotes: 0

Saiful Islam
Saiful Islam

Reputation: 1269

For simplification calculate everything just put into double and than convert the calculated value to int and store in the array.

Here you go!

import java.util.Queue; 
import java.util.LinkedList; 
import java.util.Arrays; 

class Main{
    public static void main (String[] args) {
        int[] cases = {10_036_282, 8_553_657, 5_675_032};
        int[] population = {327_096_265, 1_352_647_786, 209_469_323};
        int[] percentage = new int[cases.length];
        for(int i = 0; i < cases.length; i++){
            double temp = ((double)cases[i] / (double)population[i] ) * 100;
            int pr = (int) temp;
            percentage[i] = pr;
        }
        System.out.println(Arrays.toString(percentage));
    }
}

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35096

Unless otherwise explicitly instructed, you should use doubles for percentages. Otherwise, cases[i] / population[i] is going to normally result in 0

double[] percentage = new double[...];
percentage[i] = ((cases[i] / (double)population[i]) * 100); 

Upvotes: 2

Related Questions