+ 2
How would I make this a recursion?
I understand the concept of recursion but I spent hours trying to figure this out and I can't think of anything. Please help. Conditions: The findSum method can't take in any arguments and I'm only allowed to have the array field variable. public class RecursionPractice { private static int[] ary; public static void main(String[] args) { ary = new int[5]; for(int i = 1; i<=ary.length;i++){ ary[i-1] = i; } System.out.println(findSum()); } public static int findSum(){ int sum = 0; for(int i = 0;i<ary.length;i++){ sum += ary[i]; } return sum; } }
10 Réponses
+ 4
What you have tried. Can you share here so we can check what was the problem that you were facing.
+ 3
Kilowac What you have tried that code.
+ 1
A J The original program or what I came up with? Because what I have tried makes findSum take in arguments.
+ 1
A J
Sorry if its muddled, I just copied and pasted it. I just made another method to do the recursion since calcSum is not allowed to take in arguments. I'm trying so that only calcSum exists.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
public class BigCalculationsR{
private BigInteger[] bigArray;
public BigCalculations(String filename)throws IOException {
File inFile = new File(filename);
Scanner input = new Scanner(inFile);
bigArray = new
BigInteger[Integer.parseInt(input.nextLine())];
for(int i = 0;i<bigArray.length;i++){
bigArray[i] = (BigInteger)input.nextBigInteger();
}
input.close();
}//End constructor
public BigInteger calcSum(){
int i = 0;
BigInteger sum = new BigInteger("0");
return calcSumR(i,sum);
}//End calcSum
private BigInteger calcSumR(int i, BigInteger sum){
if(i<bigArray.length){
sum = sum.add(bigArray[i]);
i++;
return calcSumR(i,sum);
}
return sum;
}//End calcSumR
}//End class
+ 1
***
The whole reason is to get bonus points for using recursion. My original program works no problem.
What I posted is not the entire code, the conditions are:
One field variable of BigInteger array
One constructor that takes in a string for the filename, creates a file and scanner object, initialize the array length (which is the first int of the file) and fills in the array attribute.
This class holds three methods.
One method called calcSum that takes in no arguments and returns a BigInteger. This method finds the sum of the array. Bonus points for using recursion.
One method called findMax that takes in no arguments and returns a BigInteger. This method finds the biggest BigInteger in the array. Bonus points for using recursion.
One method called contains that takes in one BigInteger called "other" and returns a boolean. This method compares "other" to the values in the array. Bonus points for using recursion.
I don't know if im allowed to make more methods, but its assumed im not.
+ 1
***
If im allowed to make more methods then im fine i know how to do it, but Im not sure if im allowed to so Im trying to do it without making more methods
also the main method just gives the filename for the object, other than that it makes no difference in the methods, it just calls the methods. and the array takes in whatever values is in the file, as long as it can be a BigInteger. the first number of the file determines the size of the array.
+ 1
「HAPPY TO HELP」
In the if statement I think it should just be irec < 0 instead of i <= 0, otherwise it wouldnt get index 0. Thank you for showing me a another way to do this I really appriciate it! However, my problem is that im only allowed to have one field variable :^(. Also I didn't know return could be used that way for a void return type, always happy to learn new things.
+ 1
***
That solution is ingenious. I also think the restrictions are ridiculous as well, my professor set those. Thank you so much for the help!
+ 1
Girish Patel
Hey, I appriciate the help but I already understand recursions. My problem was the restrictions my professor set for me where the findSum method is not allowed any parameters/arguments and I'm only allowed to have the field variable array. *** gave me the answer I needed and my program is all good now. I really appriciate the response though :^).
0
In recursive method we need to do four things
1. Return type
2. Use parameters or arguments
3. Add a condition in method to stop recursion.
4. Method self calling in at return.
So you can write above code in that way.
public class RecursionPractice {
private static int[] ary;
public static void main(String[] args) {
ary = new int[5];
for(int i = 0; i < ary.length; i++){
ary[i] = i;
}
// method calling with argument.
System.out.println(findSum(ary.length, ary));
}
public static int findSum(int len, int[] arr) {
// Condition to stop recursion.
if(len == 0){
return 0;
}
// selfcalling
return arr[len-1] + findSum(len-1, arr);
}
}
https://code.sololearn.com/cPVA4unvZJIb/?ref=app