I have a question that I am not able to solve.
Alex wants to test his child's ability of identifying correct spelling of a word from a group of jumbled words. To jumble the words they want to find out how many ways a word can be rearranged. For example if they want to rearrange a word "whats" where the letters are unique, the number of ways can be found by finding 5!. It the word is "hello" where letter 'l' alone is repeated twice, the number of ways a word can be rearranged can be found out by 5!/2!(no of letters!/number of repeated letter count!). If the word is "little" where two letters 'l' and 't' are repeated twice, then number of ways to arrange letters is 6!/(2!*2!). Write a recursive function to find factorial.
Input: String to rearrange. Output: Integer denoting the number of ways a letter can be jumbled.
Sample Test: Input: document Output: 40320
I tried this code(C language):
#include<stdio.h>
#include<string.h>
int seq(char arr[], int len) {
int i, j, cnt[100], rf, nf, r;
for (i=0;i<len;i ) {
int ct=0;
for (j=i;i<len;j ) {
if (arr[i]==arr[j])
ct ;
}
cnt[i]=ct;
}
for (i=1;i<=len;i )
nf *= i;
size_t size = sizeof(cnt)/sizeof(cnt[0]);
for (j=0;j<size;j ) {
for (i=1;i<cnt[j];i )
rf *= i;
r *= rf;
}
return nf/r;
}
int main() {
char arr[100];
scanf("%s", arr);
printf("%d", seq(arr, strlen(arr)));
}
Expected: Input: Whats Output: 120
Actual: Input: Whats Output: Run Error Segmentation fault (core dumped)
CodePudding user response:
There are several bugs. One is that almost all of the variables used to calculate factorials are uninitialized. Those need to be initialized to 1.
There are also some typos. For example, for (j=i;i<len;j )
should be for (j=i;j<len;j )
(the i
in the comparison needs to be j
).
And the loop for (i=1;i<cnt[j];i )
, which calculates the factorials of the duplicated letters, needs to include the final count, i.e. it needs to be for (i=1;i<=cnt[j];i )
.
Also, this part:
size_t size = sizeof(cnt)/sizeof(cnt[0]);
for (j=0;j<size;j ) {
always forces the loop to run from 0
to 99
. It should run from 0
to len-1
, i.e. those two lines need to be replaced with:
for (j=0;j<len;j ) {
Here's a version that fixes these problems:
#include<stdio.h>
#include<string.h>
int seq(char arr[], int len) {
int i, j, cnt[100], rf, nf, r;
for (i=0;i<len;i ) {
int ct=0;
for (j=i;j<len;j ) {
if (arr[i]==arr[j])
ct ;
}
cnt[i]=ct;
}
nf = 1;
for (i=1;i<=len;i )
nf *= i;
r = 1;
for (j=0;j<len;j ) {
rf = 1;
for (i=1;i<=cnt[j];i )
rf *= i;
r *= rf;
}
return nf/r;
}
int main() {
char arr[100];
scanf("%s", arr);
printf("%d\n", seq(arr, strlen(arr)));
}
I also added a newline to the end of the printf
format string.
CodePudding user response:
At least these problems:
Code compiled without all compiler warnings enable
At least 3 issues warned about.
Uninitialized variables
nf *= i;
, r *= rf;
used without nf, r, rf
being assigned.
Loop count wrong
for (j=0;j<size;j ) {
iterates size
times even though cnt[j]
only assigned len
times.
Wrong test
Review for (j = i; i < len; j ) {
stopping condition.
int
overflow
Code like nf *= i
readily overflows. Use a wider type for nf
and check for overflow potential.
Buffer overflow
Input risks overflow without a width.
// scanf("%s", arr);
scanf("