Question

In: Computer Science

From the MathWorks Cody Challenges (Problem 9). You have a matrix for which each row is...

From the MathWorks Cody Challenges (Problem 9).

You have a matrix for which each row is a person and the columns represent the number of quarters, nickels, dimes, and pennies that person has (in that order). What is the row index of the person with the most money?

Note for those unfamiliar with American coins: quarter = $0.25, nickel = $0.05, dime = $0.10, penny = $0.01.

Example:

Input a = [1 0 0 0; 0 1 0 0]
Output b = 1

since the first person will have $0.25 and the second person will have only $0.05.

TASKS:

  1. Download the attached Live Script file, Cody_009_Most_Change_stub.mlx, which has the basic setup for the problem.
  2. Write a function most_change() which takes as its input a matrix and returns two entities: (1) M, the maximum value of the coins held by a single player; and (2) b, the row number of the player with the maximal value. You will write two versions of this function to compare the speed of using loops to the speed of using matrix operations (put only one version in the live script at a time).

a. The first version is to be done without the use of matrix multiplication; instead, use for loop(s) to find the total value of the coins held by each player. Pseudocodefor this version:

%% Initialize variables for (1) most money seen so far (say, M = -1), and 
%% (2) which was the first player to have that amount (say, b = 0).
 
%% Use the size() function to find the total number of players (number of rows in matrix a)
 
%% For each player:
%%    find the value of all their change
%%    if that value is the largest seen so far,
%%       update the most money seen so far 
%%       and the first player to have that amount.
%% Return with the maximum value found and which player had that amount

Solutions

Expert Solution

import java.util.*;

class max

{

public static void main(String[] args)

{

Scanner sc=new Scanner (System.in);

System.out.println("Ënter the no of persons you want to take as input");

int n=sc.nextInt();

int i,j,l;

int arr[][]=new int[n][4];

//taking array as input

for(i=0;i<n;i++)

{

for(j=0;j<4;j++)

{

arr[i][j]=sc.nextInt();

}

}

l=arr.length;

double ans[]=new double[2];

ans=most_change(arr,l);

System.out.println("max value="+ans[0]);

System.out.println((int)ans[1]+"person has the max amount");

}

private static double[] most_change(int[][] arr, int l) {

// TODO Auto-generated method stub

double ans[]=new double[2];

int i,j;

double value=0;

double max=-1;

int p=-1;

for(i=0;i<l;i++)

{

value=0;

for(j=0;j<4;j++)

{

if(j==0)

value+=(double) (arr[i][j]*0.25);

if(j==1)

value+=(double) (arr[i][j]*0.05);

if(j==2)

value+=(double) (arr[i][j]*0.10);

if(j==3)

value+=(double) (arr[i][j]*0.01);

}

if(value>max)

{

max=value;

p=i+1;

}

}

ans[0]=max;

ans[1]=p;

return(ans);

}

}

Since no language was mentioned, I have written the code in java I could not use size function... If you want to write in some other language you may use that function.This code is fully compiled and executed with no errors.


Related Solutions

The following matrix is in reduced row echelon form. Decode from the matrix the solution of...
The following matrix is in reduced row echelon form. Decode from the matrix the solution of the corresponding system of linear equations or state that the system is inconsistent. (If the system is dependent assign the free variable the parameter t. If the system is inconsistent, enter INCONSISTENT.) 1 0 5 −4 0 1 −8 10 0 0 0 0 (x1, x2, x3) =
Consider a 9 × 9 Sudoku, where each number appears exactly once in each row and...
Consider a 9 × 9 Sudoku, where each number appears exactly once in each row and exactly once in each column. Assume that a list of numbers already appear in some cells are given. (a) (20pts) Write an optimization model for Sudoku. (b) (10pts) Create your own initial list of numbers (1 through 9) which appear in some cells of the table and use this as your input data to solve the optimization problem in Part (a) using an optimization...
Suppose that B is a 12 × 9 matrix with nullity 5. For each of the...
Suppose that B is a 12 × 9 matrix with nullity 5. For each of the following subspaces, tell me their dimension, along with what value of k is such that the subspace in question is a subspace of R k . (For example, a possible – though incorrect – answer is that Col B is a subspace of R 2 .) So, you’ll need eight answers for this problem (two answers for each of the four parts). • Col...
How the country to which you have been assigned is impacted by population challenges such as...
How the country to which you have been assigned is impacted by population challenges such as growth, aging, migration, and urbanization. How the country to which you have been assigned is contributing to or helping to resolve population challenges such as growth, aging, migration, and urbanization.
Problem 3: Find the equilibrium distribution for each transition matrix. a) 1/2 1/9 3/10 1/3 1/2...
Problem 3: Find the equilibrium distribution for each transition matrix. a) 1/2 1/9 3/10 1/3 1/2 1/5 1/6 7/18 1/2 b) 2/5 0   3/4 0 2/3 1/4 3/5 1/3 0 Problem 4: For either transition matrix in problem 3, find the other two eigenvalues with corresponding eigenvectors.
Problem 3: Find the equilibrium distribution for each transition matrix. a) 1/2 1/9 3/10 1/3 1/2...
Problem 3: Find the equilibrium distribution for each transition matrix. a) 1/2 1/9 3/10 1/3 1/2 1/5 1/6 7/18 1/2 b) 2/5 0   3/4 0 2/3 1/4 3/5 1/3 0 Problem 4: For either transition matrix in problem 3, find the other two eigenvalues with corresponding eigenvectors.
What challenges or opportunities have you identified in developing your business and analytics problem statements?
What challenges or opportunities have you identified in developing your business and analytics problem statements?
Briefly describe each of the four major challenges that Netflix faces. Which of these four challenges...
Briefly describe each of the four major challenges that Netflix faces. Which of these four challenges will be the most difficult to address? Why? Which challenge will be the easiest to address? Why?
1. if you think of the extracellular matrix (ECM) as a fiber-reinforced matrix, which of the...
1. if you think of the extracellular matrix (ECM) as a fiber-reinforced matrix, which of the following are the fiber forming elements? (choose as many as apply) a. elastin b. glycoproteins c. proteoglycans d. collagen 2.if you had to perform an experiment where you had to evaluate cytotoxicity, which of the following assays would be options to consider. (choose as many as apply) a. western blot b. PCR c. MTS assay d. RT-PCR e. MTT assay f. northern blot g....
Please create a matrix A by extract the first 9 entries(first 9 rows) from Ownership(income&lot size)...
Please create a matrix A by extract the first 9 entries(first 9 rows) from Ownership(income&lot size) and first 9 entries(first 9 rows) from Non ownership(income& lotsize) by using matlab from the table below: Income Lot_Size Ownership 60 18.4 owner 85.5 16.8 owner 64.8 21.6 owner 61.5 20.8 owner 87 23.6 owner 110.1 19.2 owner 108 17.6 owner 82.8 22.4 owner 69 20 owner 93 20.8 owner 51 22 owner 81 20 owner 75 19.6 non-owner 52.8 20.8 non-owner 64.8 17.2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT