Home > Back-end >  C Program to Count Frequency of Element
C Program to Count Frequency of Element

Time:10-18

I have this code which finds occurrence of number from numbers entered by user. My question is how to firstly enter numbers by user and then ask user to find his number in the sequence. I'm sorry if this question is stupid, but I'm just a beginner trying to teach myself how to program :) Thank you so much for your help!

This is my current code:

int main()
{
    int num, remainder, k, count = 0;

    printf("Which number do you want to find:  \n");
    scanf("%d", &k);

    while(1)
    {
        printf("Enter numbers(end by pressing 0): ");
        scanf("%d", &num);

        remainder=num;
        if(remainder==k)count  ;
        if(remainder==0) break;

    }


    printf("\Number %d occurs %d times.\n", k, count);


    getch();
    return 0;
}

This is what I tried:

int main()
{
    int num, remainder, k, count = 0;


    while(1)
    {
        printf("Enter numbers(end by pressing 0): ");
        scanf("%d", &num);

        remainder=num;
        if(remainder==k)count  ;
        if(remainder==0) break;

    }

    printf("Which number do you want to find:  \n");
    scanf("%d", &k);

    printf("\Number %d occurs %d times.\n", k, count);


    getch();
    return 0;

CodePudding user response:

If you want to read the number list first, you need to store it somehow in the memory using variables and then you can find the number in that list.

This can be done by using the concept of arrays in C.

In general, an array stores a definite collection/list of similar typed data. For example, an array of int stores a collection of integers.

You can read more about them here, or even you can google it.

Coming to your code, You can declare and store data in an array like this,

int numbers[10]; // stores 10 integers, size 10 is static here.
int remainder, k, count = 0;

printf("Enter 10 digits to store : ");
for(int i = 0; i < 10; i  ) { // this is a for-loop that executes below block 10 times
    scanf("%d", &numbers[i]); // read a number to location i
}


printf("Which number do you want to find:  \n");
scanf("%d", &k);

// now use for-loop to iterate over the numbers array(while-loop can also be used)
for(int i = 0; i < 10; i  ) {
    remainder=numbers[i];
    if(remainder==k)count  ; // you can also directly compare numbers[i] to k
    // below line is not needed since the loop stops at 10th iteration
    // if(remainder==0) break;
}

printf("\Number %d occurs %d times.\n", k, count);

Note:

The above code works only when the user wants to give exactly 10 numbers. If the user needs to give variable number of numbers to find k from, then you will have to read the size of the array from user and create a dynamic array of that size. Refer this

CodePudding user response:

You can safely ignore all answers except mine.:)

For your task you need the data structure that is called singly-linked list. Theoretically it does not have a limit and you can enter any sequence of numbers until 0 will be entered.

Here is a demonstrative program.

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int number;
    struct node *next;
};

struct sequence
{
    struct node *head;
    struct node *tail;
};

int append( struct sequence *seq, int number )
{
    struct node *new_node = malloc( sizeof( *new_node ) );
    int success = new_node != NULL;

    if( success )
    {
        new_node->number =number;
        new_node->next = NULL;
        
        if ( seq->head == NULL )
        {
            seq->head = new_node;
        }
        else
        {
            seq->tail->next = new_node;
        }

        seq->tail = new_node;
    }

    return success;
}

size_t count( const struct sequence *seq, int number )
{
    size_t cnt = 0;

    for ( const struct node *current = seq->head; current != NULL; current = current->next )
    {
        if ( current->number == number )   cnt;
    }

    return cnt;
}

void clear( struct sequence *seq )
{
    while ( seq->head != NULL )
    {
        struct node *current = seq->head;
        seq->head = seq->head->next;
        free( current );
    }

    seq->tail = NULL;
}
        
int main(void) 
{
    struct sequence seq = { .head = NULL, .tail = NULL };

    printf( "Enter numbers (end by entering 0): " );

    int number;

    while ( scanf( "%d", &number ) == 1 && number != 0 && append( &seq, number ) );

    number = 0;

    printf( "Which number do you want to find: " );

    scanf( "%d", &number );

    printf( "\nNumber %d occurs %zu times.\n", number, count( &seq, number ) );

    clear( &seq );

    return 0;
}

Its output might look like

Enter numbers (end by entering 0): 1 2 3 4 5 6 7 8 9 1 8 7 6 5 4 3 2 1 0
Which number do you want to find: 1
Number 1 occurs 3 times.
  • Related