Beginner question - how to apply block of code on multiple arrays?
in
Programming Questions
•
1 years ago
Hi everyone,
i have some very simple algorithm to calculate percentage from items in array:
- //array of percents
- float[] city={69.7, 75, 73.7, 78.9, 81.6, 77.6, 84.2, 77.6, 85.5, 84.2};
- float cityComp=0;
- float cityFinal=0;
- //sum of array
- for (int i = 0; i<city.length; i++){
- cityComp+=city[i];
- }
- //percents from sum
- cityFinal = cityComp/city.length;
- println(cityFinal);
My question is how can I apply this algorithm on another array without have to write new variables etc.
Thanks for any help ;)
1