is the question to construct an array of calculated as follows: à ¢Ã¢â€š ¬ ¦ +
ID: 3535833 • Letter: I
Question
is
the question
to
construct an array of
calculated
as follows:
à ¢Ã¢â€š ¬ ¦ +
value[i]
sum
of the elements in the
the
array of aggregates is the
have
the same length. Write a
aggregate[]. Then it prints out
two
functions:
aggregate)
computes
aggregate[i]. It does not
its
main body, since whatever
this
function recursive: think how
aggregate[] and a
you
use before you use it.
FOR
ALLTHE FUNCTIONS, 1 POINT WILL BE GIVEN FOR LAZY INPUT
i,
int[] value, int [] aggregate) {
if (i == 0)
return aggregate [0]
=
value[0];
else
{
compute(i-1,
value,
aggregate);
return aggregate [i]
=
value[i] + aggregate[i-1];
}
void
print(int [] array, int[] arrayT) {
//make a for loop that will print array and
arrayT
using compute method above.
main(String[]
args) {
int[]
value = ????? //assign hard coded values to this
int[]
aggregate = ???? //make new array with same length as value
array
//use
print method using value and aggregate as variables
Explanation / Answer
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int[] values = {1,2,3,4,5,6,7,8,9,10};
int[] aggregate = new int[10];
compute(9,values,aggregate);
print(aggregate);
}
public static void compute(int i, int[] value, int[] aggregate)
{
if(i==0) aggregate[0] = value[0];
else
{
compute(i-1, value, aggregate);
aggregate [i] = value[i] + aggregate[i-1];
}
}
public static void print(int[] array)
{
for(int i=0; i<10; i++)
System.out.print(" " + array[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.