I want the method to count all the maximum/minimum values in the 2D array poängInsamling. Each domare (=judge) gives a value to all deltagare (=members). I want to remove the maximum/minimum value of each deltagare. The code I have now only works if there are 2 or less members.
This is what I got:
for (int x = 0; x < deltagare; x++) {
for (int y = 0; y < domare; y++) {
if (poängInsamling[y][x] == -1) {
poängInsamling[y][x] = 0;
break;
}
}
}
return poängInsamling;
}
Thanks in advance, I've been trying to fix this for hours.
Edit: int[][]PoängInsamling = int[domare][deltagare];
If all deltagare has the same value, all their points end up 0.
You are searching the entire 2D array in order to remove the lowest and highest value across all members, but you want to remove the lowest and highest value only for the current member. You can eliminate the second loop if you keep track of the index having the maximum/minimum value.
For example, for the maximum (the minimum will be similar) :
int max = -1;
int maxIndex = -1;
for(int i = 0; i < deltagare; i++) {
max = -1; // clear the max value when starting with a new member
maxIndex = -1;
for(int t = 0; t < domare; t++) {
if (poängInsamling[t][i] > max) {
max = poängInsamling[t][i];
maxIndex = t;
}
}
// clear the max value for the current member
poängInsamling[maxIndex][i] = -1;
}
Related
I am trying to get the indices of large numbers within an array and I am having trouble doing so..
My code works if there is only one large number in the array. However, if there is one or more of the same large number, it does not works.
For example,
if I have an array {2,1,1,2,1}, it should return me the index 0 and 3 (this does not)
if I have an array {2,1,3,2,1}, it will then return me index 2. (this works)
Currently, I am trying to store the index by making use of an array, however in my code, as given in my example above, it only returns me the index of the first large number it found.
My code:
class Main {
public static void getIndexOfMax(int array[]) {
int max = array[0];
int pos = 0;
int max_index[] = new int[array.length];
int counter = 0;
for(int i=1; i<array.length; i++) {
if (max < array[i])
{
pos = i;
max = array[i];
max_index[counter] = pos;
counter += 1;
}
}
public static void main(String[] args) {
int[] num = {2,1,1,2,1};
getIndexOfMax(num);
}
}
Thanks in advance for any replies!
You need to check another case when you find the maximum value again and store the index. And when the new maximum value starts the counter from 0 again.
public static void printIndexOfMax(int array[]) {
int max = 0;
int max_index[] = new int[array.length];
int counter = 0;
for (int i = 0; i < array.length; i++) {
if (max <= array[i]) { // Allowing same maximum number
if (max < array[i]) // Start counter from 0 when new maximum value found
counter = 0;
max = array[i];
max_index[counter] = i;
counter++;
}
}
for (int i = 0; i < counter; i++) {
System.out.println(max_index[i]);
}
}
You can have another conditional stating, if max is equal to an array element, then store that element index also. Then you would have to declare another array for the max index positions.
`if (max < array[i]) {
pos = i;
indexStorage.add(pos)
max = array[I];
max_index[counter] = pos;
counter += 1;
}`
I am trying to figure out on how to get the second lowest value from an array that is input by the user.
To get the lowest value I've done this:
int min = Integer.MAX_VALUE;
for(int i = 0; i<array.length; i++) {
if( array[i] < min ) {
min = array[i];
}
}
To get the second lowest value I have tried this
int secMin = 0;
for(int i = 0; i<array.length-1; i++) {
if(array[i+1] > array[i]) {
secMin = array[i];
}
}
The output is not consistent, meaning sometimes its right sometimes it isn't.I've tried changing the loop as well as the statements but its just guess work and I'm not making any real progress. Any tips?
If you want to keeping doing it as 2 separate loops, just do the same as for the min value, but skip the min value when looking.
// Find lowest value
int min = Integer.MAX_VALUE;
for (int value : array) {
if (value < min) {
min = value;
}
}
// Find second-lowest value
int secMin = Integer.MAX_VALUE;
for (int value : array) {
if (value != min) { // Skip/ignore lowest value
if (value < secMin) {
secMin = value;
}
}
}
You can of course combine that:
// Find second-lowest value
int secMin = Integer.MAX_VALUE;
for (int value : array)
if (value != min && value < secMin)
secMin = value;
You could simply update your first loop to look for both min and secMin at the same time. Try this:
int min = Integer.MAX_VALUE;
int secmin = Integer.MAX_VALUE;
for(int i = 0; i<array.length; i++) {
if( array[i] < min ) {
secmin = min; // the min is now second min
min = array[i]; // the curent value becomes min
} else if ( array[i] < secmin ){
// if the value is greater than min but still less than second min then...
secmin = array[i];
}
}
I haven't tested it but something along those lines should work
There's another approach described here.
So guys, I'm developping a program for Structural Analysis and I came across a problem that I'm having some problem to solve. Basically, I have a system of equations, of which I only need to solve some of them.
The ones I need to solve depend on a boolean array, true if I do, false if I don't. This way, having a true value in the nth element of the array means I'll have to solve the nth equation, therefore meaning that I have to get the nxn element of the matrix of the system of equations.
Do you guys have any insight on it?
So, this is what I've come up with:
//Firstly, define the size of the subsystem:
int size = 0;
for(int i = 0; i < TrueorFalseMatrix.m; i++) {
if(TrueorFalseMatrix.data[i][0] != 0)
size+= 1;
}
//Then we can assign the size values to the Matrix of the subsystem
System_A = Matrix.matrizEmpty(size,size);
System_B = Matriz.matrizEmpty(size, 1);
//This array will store the coordinates of the coefficients that
//will be used
int[] Coordinates = new int[size];
//We store these coordinates in the array
int count = 0;
for(int i = 0; i < TrueorFalseMatrix.m; i++) {
if(TrueorFalseMatrix.data[i][0] != 0) {
Dtrue[count] = i;
count++;
}
}
//We can now assign values to our system Matrix
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
System_A.data[i][j] = SourceMatrix.data[Dtrue[i]][Dtrue[j]];
}
System_B.data[i][0] = SourceResultVector.data[Dtrue[i]][0]
}
//Results
double[] Results = System.solve(System_A,System_B);
For an input like the following is it ok to use Float.compare to find the biggest float in an array?
Float[] v = {0.38118651972530804, 0.3580139606405057, 0.7358862700995704};
float max = -1;
for (int i = 0; i < v.length; i++)
if (Float.compare(max,v[i]) > 0) max = v[i];
Is there a better way? or maybe there could be some precision error?
I'd use a collection and call the max() method. See: here
How about one line:
Float max = Collections.max(Arrays.asList(v));
There can be errors if all the values in the array are less than -1. Better to initialize max as v[0].
And, your code finds the smallest element. Not the largest element.
Float[] v = {0.38118651972530804, 0.3580139606405057, 0.7358862700995704};
float max = v[0];
for (int i = 1; i < v.length; i++)
if (Float.compare(max, v[i]) < 0) max = v[i];
How about this-
float max = v[0]; // the first item in array
// Loop through all items starting the second one
for (int i = 1; i < v.length; i++)
{
if (v[i] > max) // If current item is bigger, update max to current item
{
max = v[i];
}
}
In my JAVA code, I am given a data and I have to find the mode. Everything successfully compiled, and every method works. However, when I try to access the mode, I get an java.lang.ArrayIndexOutOfBoundsException: 987 in my terminal window. The highlighted portion is in the following method, which is one of my max methods. The data array, by the way, is just int [] data.
public int maxOfData(int [] oa)
{
int max = oa[0];
for (int i = 1; i < size; i++)
{
if (oa[i] > max)
max = oa[i];
}
return max;
}
The exception is on the line if(oa[i] > max)
And the mode code is this:
public int[] modeOfData()
{
int[] tally = new int[maxOfData() + 1];
for( int i = 0; i < size; i++)
{
tally[data[i]]++;
}
//max contains frequency of modes
int max = maxOfData (tally);
int count = 0;
for( int i = 0; i < tally.length; i++)
{
if( tally[i] == max )
count++;
}
//count occurence of maxValue in tally (for)
//that determines how many modes there are
//declare another int called modes
int[] modes = new int[count];
//size of array should be count
//loop through tally and extract modes: it's the index value.
int pos = 0;
for( int i = 0; i < tally.length; i++)
{
if(tally[i] == count)
modes[pos++] = i;
}
return modes;
//modes are where the values are == max
}
My other max for data is the same but data instead of oa. I need both max methods, just like that, according to my teacher. So what do I do? How do I fix this?
I think the line
for (int i = 1; i < size; i++)
should be
for (int i = 1; i < oa.length; i++)
ArrayIndexOutOfBound Exception is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. Whenever you iterate an Array object. you need to iterate while checking the index is always lesser than its length
for example,
for(int i=1;i<array.length;i++){
//access array content in ith position
System.out.println(array[i]);
}
Your size variable has the value of an illegal array index. that's the problem
Look at the number stored into size and then check to see what size you declared oa[] to be. If size is bigger than the size of oa[] then you have a problem.
Problem is in this part of code
int max = oa[0];
for (int i = 1; i < size; i++)
{
if (oa[i] > max)
max = oa[i];
}
rewrite the method maxOfData(int [] oa) with proper checks like this
public int maxOfData(int[] oa) {
if (oa == null) {
throw new IllegalArgumentException("Input array is null");
}
int max=oa[0];
for (int i = 1; i < oa.length; i++) {
if (oa[i] > max)
max = oa[i];
}
return max;
}
if input array is null it should not be processed.