Question

I have a table that looks like this. 1234 5678 9101112 I need to use this...

I have a table that looks like this.

1234

5678

9101112

I need to use this code

public class Lab8
{
    public static void main(String argv[])
    {
        int ar[][] = new int[3][4];

        int n = 1;

        for (int i=0; i < ar.length; i++) {
            for (int j=0; j< ar[i].length; j++) {
                ar[i][j] = n;
                n++;
            }
        }

        // print out the elements of the array
        // left to right, top to bottom
        for (int i=0; i < ar.length; i++) {
            for (int j=0; j < ar[i].length; j++) {
                System.out.print(ar[i][j] + " ");
            }
        }
        System.out.println();

        // your code goes here!
    }
}
By using a two dimensional arrays to store the table. Then use two indexes to store the values so that I can print them out like so.   
== OUTPUT =================================================
1 2 3 4 5 6 7 8 9 10 11 12 
4 3 2 1 8 7 6 5 12 11 10 9 
9 10 11 12 5 6 7 8 1 2 3 4 
12 11 10 9 8 7 6 5 4 3 2 1 
1 5 9 2 6 10 3 7 11 4 8 12 
9 5 1 10 6 2 11 7 3 12 8 4 
4 8 12 3 7 11 2 6 10 1 5 9 
12 8 4 11 7 3 10 6 2 9 5 1 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program


public class Lab8 {
    public static void main(String[] args) {
        int ar[][] = new int[3][4];

        int n = 1;

        for (int i=0; i < ar.length; i++) {
            for (int j=0; j< ar[i].length; j++) {
                ar[i][j] = n;
                n++;
            }
        }

        // print out the elements of the array
        // left to right, top to bottom
        System.out.println("\n== OUTPUT =================================================");
        for (int i=0; i < ar.length; i++) {
            for (int j=0; j < ar[i].length; j++) {
                System.out.print(ar[i][j] + " ");
               
            }
        }
        System.out.println();
        for (int i=0; i < ar.length; i++) {
    for(int j = ar[i].length-1; j >=0; j--) {
        System.out.print(ar[i][j] + " ");
       
    }
}
        System.out.println();
        for (int i=ar.length-1; i >=0; i--) {
    for (int j=0; j < ar[i].length; j++) {
        System.out.print(ar[i][j] + " ");
       
    }
}
        System.out.println();
        for (int i=ar.length-1; i >=0; i--) {
    for(int j = ar[i].length-1; j >=0; j--) {
        System.out.print(ar[i][j] + " ");
       
    }
}
        System.out.println();
       for (int i=0; i <=ar.length; i++) {
            for (int j=0; j < ar.length; j++) {
                System.out.print(ar[j][i] + " ");
               
            }
        }
      
       System.out.println();
       for (int j=0; j < ar.length+1; j++) {
       for (int i=ar.length-1; i >=0; i--) {
   
                System.out.print(ar[i][j] + " ");
               
            }
        }
      
       System.out.println();
       for(int j = ar.length; j >=0; j--) {
       for (int i=0; i <ar.length; i++) {       
                System.out.print(ar[i][j] + " ");
               
            }}
      
       System.out.println();
       for(int j = ar.length; j >=0; j--) {
       for (int i=ar.length-1; i >=0; i--) {
           System.out.print(ar[i][j] + " ");
               
            }}
       
       
        }

    }

   

Output

== OUTPUT =================================================
1 2 3 4 5 6 7 8 9 10 11 12
4 3 2 1 8 7 6 5 12 11 10 9
9 10 11 12 5 6 7 8 1 2 3 4
12 11 10 9 8 7 6 5 4 3 2 1
1 5 9 2 6 10 3 7 11 4 8 12
9 5 1 10 6 2 11 7 3 12 8 4
4 8 12 3 7 11 2 6 10 1 5 9
12 8 4 11 7 3 10 6 2 9 5 1

Add a comment
Know the answer?
Add Answer to:
I have a table that looks like this. 1234 5678 9101112 I need to use this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For questions 10-12, refer to the following iterative code computes values for the table t. 1 pub...

    For questions 10-12, refer to the following iterative code computes values for the table t. 1 public int [] tIterative (int [C A)1 2 3 4 int n - A.length; int [] t = new int [n]; int j; for (inti-0; i 0) while (j > 0 && A[j] [1] A[i] [O]) 12 13 14 15 16 t[i]Math.max( t[i-1] , A[i][i] - A[i] [o] + t[j] ); return t; 10. Does the code for tIterative use dynamic programming? 11. What...

  • I am writing a program to play the game "Game of Life". I am hoping someone...

    I am writing a program to play the game "Game of Life". I am hoping someone can help me edit my sad program so that it will print the first board as well as the next generation. The first board is working just fine. It is the next generation that is not working and i'm not sure what I am doing wrong. Thanks in advance. public class Life {    public char FirstBoard[][];    public Life()    { FirstBoard =...

  • Java code for percolation, code compiles correctly but when it takes values for Input10.txt , the...

    Java code for percolation, code compiles correctly but when it takes values for Input10.txt , the code is suppose to comeback that the system percolates but instead it comes back with the exception. Photos below show the exception. I belive there may be an issue with my logic in the for loop for when to throw the exception. also, i have put the values of Input10.txt at the bottom to try and use to see the exception. The exception i...

  • Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...

    Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void dump(int ar[], int len) { for(int i=0; i<len; i++) { cout<<" DUMP: data = : "<< ar[i] << endl; } } void sort(int *ar, int length) { if(length == 1 || length == 0) return; cout << "BREAKING DOWN: " << endl; //for(int i=0; i<length; i++) // cout << " DUMP: DATA = : " <<...

  • print(" |",end=' ') for i in range(1,10): print(i,end=' ') print() for i in range(0,11): print('-',end=' ')...

    print(" |",end=' ') for i in range(1,10): print(i,end=' ') print() for i in range(0,11): print('-',end=' ') print() for i in range(1,10): for j in range(0,11): if j==0: print(i,end=' ') elif j==1: print('|',end=' ') elif i==9 and j==10: print(i*(j-1),end=' ') elif i>=4 and j>=4: print('*',end=' ') else: print(i*(j-1),end=' ') print() This code is not working I need help 1 2 3 4 5 6 7 8 9 --- --- -------------------- -------------------- 5 10 15 X 6 12 18 X 7 8...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • I need to create a C++ program to simulate a Round Robin Tournament. For example: if...

    I need to create a C++ program to simulate a Round Robin Tournament. For example: if a user enters 4, a 4 team tournament would output: 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 My goal is to create this program using a two dimentional array, however I am unsure how to go about doing initializing everything. How do I write a constructor for this? The following is the class declaration I...

  • Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, ...

    Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...

  • I am having problems getting the insertion sort function to output in descending order. Also, the...

    I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...

  • Consider the following attempt at a selection sort algorithm implementation in Java: 1 public static void...

    Consider the following attempt at a selection sort algorithm implementation in Java: 1 public static void selectionSort(int[] a) { 2 int n = a.length; 3 for (int i = 0; i < n - 1; i++) { 4 int smallest = i; 5 for (int j = 1; j < n; j++) { 6 if (j < smallest) { 7 smallest = j; 8 } 9 } 10 if (i != smallest) { 11 int temp = a[smallest]; 12 a[smallest]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT