1

I already have the code for how to convert a bidimensional one into a dimensional one, but I don't know how to do it vice-versa. Here's my code:

package laboratorio9;

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner entrada = new Scanner(System.in);
    int A[][];
    int B[];
    int n;
    int m;
    int nb = 0;

    System.out.println("Enter the number of lines in your array.");
    n = entrada.nextInt();

    System.out.println("Enter the number of columns in your array.");
    m = entrada.nextInt();

    A = new int[m][n];
    B = new int[m*n];

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        System.out.println("Enter A["+i+"]["+j+"]");
        A[i][j] = entrada.nextInt();
      }
    }
    System.out.println();

    for (int i = 0; i < m;i++) {
      for (int j = 0; j < n; j++) {
        B[nb] = A[i][j];
        nb++;
      }
    }

    for (int i = 0; i < m*n; i++) {
      System.out.println(B[i] + "  ");
    }
    System.out.println();

    Boolean Swap = false;

    do {
      Swap = false;
      for (int i=0;i<B.length-1;i++)
        if (B[i] > B[i + 1]) {
          int Temp = B[i];
          B[i] = B[i + 1];
          B[i + 1] = Temp;
          Swap = true;            
        }
    } while (Swap);

    for (int i=0;i<B.length;i++)
      System.out.println(B[i]);
  }
}
2
  • It would be neat if you format your code correctly Commented Mar 20, 2009 at 4:08
  • Oh man, I'm not doing this for you. This is 100% doable problem, and solving it will make you a better programmer. Commented Mar 20, 2009 at 4:17

1 Answer 1

2

you're looking for something like:

int j = 0;
int k = 0;
for ( int i = 0; i < nb;i++)
{
    C[j][k] = B[i];
    k++;
    if( k >= n )
    {
        j++;
        k = 0;
        if( j >= m )
            throw Error();
    } 
}

which is the same as:

int k = 0;
for ( int i = 0; i < m;i++)
{
    for ( int j = 0; j < n;j++)
    {
        C[i][j] = B[k++];
    }
}

but with trying to help explain the concept more.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.