ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 2 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 03 May 2017 My Price 11.00

Write a function called float dotproduct

I have attached the questions. please let me know if you need anything else

 

 

Question 1.
Write a function called float dotproduct (Links to an external site.)(float a, int float b,
int n ), the input are two arrays of the same length n where n could be arbitrary. The
entries of the arrays a and b are each positive and strictly less than one. So no
overflows are possible.
30 points Please submit these with test cases of arrays of length 3. Storage Organization for 2D Matrix in C
Logical Layout Physical Layout Figure 1: Memory Layout for 2D Matrix in C
When the caller function passes a 2D matrix as a parameter in C language, a double C
pointer (**A) is used to pass only the address of the matrix. A is the address of an array
of pointers. Element A[i] of this array is the address to the row number i of the 2D
matrix. A 2D matrix is represented as a collection of its rows, with each row just being a
one-dimensional array. We call this type of organization row-major order (Links to an
external site.). Two pointer dereferences are needed to access each element in a
matrix. That is, to access A[2][1] (at location (2, 1) of the matrix) we first dereference
pointer A to indicate the beginning of row number 2 (=A[2]), and dereference that pointer
(A[2]) to the element (A[2][1]).
Declaration of 2D Matrices in Data Segment
The following code shows declaration of 4x4 matrices with initial values in row-major
order. Use this code for testing a transpose function. .data
.align 2
boundary # Request alignment on word (4 byte) ## 4x4 Matrix Declaration ##
A0: .word 41, 42, 43, 44 # Declare and initialize 1st Row of A A1: .word 55, 56, 57, 58 # Declare and initialize 2nd Row of A A2: .word 19, 10, 11, 12 # Declare and initialize 3rd Row of A A3: .word 23, 24, 25, 26 # Declare and initialize 4th Row of A A:
rows .word A0, A1, A2, A3 # Declare and initialize the pointer to the NUMROWS_A: .word 4 # the number of rows NUMCOLS_A: 4 # the number of columns .word ## 4x4 Matrix Declaration ##
T0: .word 0, 0, 0, 0 # Declare and initialize 1st Row of T T1: .word 0, 0, 0, 0 # Declare and initialize 2nd Row of T T2: .word 0, 0, 0, 0 # Declare and initialize 3rd Row of T T3: .word 0, 0, 0, 0 # Declare and initialize 4th Row of T T:
rows .word T0, T1, T2, T3 # Declare and initialize the pointer to the NUMROWS_T: .word 4 # the number of rows NUMCOLS_T: 4 # the number of columns .word Array A and T are defined in row-major order in the data segment. Not that the first
dimensional elements of array A and T store addresses of the corresponding one
dimensional arrays. To get the addresses of the second dimensional arrays, we use lw
instructions, not la instruction
This example shows how to access A[i][j].
# Assume i is stored in register $t6 and j in register $t7 la $t1, A # Load address of A into
register $t1 sll $t2, $t6, 2 # Shift left twice (same as i * 4) add $t2, $t2, $t1 # Address of
pointer A[i] lw $t3, ($t2) # Get address of an array A[i] and put it into register $t3 sll $t4,
$t7, 2 # Shift left twice (same as j * 4) add $t4, $t3, $t4 # Address of A[i][j] lw $t0, ($t4) #
Load value of A[i][j]
so similarly, you can write a function/procedure float readArray(float A, int i, int j) that
reads the element A[i][j] of an array(returns it) and a procedure writeArray(float A, int
i, int j, float value) that stores value in A[i][j] or effectively performs A[i][j]=value;
remember all indexes start at 0 like,C/C++,Java and Python.
Question 2
Suppose we have the following description above for an n x n array A, write a procedure
in MIPS Assembly that performs the transpose of the matrix A and stores it in another
matrix B of the same size in the .data segment.
The C code for this would be:
matrix_transpose(float A,float B , int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
B[j][i]=A[i][j] ;
} }
}
Use a few 3X3 arrays as test cases to make sure your code is correct. Remember in
general your code should work for any arbitrary array size.
Question 3
Given 2 1 dimensional arrays a and b of length n and a single 2-dimensional array M of
size nxn where n could be arbitrary assume the .data segment is as before as we have
described above how to make 2-dimensional arrays. Compute the matrix-vector
product (Links to an external site.) of vector a and matrix M and store the result in the
array b. It is given by the following simple C code:
void matrix_vector_product(float M, float a, float b, int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
b[i] += A[i][j] *a[j];
}
}
}
Again use a few test cases with 3X3 matrices and vectors of length 3.

Attachments:

Answers

(11)
Status NEW Posted 03 May 2017 01:05 AM My Price 11.00

-----------

Attachments

file 1493776550-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)