646 views
5 votes
3-Write a program in some language that has both static and stack dynamic local variables in subprograms. Create six large (at least 100 * 100) matrices in the subprogram—three static and three stack dynamic. Fill two of the static matrices and two of the stack-dynamic matrices with random numbers in the range of 1 to 100. The code in the subprogram must perform a large number of matrix multiplication operations on the static matrices and time the process. Then it must repeat this with the stack-dynamic matrices. Compare and explain the results. (at least 5 lines)

User Inodb
by
3.6k points

2 Answers

4 votes

Answer:

See explaination

Step-by-step explanation:

Code below:

import java.util.Random;

public class Matrices {

static int staticMatrix1[][] = new int [100][100];

static int staticMatrix2[][] = new int [100][100];

static int staticMatrix3[][] = new int [100][100];

public void localMatrices() {

int localMatrix1[][] = new int [100][100];

int localMatrix2[][] = new int [100][100];

int localMatrix3[][] = new int [100][100];

Random r = new Random();

for(int i =0;i<100;i++) {

for(int j =0;j<100;j++) {

localMatrix1[i][j] = r.nextInt(100);

localMatrix2[i][j] = r.nextInt(100);

}

}

long localStart = System.nanoTime();

multiplyLocal(localMatrix1, localMatrix2);

long localEnd = System.nanoTime();

System.out.println("Time taken for local multiplication: "+ (localEnd-localStart)/ 1000000);

}

public void multiplyLocal(int first[][],int second[][]) {

int[][] multiply = new int [100][100];

for (int c = 0; c < 100; c++)

{

for (int d = 0; d < 100; d++)

{

int sum = 0;

for (int k = 0; k < 100; k++)

{

sum = sum + first[c][k]*second[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

}

public static void multiplyStatic(int first[][],int second[][]) {

int[][] multiply = new int [100][100];

for (int c = 0; c < 100; c++)

{

for (int d = 0; d < 100; d++)

{

int sum = 0;

for (int k = 0; k < 100; k++)

{

sum = sum + first[c][k]*second[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

}

public static void main(String args[]) {

Random r = new Random();

for(int i = 0;i<100;i++) {

for(int j = 0;j<100;j++) {

staticMatrix1[i][j] = r.nextInt(100);

staticMatrix2[i][j] = r.nextInt(100);

}

}

long staticStart = System.nanoTime();

multiplyStatic(staticMatrix1, staticMatrix2);

long staticEnd = System.nanoTime();

System.out.println("Time taken for static multiplication: "+ (staticEnd-staticStart)/ 1000000);

Matrices matrices = new Matrices();

matrices.localMatrices();

}

}

User Yakusha
by
4.6k points
4 votes

Answer:

Check the explanation

Step-by-step explanation:

Code:

****************************************

import java.util.Random;

public class Matrices {

static int staticMatrix1[][] = new int [100][100];

static int staticMatrix2[][] = new int [100][100];

static int staticMatrix3[][] = new int [100][100];

public void localMatrices() {

int localMatrix1[][] = new int [100][100];

int localMatrix2[][] = new int [100][100];

int localMatrix3[][] = new int [100][100];

Random r = new Random();

for(int i =0;i<100;i++) {

for(int j =0;j<100;j++) {

localMatrix1[i][j] = r.nextInt(100);

localMatrix2[i][j] = r.nextInt(100);

}

}

long localStart = System.nanoTime();

multiplyLocal(localMatrix1, localMatrix2);

long localEnd = System.nanoTime();

System.out.println("Time taken for local multiplication: "+ (localEnd-localStart)/ 1000000);

}

public void multiplyLocal(int first[][],int second[][]) {

int[][] multiply = new int [100][100];

for (int c = 0; c < 100; c++)

{

for (int d = 0; d < 100; d++)

{

int sum = 0;

for (int k = 0; k < 100; k++)

{

sum = sum + first[c][k]*second[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

}

public static void multiplyStatic(int first[][],int second[][]) {

int[][] multiply = new int [100][100];

for (int c = 0; c < 100; c++)

{

for (int d = 0; d < 100; d++)

{

int sum = 0;

for (int k = 0; k < 100; k++)

{

sum = sum + first[c][k]*second[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

}

public static void main(String args[]) {

Random r = new Random();

for(int i = 0;i<100;i++) {

for(int j = 0;j<100;j++) {

staticMatrix1[i][j] = r.nextInt(100);

staticMatrix2[i][j] = r.nextInt(100);

}

}

long staticStart = System.nanoTime();

multiplyStatic(staticMatrix1, staticMatrix2);

long staticEnd = System.nanoTime();

System.out.println("Time taken for static multiplication: "+ (staticEnd-staticStart)/ 1000000);

Matrices matrices = new Matrices();

matrices.localMatrices();

}

}

***************************************

Outputs:

Time taken for static multiplication: 6

Time taken for local multiplication: 12

******************************************

User Chrono
by
4.4k points