Frequency recursion
Page 1 of 113 Replies - 605 Views - Last Post: 23 March 2012 - 06:08 AM
#1
Frequency recursion
Posted 21 March 2012 - 03:50 PM
I'm having difficulty finding where to start on this project. My prof. wants us to write a static recursive method named frequencyCount(n,d) that returns the frequency of occurrence of a particular digit d in an integer n. For example, frequencyCount(1342457,4) returns 2, but frequencyCount (1342457,6) returns 0. We can't use local or static variables.
Replies To: Frequency recursion
#2
Re: Frequency recursion
Posted 21 March 2012 - 03:57 PM
What's the difficulty? In coding or logic? Surely, you can figure out an algorithm to count the number of occurrences. If it's a coding issue, lets see what you've tried so far.
#3
Re: Frequency recursion
Posted 21 March 2012 - 04:04 PM
I haven't tried anything yet because I don't know where to start. I'm thinking that I have to make it so it stores the digits into an array and then compare the other number to the numbers in the array. I'm not sure how to do that though.
#4
Re: Frequency recursion
Posted 21 March 2012 - 04:12 PM
Ok, that's a start. Note that you could concatenate the integer with the empty string and use String.charAt and avoid the array altogether, or you could chop the integer down using division and modulus and examine each digit to avoid using strings. The former approach is easier.
So you got your algorithm. The only thing left to do is code... Are you in need of help with setting up a skeleton or a main method? Why don't you try and write something first. There are plenty of tutorials to help you get started. If you've got experience using streams, I don't think this should be all that hard.
So you got your algorithm. The only thing left to do is code... Are you in need of help with setting up a skeleton or a main method? Why don't you try and write something first. There are plenty of tutorials to help you get started. If you've got experience using streams, I don't think this should be all that hard.
This post has been edited by blackcompe: 21 March 2012 - 04:15 PM
#5
Re: Frequency recursion
Posted 21 March 2012 - 04:22 PM
No local variables? why no local variables??
#6
Re: Frequency recursion
Posted 21 March 2012 - 09:34 PM
The OP probably means instance variable
#8
Re: Frequency recursion
Posted 22 March 2012 - 06:47 AM
You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.
#9
Re: Frequency recursion
Posted 22 March 2012 - 01:48 PM
Quote
You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.
macosxnerd101: I'd like to see you do this without using strings... I'm totally calling you out. PM me if you come up with something.
This post has been edited by blackcompe: 22 March 2012 - 01:57 PM
#10
Re: Frequency recursion
Posted 22 March 2012 - 01:55 PM
The OP : You need a similar piece of code incorporated , like the one below to get each digit
onluk=0;
num2=num;
k=0;
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
num=(num-dig[i1])/10;
}
#11
Re: Frequency recursion
Posted 22 March 2012 - 06:17 PM
blackcompe, on 22 March 2012 - 01:48 PM, said:
Quote
You shouldn't need any local variables. Big hint- x%10 returns the last digit of x; and x/10 divides x by 10, removing the end digit. Use that to evaluate each digit.
macosxnerd101: I'd like to see you do this without using strings... I'm totally calling you out. PM me if you come up with something.
pm sent
#12
Re: Frequency recursion
Posted 22 March 2012 - 07:48 PM
#13
Re: Frequency recursion
Posted 23 March 2012 - 04:24 AM
pbl, on 22 March 2012 - 07:48 PM, said:
Because you'll later need it to keep track of number of digits.
for(int i1=0;i1<=19;i1++)
{
k=k+1;
dig[i1]=num % 10;
//if(dig[i1]<0){dig[i1]=dig[i1]+10;}
num=(num-dig[i1])/10;
if(num==0)break;
System.out.println(dig[i1]);
}
for(int i=0;i<k;i++){
if(b==dig[i])
y++;
// System.out.println(dig[i]) ;
System.out.println(y) ;
// System.out.println(" freq of "+b+" = "+freqn[v]) ;
}
// System.out.println(" freq of "+b+" = "+freqn[v]) ;
return y;
#14
Re: Frequency recursion
Posted 23 March 2012 - 06:08 AM
@burakaltr: If the OP needs to use recursion, your solution would be invalid. That also is a cumbersome solution. To count the frequencies of all the digits, create an array of length 10. Then get the digit using the % operator, and increment count[digit].
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|