homeworkstudyhelp

Our Services

Get 15% Discount on your First Order

Traversing 2D Arrays Traditional For Loop assignment 1 When…

Traversing 2D Arrays

Traditional For Loop

assignment 1

When traversing a 2D array a nested loop is required. The first or outside For loop will traverse the rows and the second or inside For loop will traverse the columns.

 

 

We say “for every row, for every column of every row, do something.”

 

Enhanced For Loop (for-each Loop)

 

2D Arrays can also be traversed using the enhanced for loop (or for-each loop). In this traversal, the data type in the first loop is a 1D array of the same type as the 2D array, and the second loop is of the same type as the elements in the 2D array, double for this example.

 

This can only be used to access this 2D array, arr2. The enhanced loop cannot change the array that it is traversing.

 

2D array processing is no more difficult than 1D array processing. Getting a handle on the syntax of the nested loops is the key. One of the four free-response questions on the AP exam will be writing code to process a 2D array. So start practicing writing programs using 2D arrays!

 

Try It Out!

 

Using the TwoDArray program in the right panel, complete the following tasks. Add the code where indicated in the starter program.

 

  1. Write code to print the box 2D array in the following format:
    1 2 3
    4 5 6
    7 8 9
  2. Write code to print the box 2D array using row-major ordering. Add the code where indicated in the starter program.
    1 2 3 4 5 6 7 8 9
  3. Write code to print the box 2D array using column-major ordering. Add the code where indicated in the starter program.
    1 4 7 2 5 8 3 6 9
  4. Write code to print the arr2 2D array printing the row and column value as well as the element. Make sure to put the code below where the arr2 2D array is declared.
    loc 0, 0: 1.1
    loc 0, 1: 2.2
    loc 0, 2: 3.3
    etc.
  5. Write code to print the arr2 2D array using a for-each loop:
    1.1 2.2 3.3
    4.4 5.5 6.6

Whenever you want to compile, run and test your program, click the Compile and Run button below. The output will display in the terminal panel.
 

COMPILE AND RUN

 

 

TwoDArray.java
public class TwoDArray

{

    public static void main(String[] args)

    {

   

        // 2D Array Creation

       

        int[][] box = {{1,2,3}, {4,5,6}, {7,8,9}};

       

        //add the code for #1 below

       

 

       

       

        // Row Major Ordering

        //add the code for #2 below

       

 

        // Col Major Ordering

        //add the code for #3 below

       

   

       

       

        // Array of Arrays

        double[] row1 = {1.1, 2.2, 3.3};

        double[] row2 = {4.4, 5.5, 6.6};

        double[][] arr2 = {row1, row2};

 

        //add the code for #4 below

       

       

       

        //add the code for #5 below

       

   

    }  

       

}

2nd assignment
1.In the space below, create 3 different 2D arrays of primitive data types of different sizes. Use an initializer list for one of your arrays.

 

2.Read the following code segment. What will be printed as a result of running the code?

String[][] arr = new String[2][2];
arr[0][0] = "1";
arr[0][1] = "please";
arr[1][0] = "2";
System.out.println(arr[0][0] + " " + arr[0][1]);
System.out.println(arr[1][0] + " " + arr[1][1]);

Write the output of the code segment below.
 

3. Fill in the blanks.

For an array that is created with this code:

 

double[][] arr = new double[i][j];

and is ordered in row-major ordering, the array will have  ____ rows and ______

 columns.

 

4. In the space below, write the code to create 2 different 1D arrays. Then create a 2D array using these two arrays.

5. Read through the following code segment. If the code segment has no errors, write what the output of the code segment would be in the space below. If the code segment has errors, describe what changes should be made to correct those errors.

double[][] arr = new double[3][5];
for(int r = 0; r < 5; r++) 
{
    for(int c = 0; c < 3; c++) 
    {
        System.out.println(r + ", " + c + ": " + arr[r][c]);
    }
}

 

 

 

 

assignment 3

Analyzing Radioactivity in a Grid

A team of researchers has approached you to help them analyze the data that they collected as quickly as possible. They took several measurements of radioactivity (measured in rem) and wrote down the measurements and where they were taken on a grid. They are trying to narrow down the exact location of where the radioactivity is coming from so that they can warn people to avoid these areas. They are still unsure why this area is showing high levels of radioactivity and need your help to solve this environmental crisis.

 

Pair Programming

 

In the right panel is the Radioactivity starter code.

 

You and your partner have been tasked with the following problems. You are required to use the for-each loop for at least one of the three problems listed below.

 

  1. Write the code to print out the grid with all the values shown and with appropriate spacing. Make sure it is formatted for easy readability.

 

  1. Write the code to identify the largest measurement of radioactivity. Print out the row and column in the following format:”The max radiation is found at coordinates: [row], [col]”

 

 

  1. Write the code to identify all areas that have a measurement of 50rem or higher. Any exposure to these high levels at once can be lethal without medical treatment. Print out the information in the following format:”Location [row], [col] has more than 50rem.”

 

 

When you are ready to compile, run and test your program, click the Compile and Run button to run the program. The output will display in the terminal panel.
 

COMPILE AND RUN

 

To clear the terminal panel, type “clear” and press Enter in the terminal window.

 

 

Radioactivity.java

public class Radioactivity

{

    public static void main(String[] args)

    {

   

        double[][] grid = {{45, 36, 17, 35, 4, 38},{25,32,60,43,60,1},

                           {50,4,65,42,2,26},{36,24,3,43,41,11}};

 

        //add your code below

       

       

       

  }

}

 

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Retail Price Calculator using C#   Create an application that lets…

Retail Price Calculator using C#   Create an application that lets the user enter an item’s wholesale cost and its markup percentage. It should then display the items retail price. For example: If an item’s wholesale cost is $5.00 and its markup percentage is 100 percent, then the item’s retail

CSC521 Programming Project 3 A Stack-based Calculator With Postfix…

CSC521 Programming Project 3 A Stack-based Calculator With Postfix Expression Using Java Purpose • To make a simple integer calculator for 8-bit signed numbers (i.e. accept 12*4*(5-3) = and produce an answer). • To see how the stack can be used to support arithmetic operations. • Note: Groups of at

/* * Part 1: Light * A class which represents a single light bulb….

/** Part 1: Light* A class which represents a single light bulb.For this assignment, you will be writing a class called Light that represents asingle light bulb.Your job is to add code to Light.java so that your implementation meets therequirements specified below.To complete the assignment, replace all the /* missing

  Javascript Program writing Task A   1.1 As a JavaScript…

  Javascript Program writing Task A   1.1 As a JavaScript  programming student ,I want to establish  code with   a variable `isHappy` and assign it a boolean value based on the value of the given String variable `action`, whether it is `Smile` or not.    1.2 As a JavaScript student,

import java.util.Arrays; import java.util.Random; public class…

import java.util.Arrays; import java.util.Random; public class Board { private boolean[][] board /** * Construct a puzzle board by beginning with a solved board and then * making a number of random moves. That some of the possible moves * don’t actually change what the board looks like. * * @param

Implement the  programs in ARM assembly. implement them first in…

Implement the  programs in ARM assembly. implement them first in JAVA and translate the logic into assembly Implement a program to calculate multiplication using successive addition with recursion. For example, 5×4 is 5+5+5+5.  This can be defined recursively as:             Mult(m, n) = if n is 1,  return m                               

file named A10.java. Place all your code in this file. Create…

file named A10.java. Place all your code in this file. Create public static ArrayList of integers named intList. Define public static method addNumber (int i) that adds i to your list. Create public static HashMap of integer, integers named int Map. Define public static method add Pair (int i, int

3.1.2 – Using nslookup for Passive Reconnaissance What is NSLookup?…

3.1.2 – Using nslookup for Passive Reconnaissance What is NSLookup? Try to do NSLookup on a different website and record your answer. How is Nslookup beneficial? Which part of the ethical hacking methodology does this relate to? 3.1.3 – Using ipconfig for passive reconnaissance What is ipconfig used for? After

Help me with Java   MovieStack Your are tired of asking your…

Help me with Java   MovieStack Your are tired of asking your “friend” to watch the movies you like. So you want to give your friend a stack of movies to watch, representing a watchlist. Let’s automate this process by creating a new data structure class called MovieStack, which should

Question 1: The difference between head and tail recursion? Does…

Question 1: The difference between head and tail recursion? Does JAVA support it? What are the advantages and disadvantages of each?Question 2: Describe, in detail, what are the “Traversal of Binary Trees” (hint: In-order Traversal, Pre-order Traversal, Post-order Traversal):

I need help writing a Java class StringSet .  A StringSet object…

I need help writing a Java class StringSet.  A StringSet object is given a series of up to 15 String objects.  It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series.   The StringSet class has the following specification:

What is the purpose of comments and whitespaces in Java? What are…

What is the purpose of comments and whitespaces in Java? What are the Java statements and logic necessary to produce a Java program that correctly prompts users to enter a username and password? Consider that the username is not case-sensitive, but the password is. The user needs to correctly type