
Patrick B. answered 04/10/21
Math and computer tutor/teacher
You are not going to be able to translate them EXACTLY because the java array has the length property while C does not. You must pass the dimensions of the array matrix in C. Moreover, the matrices do not have to be square. You can add , for example, TWO 5x3 arrays.
The same is true for the average() method
Next, in Java, the methods RETURN the result as an array, which is C is a memory leak.
The CALLER shall be responsible for freeing the matrix or string that is returned by these
functions which is a BAD idea and a BAD practice.
My recommendation is to change the interface and pass the following data structure:
#define MAX_MATRIX_BUFF_SIZE (2048)
// matrix data structure
typedef struct _TMatrix
{
int ** matrix;
int nRows;
int nColumns;
} * TMatrix;
void int2DArray (TMatrix, char * matrixBuff);
int mAdd(TMatrix A, TMatrix B, TMatrix sumResult);
int iN (TMatrix identityMatrix, int n);
double average (TMatrix matrix);
Finally, It is very unclear what is being done in the question2 method. Why are you doing a double mapping?
Please give examples of input and how the output array is created from these mappings.
I honestly and humbly admit I am very confused as to what is going on with that particular method.
Fortunately, I was able to get your code into a compile-able state and verify that the matrix methods
deliver as promised. Code below. However, I had to research the packages to import.
Clarify these changes are acceptable and send an email. I will then generate the C code for you.
-----------------------------------------
//Your code
import java.util.*;
import java.util.Arrays;
import java.util.stream.*;
class Jeff
{
String int2DArrayToString(int[][] input)
{
return Arrays.stream(input).map(x -> Arrays.stream(x).
mapToObj(i -> String.format(" % 6d", i))
.reduce("", (a,b) -> a + b)).reduce("", (a,b) -> a + b+'\n');
}
//Matrix Addition
int[][] mAdd (int[][]a, int[][] b)
{
if((a.length != b.length) && (a[0].length != b[0].length) ) return null;
return IntStream.range(0, a.length).mapToObj(v
-> IntStream.range(0, a[0].length).map(e
-> a[v][e] + b[v][e]).toArray()).toArray(int[][]::new);
}
// Identity matrix
int[][] iN (int n)
{
return IntStream.range(0, n).mapToObj(v
-> IntStream.range(0, n).map(e
-> (e!=v)? 0:1
).toArray()
).toArray(int[][]::new);
}
// Find the Average of a 2D array
double average (int [][]input)
{
return IntStream.range(0, input.length).mapToDouble(v ->
IntStream.range(0, input[v].length).mapToDouble(e
-> input[v][e]
).reduce(0.0, (a,b) ->a+b)/input[v].length
).reduce(0, (a,b) ->a+b)/input.length;
}
int[] question2(int[] input)
{
return IntStream.range(0, input.length).map(i
-> IntStream.range(i, input.length).map(e
-> input[i]
).reduce(0, (a,b)->a+b)
).toArray();
}
public static void main(String args[])
{
Jeff x = new Jeff();
int n=4;
int A[][] = new int[n][n];
int B[][] = new int[n][n];
for (int rowLoop=0; rowLoop<n; rowLoop++)
{
for (int columnLoop=0; columnLoop<n; columnLoop++)
{
A[rowLoop][columnLoop] = 2*(rowLoop+columnLoop)+1; //odds
B[rowLoop][columnLoop] = 2*(rowLoop+columnLoop)+2; //evens
}
}
/* -----------------------------------
cells contain {total,A-entry,B-entry}
columnLoop
rowLoop 0 1 2 3
----------------------------------------------------------
0 {0,1,2} {1,3,4} {2,5,6} {3,7,8}
1 {1,3,4} {2,5,6} {3,7,8} {4,9,10}
2 {2,5,6} {3,7,8} {4,9,10} {5,11,12}
3 {3,7,8} {4,9,10} {5,11,12} {6,13,14}
------------------------------ */
String strA = x.int2DArrayToString(A);
String strB = x.int2DArrayToString(B);
System.out.println(">"+strA+"<");
System.out.println(">"+strB+"<");
int [][] C = x.mAdd(A,B);
String strC = x.int2DArrayToString(C);
System.out.println(strC);
int [][] I4 = x.iN(4);
String strI4 = x.int2DArrayToString(I4);
System.out.println(strI4);
double mAvg = x.average(C);
System.out.println(mAvg);
int [] v = {6,9,4};
int V[] = x.question2(v);
for (int iLoop=0; iLoop<V.length; iLoop++) { System.out.print(V[iLoop]+" "); }
}
}

Patrick B.
patrick dot baldwin dot 1 at wyzant.com04/10/21

Patrick B.
I got the C code working for the matrix stuff, using the changes I suggested04/10/21

Patrick B.
I uploaded the C code to the FILES section under RESOURCES with this link as the description and title JEFF: Java to C04/10/21

Patrick B.
no need for email, click on my picture or the button labeled "ABOUT THIS TUTOR"; THen you will see another page with a button to contact the tutor04/10/21
Jeff C.
What is your email? It'll probably be clarified if I send screenshots of the questions and sample inputs and outputs04/10/21