Write a function called ''minandmax'' .
Function ''minandmax'' takes only one input argument that is a matrix M .
This matrix M can be any size (any number of columns and rows) with any random integer values . Function ''minandmax'' returns two output arguments.
The first output argument is called ''difference_row'' that returns a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row.
The second output argument is called ''difference_all'' that returns the difference between the maximum and minimum valued elements in the entire matrix.
NOTE: HERE THE PROGRAM IS DONE BOTH WITH C++ AND JAVA
FOR BOTH THE PROGRAM TWO OUTPUT ARGUMENTS ARE TAKEN IN ARRAY AND PASSED TO MAIN FUNCTION. THE ARRAY IN MAIN FUNCTION PRINTS BOTH OUTPUT ARGUMENTS.
IN THIS WAY TWO OUTPUT ARGUMENTS ARE RETURNED BY FUNCTION USING ARRAY TO MAIN FUNCTION.
PROGRAM IN C++
#include<iostream>
#include <stdlib.h>
#include<time.h>
using namespace std;
/* r,c as global variable */
int r,c;
/* function defined with pointer for returning array */
int * minandmax(int v[][100])
{
int difference_row[r],i,j;
/* array of pointer */
int *result;
int max=0;
int min=v[0][0];
int difference_all;
/* dynamically allocate space in result array */
result=new int[r+1];
/*Difference between the maximum and minimum valued
elements in each row.*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(max<v[i][j])
{
max=v[i][j];
}
if(min>v[i][j])
{
min=v[i][j];
}
}
/* first output argument
difference_row is a vector */
difference_row[i]=max-min;
max=0;min=v[0][0];
}
max=0;min=v[0][0];
/* Difference between the maximum and minimum valued
elements in the entire matrix */
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(max<v[i][j])
{
max=v[i][j];
}
if(min>v[i][j])
{
min=v[i][j];
}
}
}
/*The second output argument is difference_all*/
difference_all=max-min;
/* take two output arguments in a sing array named by result
*/
for(i=0;i<r;i++) result[i]=difference_row[i];
result[r]=difference_all;
/* return the two arguments difference_row vector and
difference_all with the array result*/
return result;
}
main()
{
int i,j,v[100][100];
/* pointer to array */
int *res;
/* console input */
cout<<"How many rows :";
cin>>r;
cout<<"How many columns :";
cin>>c;
/* random integers taking in matrix */
srand(time(0));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
v[i][j]=rand();
}
}
/* print matrix */
cout<<endl<<endl<<"Matrix
is"<<endl<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<v[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl;
/* store starting address of returned array in
res*/
res=minandmax(v);
/* print two desired returned values */
cout<<endl<<"Difference between the
maximum and minimum valued elements in each
row."<<endl<<endl;
for(i=0;i<r;i++)
{
cout<<res[i]<<" ";
}
cout<<endl<<endl<<"Difference
between the maximum and minimum valued elements in the entire
matrix : " << res[i];
}
SCREEN SHOT
OUTPUT
PROGRAM IN JAVA
/* import scanner class from util package */
import java.util.Scanner;
// using java.util.Random;
import java.util.Random;
class cal
{
/* function defined with array type for returning array */
public static int[] minandmax(int[][] v)
{
int r=v.length;
int c=v[0].length;
int difference_row[]=new int[r];
int i,j;
int result[]=new int[r+1];
int max=0;
int min=v[0][0];
int difference_all;
/*Difference between the maximum and minimum valued
elements in each row.*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(max<v[i][j])
{
max=v[i][j];
}
if(min>v[i][j])
{
min=v[i][j];
}
}
/* first output argument
difference_row is a vector */
difference_row[i]=max-min;
max=0;min=v[0][0];
}
max=0;
min=v[0][0];
/* Difference between the maximum and minimum valued
elements in the entire matrix */
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(max<v[i][j])
{
max=v[i][j];
}
if(min>v[i][j])
{
min=v[i][j];
}
}
}
/*The second output argument is difference_all*/
difference_all=max-min;
/* take two output arguments in a sing array named by result
*/
for(i=0;i<r;i++) result[i]=difference_row[i];
result[r]=difference_all;
/* return the two arguments difference_row vector and
difference_all with the array result*/
return result;
}
}
public class res_min_max
{
public static void main(String[] args)
{
int i,j;
int r,c;
/* pointer to array */
Scanner sc=new Scanner(System.in);
cal ob=new cal();
/* console input */
System.out.print("How many rows :");
r=sc.nextInt();;
System.out.print("How many columns :");
c=sc.nextInt();;
int v[][]=new int[r][c];
/* random class */
Random random = new Random();
/* random integers taking in matrix */
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
/* here we take any random integer
considering positive in this program */
/* so Math.abs function is used
*/
v[i][j]=Math.abs(random.nextInt());
}
}
/* print matrix */
System.out.println("\nMatrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(v[i][j]+
"\t");
}
System.out.println();
}
System.out.println();
/* store starting address of returned array in
res*/
int res[]=ob.minandmax(v);
/* print two desired returned values */
System.out.println("Difference between the maximum and
minimum valued elements in each row.\n");
for(i=0;i<r;i++)
{
System.out.print(res[i] + " ");
}
System.out.println("\n\nDifference between the maximum
and minimum valued elements in the entire matrix : " +
res[i]);
}
}
SCREEN SHOT
OUTPUT
Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a...
Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example: >> A = randi(100,3,4) A = 66 94 75 18 4 68 40 71 85 76...
1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...
Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...
The problem demonstrates the use of the random number generator to recover previously generated random numbers Write a function called randi test that takes two scalar positive integer arguments maxi and n, and retums two output arguments: a row vector of n2 elements and and n-by-n matrix. The two output arguments must contain the exact same set of random integers that fall between 1 and maxi Do this using the random number genertor, not by reshaping the data Example n,v-randi...
Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...
R assignment In the matrix() function: . The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9) e The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow FALSE. . The third argument nrow indicates...
1. Write a Lisp function called piece which takes a single argument x and implements the following piecewise linear function: piece(x) = x if 0 < x < 1 2. Write a Lisp function called intList which takes two integer arguments, a and b and returns the list of all integers from a to b (inclusive at both ends). For example, (intList 3 8) should return (345678) 1. Write a Lisp function called piece which takes a single argument x...
Write a JAVA PROGRAM with function named getData() that takes a matrix A and an integer "row" as input parameters and returns an array containing all elements in the given row of A.
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby- n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note...
Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...