 # C Programming Code Examples

## C > Functions Code Examples

### program to find maximum and minimum elements in array using recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* program to find maximum and minimum elements in array using recursion Write a C program to find maximum and minimum elements in an array using recursion. How to find maximum and minimum element in an array using recursion in C programming. Recursion works on the concept of divide and conquer. We break the problem in its smallest size where it can be solved directly. For the maximum/minimum problem, the smallest problem size would be finding maximum/minimum between two elements. Before writing our main logic, let us first define a recursive function to find maximum element in an array. int maximum(int array[], int index, int N); For this recursive approach to find maximum, I have divided the algorithm in three main steps. If only two elements are left. We can easily find maximum using array[currentIndex] > array[currentIndex + 1]. */ /* * This conditions confirms that only two elements * are left i.e. array[index] and array[index + 1] */ if(index == N-2) { if(array[index] > array[index + 1]) { // array[index] i.e. second last element is max } else { // array[index + 1] i.e. last element is max } } Find maximum on the right side of current array index. For this step we will use recursive function call. max = maximum(array, index + 1, N); Finally compare the current array element with maximum element on its right (found in above step). if(array[index] > max) { // array[index] is maximum } else { // max is maximum } #include <stdio.h> #define maxsize 100 // Maximum size of the array /* Function declarations */ int maximum(int array[], int index, int len); int minimum(int array[], int index, int len); int main() { int array[maxsize], N, max, min; int i; /* Input size and elements of array */ printf("Enter size of the array: "); scanf("%d", &N); printf("Enter %d elements in array: ", N); for(i=0; i<N; i++) { scanf("%d", &array[i]); } max = maximum(array, 0, N); min = minimum(array, 0, N); printf("Minimum element in array = %d\n", min); printf("Maximum element in array = %d\n", max); return 0; } /* Recursive function to find maximum element in the given array. */ int maximum(int array[], int index, int len) { int max; /* Only last and second last element are left */ if(index >= len-2) { if(array[index] > array[index + 1]) return array[index]; else return array[index + 1]; } /* * Recursively call maximum to find maximum element in * right side of the array from current index. */ max = maximum(array, index + 1, len); /* * Compare the current array element with maximum * element on its right side */ if(array[index] > max) return array[index]; else return max; } /* Recursive function to find minimum element in the array. */ int minimum(int array[], int index, int len) { int min; if(index >= len-2) { if(array[index] < array[index + 1]) return array[index]; else return array[index + 1]; } min = minimum(array, index + 1, len); if(array[index] < min) return array[index]; else return min; }

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.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

#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.

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,

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions. A function can also be referred as a method or a sub-routine or a procedure, etc.

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.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

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...

C Program code to demonstrate nested printf statements. String will be first printed while executing inner printf. Length of the string "abcdefghijklmnopqrstuvwxyz". So printf will

C program to 'input any string' from user and remove first occurrence of a given word from string. Logic to remove 'first occurrence' of a word. Input string from user, store it in some

The if...else statement executes some code if the test expression is true (nonzero) & some other code if the test expression is false (0). If test expression is true, codes inside the body

C language program take the number of days as input. For the number of years, divide the input by 365 and obtain its quotient. For the number of weeks, divide the input by 365 and

Take the sentence as input, store in the array sentence[] and Initialize the variables vowels, consonants and special to zero. Using if, else statements, check if the sentence has vowels