I am having a hard time trying to count how many items in my linked list that are divisible by an int. We will call this int 3. I have attached my divList.c function. Can anyone help? Thanks!
Code:
```#include "ll.h"

int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
printf ("%d ", h -> info);
divList (h -> next, div);
}```

2. Your function is not returning values like it should. If you find a number divisible by div, you must essentially return 1 plus the value of the divList call on the remainder of the list; if the current value is not divisible by div, just return the value of the divList call on the remainder of the list.

Code:
```int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
{
printf ("%d ", h -> info);
return 1 + divList (h -> next, div);
}
else
return divList (h -> next, div);
}```

3. Originally Posted by hk_mp5kpdw
Your function is not returning values like it should. If you find a number divisible by div, you must essentially return 1 plus the value of the divList call on the remainder of the list; if the current value is not divisible by div, just return the value of the divList call on the remainder of the list.

Code:
```int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
{
printf ("%d ", h -> info);
return 1 + divList (h -> next, div);
}
else
return divList (h -> next, div);
}```
Unfortunately, it returns the same thing I am not looking for. I may not have made myself clear. In my input list I have the numbers of 3, 4, 0,1, 6, 8, -3, 21, and -33. All these numbers are being divided by the int 3. What happens is it prints all the numbers divisible by 3 like -33, 21, -3, 6, 0 and 3. What my actually end result or what should print is the count of the numbers divisible by 3. In this case it should print 5 because that is how many elements that were divisible by 3. I appreciate the help given.

4. > In this case it should print 5 because that is how many elements that were divisible by 3
What does it print? 6 perhaps?

> What happens is it prints all the numbers divisible by 3 like -33, 21, -3, 6, 0 and 3.
Because that's how many numbers there are...

5. Originally Posted by Salem
> In this case it should print 5 because that is how many elements that were divisible by 3
What does it print? 6 perhaps?

> What happens is it prints all the numbers divisible by 3 like -33, 21, -3, 6, 0 and 3.
Because that's how many numbers there are...
My bad, you are correct it shoud print 6. What the above functions print is -33, 21, -3, 6, 0 and 3.

6. Got it! Here is what I did,
Code:
```#include "ll.h"

int count = 0;

int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
count++;
divList (h -> next, div);
printf ("%d ", count);
}```
Thanks for all the help!

7. > Got it! Here is what I did,
That's wrong then.
Using a messy global variable doesn't make your function re-usable at all.

Post #3 was correct.

8. Not sure what you mean by "messy global variable" Post #3 is my post, could you mean post #2?

9. Sorry, it was hk_mp5kpdw that posted it.

Re: "messy"
> int count = 0;
- You HAVE to remember to reset this each time you want to use the function.
- The code is unusable in a multi-threaded environment.

You can also achieve the same thing without using recursion, which would be even better.

10. Originally Posted by Salem
Sorry, it was hk_mp5kpdw that posted it.

Re: "messy"
> int count = 0;
- You HAVE to remember to reset this each time you want to use the function.
- The code is unusable in a multi-threaded environment.

You can also achieve the same thing without using recursion, which would be even better.
Our assigment is recursion and that is what the instructor wants at this time. Not sure what you mean by multi-threaded environment but I happen to have that feeling we are not there.

Are you saying that int count = 0 needs to be set to 0 everytime the recursion loop runs? In other words, read the first element in the list, set to 0, read the next element, set to 0, etc? As it stands, the code of:
Code:
```#include "ll.h"

int count = 0;

int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
count++;
divList (h -> next, div);
printf ("%d ", count);
}```
gets the count of 6 elements that are divisible by 3. The only thing I have to contend with now is it prints the "6" six times and I believe that can be corrected in my printList.c function.

11. Instead of making count a global variable, make it into a parameter.

12. Originally Posted by laserlight
Instead of making count a global variable, make it into a parameter.
How is count a global variable? It is not a local variable since it is declared in this function only? I am a bit confused now. Why and how would I want to make it a parameter. I probably need it dumbed down to my level. Is the last code I wrote correct for what I am trying to accomplish?

13. Originally Posted by csharp100
How is count a global variable? It is not a local variable since it is declared in this function only?
You declared count at file scope. If you declared count in the function, it would either be as a parameter or as something like:
Code:
```int divList (lnode * h, int div) {
int count = 0;
if (h == NULL) return 0;
if (h -> info%div == 0)
count++;
divList (h -> next, div);
printf ("%d ", count);
}```
Originally Posted by csharp100
Why and how would I want to make it a parameter.
Looking more closely at what you want to do, I'd say: my mistake. No, count should not be a parameter. Rather, you do not need it at all, save for the fact that you want to print the count in divList if h is not a null pointer. As such, you would use a local variable named count to store the result of the recursive call (+1), and then print that, and return it.

Originally Posted by csharp100
Is the last code I wrote correct for what I am trying to accomplish?
Notice that there is a path of control for which divList does not return a value. That is wrong.

14. Originally Posted by laserlight
You declared count at file scope. If you declared count in the function, it would either be as a parameter or as something like:
Code:
```int divList (lnode * h, int div) {
int count = 0;
if (h == NULL) return 0;
if (h -> info%div == 0)
count++;
divList (h -> next, div);
printf ("%d ", count);
}```

Looking more closely at what you want to do, I'd say: my mistake. No, count should not be a parameter. Rather, you do not need it at all, save for the fact that you want to print the count in divList if h is not a null pointer. As such, you would use a local variable named count to store the result of the recursive call (+1), and then print that, and return it.

Notice that there is a path of control for which divList does not return a value. That is wrong.
Do I really need to print it, or can I just return "count" to say printList.c.

15. This is how I imagined the function being used/called:

Code:
```...

int divList (lnode * h, int div){
if (h == NULL) return 0;
if (h -> info%div == 0)
return 1 + divList (h -> next, div);
else
return divList (h -> next, div);
}

...

int main(void)
{
int count = 0;

...