Home > OS >  Finding the frequency of an element in an array using a user-defined function in C
Finding the frequency of an element in an array using a user-defined function in C

Time:10-21

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.

CodePudding user response:

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  ;
    }
  • Related