- 2
Arrays ....Index 0 = 20 ,index1=25,index3=43,index 4=44,index5=67,index6=15,index7=101,index8=99,index9=8.
(Q) CREATE AN ALGORITHM THAT EXPRESSES EACH INTEGER VALUE AS A PERCENTAGE OUT OF 200 (2) CREATE AN ALGORITHM TO FIND THE LARGEST AND THE SMALLEST INTEGER VALUES IN THE ARRAY (3) CREATE AN ALGORITHM THAT SORTS THE ARRAY VALUES IN ASCENDING ORDER (4)CREATE AN ALGORITHM TO COMPLETE THE CUMULATIVE TOTAL/SUM OF THE VALUES IN THE ARRAY
4 Answers
+ 1
For the first question, try type-casting the integers as doubles, then divide them by two-hundred. Type cast this new value as an integer, and use this value as you need. Loop through the whole array using this.
The code might look like this:
...
int[] a = {...};
for(int i = 0; i < a.length; i++){
double temp1 = a[i] / 200.0 * 100;
int temp2 = (int) temp1;
System.out.println(temp2);
}
+ 1
For the second question, loop through the whole array, and use the Math.min and Math.max methods to compare between each value, storing the current minimum and maximum in variables that exist outside the loop.
The code might look like this:
...
int[] a = {...};
int min = a[0], max = a[0];
for(int x = 1; x < a.length; x++){
min = Math.min(min, a[x]);
max = Math.max(max, a[x]);
}
System.out.println(min);
System.out.println(max);
...
+ 1
In the fourth one, create a variable outside of the loop in which you add the values of the array. Within the loop, increment the variable by the amount you currently are on, and loop through each value doing this.
The code might look something like this:
...
int[] a = {...};
int total = 0;
for(int x = 0; x < a.length; x++){
total += a[x];
}
System.out.println(total);
...
0
The third one might take me a moment to think on. I will get back to you once I have an idea.