1

I am writing a program where we need to find the frequency of the number chosen in an array. The numbers were inputted by the user and he/she will choose a number to count its instance. I did it with this program.

#include<stdio.h>
int main()
{
    
    int n, frq=0, chn;
    int num[10];
    int i, j;
    
    printf("Enter quantity of numbers to be inputted: ");
    scanf("%d", &n);
    
    for(i=0; i<n; i++)
    {
        printf("Enter number[%d]: ", i+1);
        scanf("%d", &num[i]);
    }
    printf("(");
    for(j=0; j<n; j++)
    {
        printf("%d, ", num[j]);
    }
    printf(")\n\n");
    
    printf("Enter number to be counted: ");
    scanf("%d", &chn);
    
    for(i=0; i<n; i++)
    {
    if(num[i]==chn)
    {
       frq++;   
    }}
    printf("Instance count: %d", frq);
    return 0;
}

It works perfectly fine. Then our teacher tasked us to do the same. But this time, we will use user-defined functions for the part where the program will count the instances of the chosen number. This is what I did.

#include<stdio.h>

int freq(int n);

int main()
{
    
    int n, frq=0, chn;
    int num[10];
    int i, j;
    
    printf("Enter quantity of numbers to be inputted: ");
    scanf("%d", &n);
    
    for(i=0; i<n; i++)
    {
        printf("Enter number[%d]: ", i+1);
        scanf("%d", &num[i]);
    }
    printf("(");
    for(j=0; j<n; j++)
    {
        printf("%d, ", num[j]);
    }
    printf(")\n\n");
    
    printf("Enter number to be counted: ");
    scanf("%d", &chn);
    
    frq = freq(n); 

    printf("Instance count: %d", frq);
    return 0;
}

int freq(int n)
{
int chn, num[10], i, frq=0;
    
    
    for(i=0; i<n; i++)
    {
    if(num[i]==chn)
    {
       frq++;   
    }}
    return(frq);
}

But when I run it, it doesn't bring out the supposed output. For example;

input quantity: >>>6
Enter number[1]: >>>1
Enter number[2]: >>>2
Enter number[3]: >>>3
Enter number[4]: >>>3
Enter number[5]: >>>3
Enter number[6]: >>>2
(1, 2, 3, 3, 3, 2, )

Enter number to be counted: >>>3
Instance count: 1

Process Finished.
>>>

It was supposed to print 3 but instead, it prints 1. I don't know where I did wrong and I need your help guys. I really appreciate every answers and comments you put here, I run through every single one of them and it helped me. Thank you again!

ps. we were not allowed to use pointers or structures or idk in this program yet. Just in case some of you will suggest using them lol.

Dexter1403
  • 35
  • 6
  • `if(num[i]==chn)` You should read your learning material about function parameters and local variables. You use a local variable `num` inside `freq` that is not related to the variable `num` in `main` and contains undetermined (random) content. – Gerhardh Oct 21 '21 at 08:05
  • 2
    I can't help wonder... You correctly pass `n` as argument to the function. Fine - you know that `n` needs to be passed from `main` to the function. So what makes you think that `chn` doesn't need to be passed as well. And what kind of magic do think will bring the array `num` into the function without passing it. – Support Ukraine Oct 21 '21 at 08:13
  • In other words: `int chn, num[10], i, frq=0;` --> `int i, frq=0;` and pass the other variables as arguments to the function – Support Ukraine Oct 21 '21 at 08:14
  • Stop wasting your time by troubleshooting problems that the compiler already found for you. [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Oct 21 '21 at 11:04

1 Answers1

0

To write the function finding the frequency you need to pass:

  1. The array
  2. The size of the array
  3. The number.

You simply declare the array inside the function thinking that it will magically have the content you have entered in another function. No, it contains undetermined values and invokes an UB

size_t freq(int *array, size_t size, int val)
{
    size_t freq = 0;
    while(size--) freq += *array++ == val;
    return freq;
}

int main(void)
{
    
    int num[] = {1, 2, 3, 3, 3, 2, };

    printf("The %d is present in the array %zu times\n", 3, freq(num, sizeof(num)/sizeof(num[0]), 3));
}

or instead ( as @4386427 spotted) "oneliner"

    while(size--) 
    {
        if(*array == val) freq += 1;
        array++;
    }
0___________
  • 60,014
  • 4
  • 34
  • 74
  • hmmm... a one-liner doing 6 different things. I'm sure the teacher will be impressed when the student turns that in. BTW: It wouldn't pass code review at my work – Support Ukraine Oct 21 '21 at 08:22
  • @4386427 why? It is quite simple. I would easy pass at mine. – 0___________ Oct 21 '21 at 08:37
  • Not even `size_t` is able to count the number of bugs introduced by statements with multiple side effects. Keep it simple is a great design principle. – Support Ukraine Oct 21 '21 at 08:45
  • while(size--)freq+=array[size-1]==val?1:0; – Evil Dog Pie Oct 21 '21 at 08:45
  • @EvilDogPie nah... `*array++ == val` is already 0 or 1 so adding `?:` is just making things worse – Support Ukraine Oct 21 '21 at 08:47
  • 1
    @EvilDogPie BTW: Thank you for making my point stronger. Your rewrite suggestion now made the code fail. Can you see why? This is exactly what happens when the original code is too complex. Sooner or later a coworker comes by and do a "simple" rewrite that makes thing fail. – Support Ukraine Oct 21 '21 at 08:48
  • @4386427 there is an alternative version too :) – 0___________ Oct 21 '21 at 08:54
  • @4386427 Yup. I've just been debugging something almost exactly like this and I'm tired. – Evil Dog Pie Oct 21 '21 at 08:54
  • @EvilDogPie if you do not know that `*array++ == val` is zero or one, you should not debug someones code :) – 0___________ Oct 21 '21 at 08:57
  • @0___________ yes, i have seen the alternative code. If that had been the original/only proposal I would have upvoted. But, sorry, I can't upvote an answer with that one-liner. It simply violates the basic coding principle I'm trying to teach new unexperienced coworkers. That said - I don't downvote either cause your one-liner is correct (as far as I can see). – Support Ukraine Oct 21 '21 at 09:03