Jeff C.

asked • 04/09/21

Convert the following methods from Java to C and then write a function to test each output with at least 4 test cases to see if the method work correctly.

//Convert an 2D array to a string

    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();
    }

If a test pass, then you need to state that it has passed in a green color, however if it fails then you need to display that it has failed in a red color.

1 Expert Answer

By:

Patrick B. answered • 04/10/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Jeff C.

What is your email? It'll probably be clarified if I send screenshots of the questions and sample inputs and outputs
Report

04/10/21

Patrick B.

patrick dot baldwin dot 1 at wyzant.com
Report

04/10/21

Patrick B.

I got the C code working for the matrix stuff, using the changes I suggested
Report

04/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 C
Report

04/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 tutor
Report

04/10/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.