2.1k views
1 vote
Write a recursive method that takes four inputs: a 1-D array of Strings (i.e., exams), the length of this 1-D array (i.e., n), an index to access array elements (i.e., i) and a 2-D array containing double values (i.e., ret).

1 Answer

1 vote

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args)

{

String [] exams = {"1;2;3;4;" , "5;6;7;8;" , "9;10;11;12;" };

double[][] n = new double[exams.length][4];

examArray = convert(exams , exams.length , 0 , n);

for(int i = 0 ; i < exams.length ; i++)

{

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

{

System.out.print(n[i][j] + "\t");

}

System.out.print("\\");

}

}

/* recursive function convert defined here */

public static double[][] convert(String[] exams , int number , int x , double [][] result)

{

if(x==number)

return result;

else

{

String total = exams[x];

String currentScore = "";

int p = 0;

for(int j = 0 ; j<totalScore.length() ; j++)

{

char ch = total.charAt(j);/

if(ch==';')

{

result[x][p] = Double.parseDouble(currentScore);

p++;

currentScore = "";

}

else

{

currentScore = currentScore + Character.toString(ch);

}

}

return convert(exams , number , x+1 , result);

}

}

}

Step-by-step explanation:

The Java source code defines a recursive function or method called convert that converts the results of three students that sat for four examinations to double or float number and screens their individual results for the four exams on the screen.

User Majocha
by
4.6k points