Antoniohawk 11-02-2004, 03:31 AM Be forewarned that this is a homework assignment and that I'm not asking for someone to do it for me, only for a little help since I can't figure out what the heck is wrong with it.
In a nutshell, this program is supposed to take a one dimensional array list and make a matrix out it (multi-dimensional array). It may be my equation, stored under the variable of position, but I'm pretty sure that that is right because I checked it on paper. The problem is that it is outputting the matrix with the opposite dimensions of what I send as a parameter to the method. Instead of 3 columns and 5 rows, it prints out 5 columns and 3 rows. Thanks to anyone that can provide some insight into this mess. :)
import java.util.ArrayList;
public class Lab26a8{
public static void main(String args[]){
System.out.println("\nLAB26A 80-POINT VERSION\n");
Matrix m2 = new Matrix(3,5);
m2.displayMatrix("Matrix m2 3 X 5 Display");
System.out.println();
int count = 100;
}
}
class Matrix{
private ArrayList list; // one-dimensional array stores matrix values
private int listSize; // total number of elements in the matrix
private int numRows; // number of rows in the matrix
private int numCols; // number of cols in the matrix
public Matrix(int c, int r){
list = new ArrayList();
numCols = c;
numRows = r;
listSize = c * r;
for(int i = 0; i < listSize; i++)
list.add(new Integer(0));
}
public int getRows(){
return numRows;
}
public int getCols(){
return numCols;
}
public int getSize(){
return listSize;
}
public int getValue(int c, int r){
int rowLength = getRows();
int colLength = getCols();
int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
return ((Integer)list.get(position)).intValue();
}
public void setValue(int r, int c, int value){
int rowLength = getRows();
int colLength = getCols();
int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
list.add(position,new Integer(value));
}
public void displayMatrix(String str){
System.out.println(str);
for(int j = 0; j < getCols(); j++){
for(int i = 0; i < getRows(); i++){
System.out.print(getValue(j,i) + " ");
}
System.out.println("");
}
}
}
turbowrx 11-07-2004, 02:49 AM Well, simple problem. Just switch your display for loop so that your loop is based on rows first, outside loop, and then columns, inner loop.
turbowrx 11-07-2004, 02:54 AM Also, when you say a 3 x 5 matrix, that is actually a 3 row by 5 column matrix, which is not what you are outputting. I think it would make sense to reverse the order in which you do the program. Do rows calculations first, then column. For instance, you take columns first in the constructor. Hope I helped, although I did very little.
Antoniohawk 11-07-2004, 03:39 AM Thanks for the help, I thought that I would never get any lol. There are no runtime errors now, only logic errors. I think that the problem lies in the passing of the parameters as you stated, now I just have to figure out what in the heck is going on with them. :(
Thanks again for the help man, you're a life-saver. :thumbsup:
turbowrx 11-07-2004, 04:41 AM The code you initially posted had problems with the arraylist. It was getting out of bounds exceptions. I just through in some println's to check your position variable and notice it was going too far. Then it worked, but it did everything backwards, well kinda. It was actually producing the 3 x 5 matrix, but you really want a 5 x 3, so then I switched the loop construct and it worked perfect.
Antoniohawk 11-07-2004, 04:48 AM Another problem has arisen and this one is pretty weird. I only showed you part of the code so that it would be easier on both of us to fix. Here's the same program, but with a different constructor being used. The problem seems to be with the variable count, but I don't understand what's going on with it.
import java.util.ArrayList;
public class Lab26a8{
public static void main(String args[]){
System.out.println("\nLAB26A 80-POINT VERSION\n");
Matrix m2 = new Matrix(3,5);
m2.displayMatrix("Matrix m2 3 X 5 Display");
System.out.println();
int count = 100;
System.out.println("m2.getRows() = " + m2.getRows()); //
System.out.println("m2.getCols() = " + m2.getCols()); //
System.out.println("");
for (int r = 0; r < m2.getRows(); r++)
{
System.out.println("r = " + r); //
for (int c = 0; c < m2.getCols(); c++)
{
m2.setValue(r,c,count);
count++;
System.out.println("c = " + c); //
System.out.println("count = " + count); //
}
System.out.println(""); //
}
m2.displayMatrix("Matrix m2 3 X 5 Consecutive Integers Display");
System.out.println();
}
}
class Matrix{
private ArrayList list; // one-dimensional array stores matrix values
private int listSize; // total number of elements in the matrix
private int numRows; // number of rows in the matrix
private int numCols; // number of cols in the matrix
public Matrix(int r, int c, int value){
list = new ArrayList();
numRows = r;
numCols = c;
listSize = r * c;
for(int i = 0; i < listSize; i++)
list.add(new Integer(value));
}
public int getRows(){
return numRows;
}
public int getCols(){
return numCols;
}
public int getSize(){
return listSize;
}
public int getValue(int r, int c){
int rowLength = getRows();
int colLength = getCols();
int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
return ((Integer)list.get(position)).intValue();
}
public void setValue(int r, int c, int value){
int rowLength = getRows();
int colLength = getCols();
int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
list.add(position,new Integer(value));
}
public void displayMatrix(String str){
System.out.println(str);
for(int j = 0; j < getRows(); j++){
for(int i = 0; i < getCols(); i++){
System.out.print(getValue(j,i) + " ");
}
System.out.println("");
}
}
}
turbowrx 11-07-2004, 05:09 AM It looks like count is the value that is being used to give a value to the data that you are entering into the matrix. Instead of a zero, you are using 100.
turbowrx 11-07-2004, 05:10 AM Oh, and you are adding 1 to count all the time too. I'm loading your new code right now.
turbowrx 11-07-2004, 05:15 AM It seems to run fine for me. What is concerning you?
Antoniohawk 11-07-2004, 05:37 AM My output for that object is
100 101 102 105 106
105 106 107 110 111
110 111 112 113 114
Are you getting that too? I have no idea why it won't just count up one at a time instead of jumping from 102 to 105.
turbowrx 11-07-2004, 06:57 AM Well, it's your set and get value methods, specifically the method by which you obtain the position number. It seems to work when you are using an n x n matrix, 2 x 2, 3 x 3, etc., but fails when you are using an n x m.
I also got the same values as you. You have to look at the position. There is an easier way to add the values. Just add to the list based on how many times you've been through the loop. Easier calculation for you and it works the same.
I'll get back to you tomorrow on the position problem. Later.
turbowrx 11-07-2004, 07:17 AM Couldn't resist solving it tonight.
Here is the new position algorithm -
int position = (r * colLength) + c;
Frankly though, why don't you just use a multi-dimension array for this.
Here's the code I have.
import java.util.ArrayList;
public class Matrix {
private ArrayList list; // one-dimensional array stores matrix values
private int listSize; // total number of elements in the matrix
private int numRows; // number of rows in the matrix
private int numCols; // number of cols in the matrix
public Matrix(int r, int c, int value){
list = new ArrayList();
numRows = r;
numCols = c;
listSize = r * c;
//for(int i = 0; i < listSize; i++)
// list.add(new Integer(value));
}
public int getRows(){
return numRows;
}
public int getCols(){
return numCols;
}
public int getSize(){
return listSize;
}
public int getValue(int r, int c){
int rowLength = getRows();
int colLength = getCols();
//System.out.println("r in get = " + r);
int position = (r * colLength) + c;
//int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
//System.out.print("Pos get = " + position + "; ");
return ((Integer)list.get(position)).intValue();
}
public void setValue(int r, int c, int value, int pos){
int rowLength = getRows();
//int colLength = getCols();
//int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
//System.out.println("Pos set = " + position + "; ");
list.add(pos,new Integer(value));
}
public void displayMatrix(String str){
System.out.println(str);
for(int j = 0; j < getRows(); j++){
for(int i = 0; i < getCols(); i++){
System.out.print(getValue(j, i) + " ");
}
System.out.println("");
}
}
public static void main(String[] args) {
System.out.println("\nLAB26A 80-POINT VERSION\n");
Matrix m2 = new Matrix(3, 5, 100);
//m2.displayMatrix("Matrix m2 3 X 5 Display");
System.out.println();
int count = 100;
System.out.println("m2.getRows() = " + m2.getRows()); //
System.out.println("m2.getCols() = " + m2.getCols()); //
System.out.println("");
int pos = 0;
for (int r = 0; r < m2.getRows(); r++)
{
//System.out.println("r = " + r); //
for (int c = 0; c < m2.getCols(); c++)
{
m2.setValue(r,c,count, pos);
pos++;
count++;
//System.out.println("r = " + r + " c = " + c + " and count = " + count); //
}
//System.out.println(""); //
}
m2.displayMatrix("Matrix m2 3 x 5 Consecutive Integers Display");
//System.out.println("");
}
}
If you have any question, just fire them off.
Antoniohawk 11-07-2004, 04:34 PM That works beautifully, thank you. :)
Any idea why my algorithm wouldn't work? I realize now that it's overcomplicated, but it worked on paper. Oh, about using a multi-dimensional array, everyone else in class was doing that so I was trying to do it a different way.
turbowrx 11-07-2004, 06:32 PM Well, I started doing the math on your algorithm and it just didn't work. It worked for the first row, but stopped working whenever you went to the second row, at least with an n x m matrix. I'm not sure how it worked for you, but it shouldn't have. Just a reminder, had you started your row and column counting at 1, the algorithm would be a little different.
Basically, your algorithm just wasn't calculating exactly what it should have been calculating.
Antoniohawk 11-07-2004, 06:37 PM Alright, fair enough. Thanks again for your help, I'm deeply indebted to you. This flu has really gotten me behind in my school work and this has helped me a great deal. :)
turbowrx 11-07-2004, 10:55 PM No problem. I'm always willing to help if I can. I don't really do whole problems for people, but you were just about done with what you had, you had just gotten into a little sticky point. Boy, you were so close the first time to. Hardly anything changed to get it to work. Good luck.
|
|