Question

Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point...

Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parameters row and col), set an element (given parameters row, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have taken the liberty to create and initialize 2 arrays of dimensions 2x2 in the class because I inferred from the question that the arrays must be initialized by the programmer within the class itself. If you want arrays to have user-defined rows, columns, and elements, do comment!

Array is a keyword in scala. So it will throw errors if we use it as a class name. So our class is called Arrayx.

Nm 1. class Arrayx{ var array1 = Array.ofDim[Float](2,2) // We Create our multidimensional array var array2 = Array.ofDim[FloCPU Time: 17.84 sec(s), Memory: 182880 kilobyte(s) executed in 10.039 sec(s) 2D ARRAY is: 1.0 2.0 3.0 4.0 Element at [1][1] i

class Arrayx{
var array1 = Array.ofDim[Float](2,2) // We Create our multidimensional array
var array2 = Array.ofDim[Float](2,2)
array1 = Array(Array(1,2), Array(3,4)) // We initialise our multidimensional array
array2 = Array(Array(5,6), Array(7,8))
var array3 = Array.ofDim[Float](2,2)
  
def displayArray(){
print("2D ARRAY is: ")
println()
for(i<- 0 to 1)
{ // Traversing elements using loop
for(j<- 0 to 1)
{
print(array1(i)(j) + " ")
}
println()
}
}
  
def getElement(a:Int, b:Int){
print("Element at [" + a + "][" + b + "] " + "is: ")
print(array1(a)(b)) //print the element at i row and j coloumn
println()
  
}
def setElement(a:Int, b:Int, value:Int){
array1(a)(b) = value //set the element at i row and j coloumn to user defined value
print("New 2D ARRAY is: ")
println()
displayArray()
  
}
  
def sumOfArrays(){
for(i<- 0 to 1)
{
for(j<- 0 to 1)
{
array3(i)(j) = array1(i)(j)+array2(i)(j) //addition of i and j element of both rows
}
}
print("The sum of arrays is:")
println()
for(i<- 0 to 1)
{   
for(j<- 0 to 1)
{
print(array3(i)(j) + " ")
}
println()
}
}
  
}
  
object MainObject{
def main(args:Array[String]){
var a = new Arrayx()
a.displayArray()
a.getElement(1,1)
a.setElement(1,1, 22)
a.sumOfArrays()
}
}


Add a comment
Know the answer?
Add Answer to:
Programing in Scala: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point...
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
  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • Assignment Input from the user 9, 64-bit, floating-point values as a 3x3, row-major, multi-dimensional array. This...

    Assignment Input from the user 9, 64-bit, floating-point values as a 3x3, row-major, multi-dimensional array. This will be the 3x3 matrix. Then, input 3, 64-bit, floating-point values. This will be a single-dimensional array and is the vector. Your program will simply ask the user to enter matrix values. Remember, there are 9 of these, but they need to be stored into a 3x3 multi-dimensional array (row-major). Then, your program will ask for the vector values, which will be stored into...

  • Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...

    Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...

  • need help with this python progam using numpy Create a 4x4 two dimensional array with numbers...

    need help with this python progam using numpy Create a 4x4 two dimensional array with numbers from 1 thru 16. show this then: Change the last element by dividing it in half. Show that the original array has changed. show this then: Set all values in row 2 to zero. Show the original array contains this change show this then: Set all values in column 1 to one. Shoe the original array contains this change. show this then: Display the...

  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • Create an application that instantiates a 20?20 two-dimensional array of Date objects, populates it with random...

    Create an application that instantiates a 20?20 two-dimensional array of Date objects, populates it with random dates drawn from the range of 1/1/1970 to 12/31/2020, and then outputs the index and actual date of the row with the most recent date (relative to today) among all the rows and the index and actual date of the column with the oldest date (closest to 1/1/1970) among all the columns.

  • This is for JAVA programming. Create a test class that contains two arrays and two methods:...

    This is for JAVA programming. Create a test class that contains two arrays and two methods: - The first array has 3 rows and 3 columns and is initialized with type double data - The second array has 4 rows and 4 columns and is also initialized with type double data - The first method ArraysRows will receive an array of type double as an argument and then print out the sum and average of all elements in each ROW....

  • /** * Class that defines a method which performs matrix addition. This gives students their first...

    /** * Class that defines a method which performs matrix addition. This gives students their first practice at creating and using * the code needed for higher dimensional arrays. * * @author Matthew Hertz */ public class MatrixAddition { /** * Given two equally-sized matrices, return a newly allocated matrix containing the result of adding the two.<br/> * Precondition: a &amp; b must have the same number of rows and each row will have the same number of columns. You...

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