Reputation: 59
I'm practicing finding max and min values, but I want to keep track of how many instances of those values I have. The program does find the min and max values, but it doesn't give the number of times the value appears in the array, so my counter is not working. Can someone give me a hand please? Thanks!
int[] values = {222,34,56,222,45,1,3,222,56,10,15,56,1,222,12,23,1,222};
int minValue = values[0];
int maxValue = values[0];
int counterMinValue = 0;
int counterMaxValue = 0;
int i;
for(i = 0; i < values.length; i++){
if (values[i] < minValue){
minValue = values[i];
counterMinValue++;
}
}
for(i = 0; i < values.length; i++){
if(values[i] > maxValue){
maxValue = values[i];
counterMaxValue++;
}
}
System.out.printf("The min value of this array is%2d and is repeated%2d times(s)", minValue, counterMinValue);
System.out.printf("\nThe max value of this array is %2d and is repeated %2d time(s)", maxValue, counterMaxValue);
Here is the output:
The min value of this array is 1 and is repeated 2 times(s)
The max value of this array is 222 and is repeated 0 time(s)
Upvotes: 1
Views: 107
Reputation: 3441
I would do something like:
for (i = 1; i < values.length; i++) {
if (values[i] == minValue) {
counterMinValue++;
}
else if (values[i] < minValue) {
minValue = values[i];
counterMinValue = 1;
}
if (values[i] == maxValue) {
counterMaxValue++;
}
else if (values[i] > maxValue) {
maxValue = values[i];
counterMaxValue = 1;
}
}
Basically the same for loop that you have, except it checks to see if the current value is equal to the minValue
, if it is, add to the counter, otherwise if the value is lower then set minValue
to the current value and reset the counter, then do the same for the max value. I placed this in one for-loop
since there is no point in iterating through the same list twice (with bigger sets of data that would greatly impact performance). I'm defaulting the counter to 1 so that you count the current element in the array and not just every element equal to it after.
Likewise, you'll probably want:
int counterMinValue = 1;
int counterMaxValue = 1;
EDIT: In the case of your example where the largest (could also be smallest) value is the first in the array it gets counted twice due to this section:
if (values[i] == maxValue) {
counterMaxValue++;
}
To correct this you can change the for loop to start at 1 (since you already set the min and max value to the first item in the array you don't need to compare it to itself: for (i = 1; ...)
which I have corrected in my above code.
Upvotes: 3
Reputation: 169
int[] values = {222,34,56,222,45,1,3,222,56,10,15,56,1,222,12,23,1,222};
int minValue = values[0];
int maxValue = values[0];
int counterMinValue = 0;
int counterMaxValue = 0;
int i;
for(i = 0; i < values.length; i++){
if (values[i] == minValue) {
counterMinValue++;
}
else if (values[i] < minValue){
minValue = values[i];
}
}
for(i = 0; i < values.length; i++) {
if (values[i] > maxValue) {
maxValue = values[i];
} else if (values[i] == maxValue) {
counterMaxValue++;
}
}
System.out.printf("The min value of this array is%2d and is repeated%2d times(s)", minValue, counterMinValue);
System.out.printf("\nThe max value of this array is %2d and is repeated %2d time(s)", maxValue, counterMaxValue);
}
I got these results:
The min value of this array is 1 and is repeated 3 times(s)
The max value of this array is 222 and is repeated 5 time(s)
Upvotes: 0