C Programming Code Examples
C > Recursion Code Examples
Count the Number of Occurrences of an Element in the Linked List using Recursion
/* Count the Number of Occurrences of an Element in the Linked List using Recursion
This C Program uses recursive function & finds the occurrence for an element in an unsorted list. The user enters the element need to be counted. */
#include <stdio.h>
void occur(int [], int, int, int, int *);
int main()
{
int size, key, count = 0;
int list[20];
int j;
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Printing the list:\n");
for (j = 0; j < size; j++)
{
list[j] = rand() % size;
printf("%d ", list[j]);
}
printf("\nEnter the key to find it's occurence: ");
scanf("%d", &key);
occur(list, size, 0, key, &count);
printf("%d occurs for %d times.\n", key, count);
return 0;
}
void occur(int list[], int size, int index, int key, int *count)
{
if (size == index)
{
return;
}
if (list[index] == key)
{
*count += 1;
}
occur(list, size, index + 1, key, count);
}
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.
Generate random number. Returns a pseudo-random integral number in the range between 0 and RAND_MAX. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.
The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
This software can convert your File/Folder to Control Panel and can Restore again. Path of the file. This software is not responsible for any loss in data. New name of your folder/file
Take the number of rows as input and store in the variable number. Firstly decrement the variable number by 1 and assign this value to the variable count. Use this variable count as
C language read elements in two matrices and find the difference of two matrices. Subtract two matrices in C. Elements of two matrices can only be subtracted if and only...
You need a HUGE memory model to run this. Checks to see if a Sound Blaster exists at a given address, returns true if Sound Blaster found, false if not. Send a byte to the DSP