Java Forum

 Forum:   Subject:  View:  

 

 Blue Pelican Site
 Login/Edit Account


 Forgot your password?

 

     (Page 1 of 5)
1 2  3  4  5  Nxt>  

Topic: Welcome to the Question forum
ID: Author, Blue Pelican Date/Time: 01/01/2006 7:13:18 PM
Message:
Please free to post any questions to this forum. I will try to answer them as time permits. If someone else beats me to it and answers first, that's even better

Let's keep it clean and be especially encouraging and patient with beginners. Remember, we were all beginners at one point.

... complex questions are welcome too.

Topic: Sorting columns in an array
ID: Pretty poison Date/Time: 01/02/2006 1:21:29 PM
Message:
I will really appreciate help on this problem. I have an int array like this (could be any size and any dimensions):

1  7  3  5       Now suppose I want to            1   7  -2    5
5  2 -2 -1      rebuild it and have column       5   2   1   -1
8  8  1 -2      with index 2 sort like so           8   8   3   -2
0  3  5 -8                                                  0   3   5   -8

What's the best way to do this?
Reply Title: possiable answer  
ID: CombatBoots Date/Time: 01/06/2006 1:24:21 PM
Message:
i think that you could go through a for loop and put the numbers from the positions that you want and store the numbers in and array.  the you would sort that array.  then you would then use another loop to put these values into your original array.
Reply Title: Need to be more specific  
ID: Author, Blue Pelican Java Date/Time: 01/06/2006 2:26:54 PM
Message:
Yeah, that's all right. but could someone be  more specific (maybe with some code?)
Reply Title: Use "transposing" a matrix  
ID: Pete Smithwick Date/Time: 01/15/2006 3:45:04 PM
Message:
The secret to doing this problem is to first transpose the matrix (interchange rows and columns. Now, instead of sorting the third column, sort the third row using Arrays.sort (it can only sort rows, not columns)       
        int orig[][] = { {1,7,3,5}, {5,2,-2,-1}, {8,8,1,-2}, {0,3,5,-8} };
      
       //Transpose
       int x[][] = new int[orig[0].length][orig.length];
       for(int r=0; r < orig[0].length; r++)
       {
           for(int c = 0;c < orig.length; c++)
           {
              x[r][c] = orig[c][r];
           }
           System.out.println("");
       }
       
       Arrays.sort(x[2]);  //sorts the 3rd row
    //Now just transpose this x matrix and you will have what you want
Reply Title: sounds good  
ID: Big Knee Date/Time: 01/16/2006 1:39:48 PM
Message:
ya pete. this sounds exactly like the way i would do it if i needed to. the trick is to know how to "transpose" the array and then putting it back to the way it was.

~Big Knee~ (not little elbow)

Topic: UIL problem has me stumped
ID: hobojoe_2006 Date/Time: 01/26/2006 10:45:51 AM
Message:
This is a UIL problem that I had on a test last week. My teacher and I cannot figure out why we keep getting the wrong answer. Any help would be appreciated!

import java.util.*;
public class shell
{    public static void main( String [] args )
    { String s="";
    int k=6;
    while(k>2)
    s= Integer.toString(k%2 + 1) + s.trim();
    k--;}
k = Integer.parseInt(s);
System.out.println(k);    }}
Now the computer gives the right answer which is 2121, but we keep getting 1212.
Reply Title: problem solved  
ID: Big Knee Date/Time: 01/26/2006 1:21:03 PM
Message:
The line that says "s= Integer.toString(k%2 + 1) + s.trim();" is the main trick. When the new number is added to the string it is added at the BEGINNING of the string instead of the end. At first, I saw it the way you did. That was a tricky one. If you would reverse the answer you are getting, you would get the same thing as the computer.
Reply Title: why?  
ID: hobojoe_2006 Date/Time: 01/30/2006 10:54:37 AM
Message:
Could you go into more detail...why does it add to the beginning?

I have noticed if you put this line of code in it prints at the end :  s= s.trim() +Integer.toString(k%2 + 1);.  The result of that is 1212.
Reply Title: Concatenation  
ID: Author, Blue Pelican Java Date/Time: 01/30/2006 6:18:15 PM
Message:
In the statement s = Integer.toString(k% + 1) + s.trim(); the new part is concatenated to the LEFT side of s because it's on the LEFT side in the expression and we get the correct answer (2121).

If we change the problem to s =  s.trim() + Integer.toString(k% + 1); notice that now we are concatenating the new part to the RIGHT side of s becuase it's on the RIGT side of s in the expression and we get 1212.

If your IDE permits single stepping (as Blue J does) it will show you exactly what's happening.

Topic: try this
ID: CombatBoots Date/Time: 02/10/2006 2:04:11 PM
Message:
What is the output of this?


int k=5;
int j=1;
String s="";
s=s+k+j;
int s2=k+j;
System.out.print(s);
System.out.print(s2);
Reply Title: Possible answer  
ID: Ashley D Date/Time: 02/24/2006 1:35:17 PM
Message:
I think it prints 516
  << New message thread

(Page 1 of 5)             1   2   3   4   5   NextPage>>  

This Forum automated by software from bluepelicanjava.com