General

Tech

I need Java help, please

Hello Basil, I request upon your assistance today.

So, I'm working on an assignment, and we're suppose to make 5-by-5 arrays, put numbers in them, and then rearrange them to increase or decrease from array to array. I have the array part done, but not the rearranging, and that is what I need help on. I was thinking about doing one of two things to approach this. I could use boolean statements, and if the result is true, make the arrays increase, and if the result if false, make them decrease. Another thing I can do is use {if/else} statements, where I always make "if" increase, and somehow (this is where I come to a problem), if there is someway to freely pick "else", cause it to decrease.

Here is the sample code that I will provide, because it is relatively similar to the code I am actually doing:
[quote=Masinko]
importing of a java.io.*; //for some reason, basil doesn't let you say the right thing for this line o.o

public class ExampleOfArray
{
public static void main(String[] args)
{
int array[][] =
{
{4, 6, 1},
{3, 4, 2},
{13, 15, 6}
};
for (int i=0; i<array.length; i++)
{
System.out.println(array[i][0] + "t" + array[i][1] + "t" + array[i][2]);
}
}
} [/quote]

Any help would be kindly appreciated!

February 5, 2013

11 Comments • Newest first

walridge3

note the Arrays.sort() method only sorts in ascending order. However you could probably find some way to flip the array after sorting to get descending order.

Reply February 5, 2013
Masinko

I have to write my own, or import it.

Reply February 5, 2013
j2p6t

Are you allowed to use imported sort functions (like the guy above me used) or do you have to write your own?

Reply February 5, 2013
Masinko

[quote=FreeWii4Mii]ok actually forget that for now I'll just try to answer your original question without user input/output
idk java so this will be in c++, you can probably translate without too much trouble

int main(){

bool sort_type[5] = {true, false, false, true, true}; //this decides how to sort each array
int array[5][5] = {{...}...}; //fill this in yourself

for(int i = 0; i<array.size(); i++){
if(sort_type[i]==true){
sort_array_increasing(array[i]);
}else{
sort_array_decreasing(array[i]);
} }

return 0;
}

this will sort the first, fourth, and fifth arrays in increasing order and the others in decreasing order
you'll have to implement sort_array_increasing() and sort_array_decreasing() yourself[/quote]

Thank you very, very much for your help. And by the looks of it, it almost looks the same as java, but yeah, i'll re-code it so that it fits what I need to do.

Reply February 5, 2013
FreeWii4Mii

ok actually forget that for now I'll just try to answer your original question without user input/output
idk java so this will be in c++, you can probably translate without too much trouble

int main(){

bool sort_type[5] = {true, false, false, true, true}; //this decides how to sort each array
int array[5][5] = {{...}...}; //fill this in yourself

for(int i = 0; i<array.size(); i++){
if(sort_type[i]==true){
sort_array_increasing(array[i]);
}else{
sort_array_decreasing(array[i]);
} }

return 0;
}

this will sort the first, fourth, and fifth arrays in increasing order and the others in decreasing order
you'll have to implement sort_array_increasing() and sort_array_decreasing() yourself

Reply February 5, 2013 - edited
Masinko

I think it should, I've done something similar with it.

Reply February 5, 2013 - edited
FreeWii4Mii

you said you want to be able to sort it both ways, so the program should have some way to decide which one to choose
you can have something like "do you want to sort array 1 in increasing order? (Y/N) : " etc

i'm assuming you learned input/output

Reply February 5, 2013 - edited
Masinko

What kind of input? If i need to import a class through the header, I can do that.

Reply February 5, 2013 - edited
FreeWii4Mii

does your program take input to decide how to sort each array?

Reply February 5, 2013 - edited
Masinko

[quote=FreeWii4Mii]I don't understand what you mean by increasing/decreasing an array[/quote]

I should have said made the elements in the array sorted by their number to increase or decrease.

ie:
{4, 6, 1}, ---> sorted to {1,4,6} or {6,4,1}
{3, 4, 2}, ---> sorted to {2,3,4) or {4,3,2}
{13, 15, 6} ---> sorted to {6,13,15} or {15,13,6}

And i can decide which I want. I was thinking using boolean true to make the increasing one, and false as the decreasing, but I don't really know how. So I decided 'if' as increasing and 'else' as decreasing, but I don't know how to get 'else' to run if I want 'else' instead of 'if'

Reply February 5, 2013 - edited
FreeWii4Mii

I don't understand what you mean by increasing/decreasing an array

Reply February 5, 2013 - edited