0% found this document useful (0 votes)
24 views1 page

Question 2

The document contains a C program that counts the occurrences of a specified item in an array, simulating a linked list. It prompts the user to enter the size of the list and generates random numbers, then asks for a key to search for its occurrences. The program outputs the total count of the key found in the list.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

Question 2

The document contains a C program that counts the occurrences of a specified item in an array, simulating a linked list. It prompts the user to enter the size of the list and generates random numbers, then asks for a key to search for its occurrences. The program outputs the total count of the key found in the list.

Uploaded by

Ritesh Vashisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 2

Write a program to print the total number of occurrences of a given item in the linked list

CODE:
#include <stdio.h>
 
int occur(int [], int, int);
 
int main()
{
  int size, key, count;
  int list[20];
  int i;
 
  printf("Enter the size of the list: ");
  scanf("%d", &size);
  printf("Printing the list:\n");
  for (i = 0; i < size; i++)
  {
    list[i] = rand() % size;
    printf("%d  ", list[i]);
  }
  printf("\nEnter the key to find it's occurence: ");
  scanf("%d", &key);
  count = occur(list, size, key);
  printf("%d occurs for %d times.\n", key, count);
  return 0;
}
 
int occur(int list[], int size, int key)
{
  int i, count = 0;
 
  for (i = 0; i < size; i++)
  {
    if (list[i] == key)
    {
      count += 1;
    }
  }
  return count;
}

You might also like