Recursion & Its type
Linear Recursive
A linear recursive function is a function that only makes a single call to itself each time the function runs (as opposed to one that would call itself multiple times during its execution). The factorial function is a good example of linear recursion.
Tail recursive
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last thing the function does.
Head recursive
Head recursion is a form of linear recursion. In head recursion, the recursive call is the first thing the function does.
Binary Recursive
Some recursive functions don't just have one call to themselves, they have two (or more). Functions with two recursive calls are referred to as binary recursive functions.
Nested Recursion
In nested recursion, one of the arguments to the recursive function is the recursive function itself!
=================================================
=================================================
C Programs
To See Source Code of All 'C' Programs
Click Here
FOR ALL PROGRAMS
SEE "BLOG ARCHIVE-LIST OF PROGRAMS"
(AT RIGHT SIDE OF PAGE)
=================================================
Link to some new programs...
=================================================
Array
An array is a sequence of data item of homogeneous value(same type).
Declaration of one-dimensional array
data_type array_name[array_size]; For example: int age[5];
int age[5];
C Arrays in Detail
Concept | Description |
---|---|
C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. | |
You can pass to the function a pointer to an array by specifying the array's name without an index. | |
C allows a function to return an array. | |
You can generate a pointer to the first element of an array by simply specifying the array name, without any index. |
=================================================
Difference between Recursion and Iteration
RECURSION
|
ITERATIONS
|
Recursive function – is a function that is partially defined by itself
|
Iterative Instructions –are loop based repetitions of a process
|
Recursion Uses selection structure
|
Iteration uses repetition structure
|
Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition.(base case)
|
An infinite loop occurs with iteration if the loop-condition test never becomes false
|
Recursion terminates when a base case is recognized
|
Iteration terminates when the loop-condition fails
|
Recursion is usually slower then iteration due to overhead of maintaining stack
|
Iteration does not use stack so it's faster than recursion
|
Recursion uses more memory than iteration
|
Iteration consume less memory
|
=================================================
Difference between Structure and Union in C language
Structure
|
Union
|
1.The keyword struct is used to define a structure
|
1. The keyword union is used to define a union.
|
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structureis greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.
|
2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
|
3. Each member within a structure is assigned unique storage area of location.
|
3. Memory allocated is shared by individual members of union.
|
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
|
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
|
5 Altering the value of a member will not affect other members of the structure.
|
5. Altering the value of any of the member will alter other member values.
|
6. Individual member can be accessed at a time
|
6. Only one member can be accessed at a time.
|
7. Several members of a structure can initialize at once.
|
7. Only the first member of a union can be initialized.
|
=================================================
Important programs logic
WRITE A FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER
Copy Text From One File to Other File
http://c-programs-database.blogspot.in/2015/06/copy-text-from-one-file-to-other-file.html
C program to convert the file contents in Upper-case & Write Contents in a output file
Remove duplicate items from an array
Remove extra spaces from a sting
Power using recursion
WACP to find whether number is prime or not.
PROGRAMME TO SORT AN ARRAY
http://c-programs-database.blogspot.in/2014/04/programme-to-sort-array.html
PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY
http://c-programs-database.blogspot.in/2014/04/program-to-search-element-in-array.htmlWRITE A FUNCTION TO READ THE DETAILS OF A STUDENT AND THEN DISPLAY DETAILS IN CALLING FUNCTION
WRITE A PROGRAMME TO STORE INFORMATION OF N STUDENTS WHERE INFORMATION COMPRISES OF STUDENT'S NAME,SNO AND MARKS IN 4 SUBJECTS
A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicitly specified by using the static keyword. Actually, a string is a character array with following properties :
- The multi-byte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
- It not specified what happens if this array, i.e., string, is modified. So the value of a string is the sequence of the values of the contained characters, in order.
=================================================
Advantages and disadvantages of pointers in c
Benefits(use) of pointers in c:
- Pointers provide direct access to memory
- Pointers provide a way to return more than one value to the functions
- Reduces the storage space and complexity of the program
- Reduces the execution time of the program
- Provides an alternate way to access array elements
- Pointers can be used to pass information back and forth between the calling function and called function.
- Pointers allows us to perform dynamic memory allocation and deallocation.
- Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc.
- Pointers allows us to resize the dynamically allocated memory block.
- Addresses of objects can be extracted using pointers
Drawbacks of pointers in c:
- Uninitialized pointers might cause segmentation fault.
- Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak.
- Pointers are slower than normal variables.
- If pointers are updated with incorrect values, it might lead to memory corruption.
Basically, pointer bugs are difficult to debug. Its programmers responsibility to use pointers effectively and correctly.
=================================================
Limitations of Array in C Programming :
We know What is an array in C Programming. Array is very useful which stores multiple data under single name with same data type. Following are some listed limitations of Array in C Programming.
A. Static Data
- Array is Static data Structure
- Memory Allocated during Compile time.
- Once Memory is allocated at Compile Time it Cannot be Changed during Run-time
B. Can hold data belonging to same Data types
- Elements belonging to different data types cannot be stored in array because array data structure can hold data belonging to same data type.
- Example : Character and Integer values can be stored inside separate array but cannot be stored in single array
C. Inserting data in Array is Difficult
- Inserting element is very difficult because before inserting element in an array we have to create empty space by shifting other elements one position ahead.
- This operation is faster if the array size is smaller, but same operation will be more and more time consuming and non-efficient in case of array with large size.
D. Deletion Operation is difficult
- Deletion is not easy because the elements are stored in contiguous memory location.
- Like insertion operation , we have to delete element from the array and after deletion empty space will be created and thus we need to fill the space by moving elements up in the array.
E. Bound Checking
- If we specify the size of array as ‘N’ then we can access elements upto ‘N-1′ but in C if we try to access elements after ‘N-1′ i.e Nth element or N+1th element then we does not get any error message.
- Process of Checking the extreme limit of array is called Bound Checking and C does not perform Bound Checking.
- If the array range exceeds then we will get garbage value as result.
F. Shortage of Memory
- Array is Static data structure. Memory can be allocated at compile time only Thus if after executing program we need more space for storing additional information then we cannot allocate additional space at run time.
- Shortage of Memory , if we don’t know the size of memory in advance
G. Wastage of Memory
- Wastage of Memory , if array of large size is defined
=================================================
Difference between Arrays and Structures in C
Both the arrays and structures are classified as structured data types as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they differ in a number of ways listed in table below:
Arrays
|
Structures
|
1. An array is a collection of related data elements of same type. | 1. Structure can have elements of different types |
2. An array is a derived data type | 2. A structure is a programmer-defined data type |
3. Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it. | 3. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used. |
=================================================
C Programming Enumeration
An enumeration is a user-defined data type consists of integral constants and each integral constant is give a name. Keyword
enum
is used to defined enumerated data type.enum type_name{ value1, value2,...,valueN };
Here, type_name is the name of enumerated data type or tag. And value1, value2,....,valueN are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value.
// Changing the default value of enum elements enum suit{ club=0; diamonds=10; hearts=20; spades=3; };
Declaration of enumerated variable
Above code defines the type of the data but, no any variable is created. Variable of type
enum
can be created as:enum boolean{ false; true; }; enum boolean check;
Here, a variable check is declared which is of type
enum boolean
.Example of enumerated type
#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Output
4 day
=================================================
Definition of Function:
A function is a routine or a set of instruction or code that performs a specific task and can be processed independently.
When the program passes control to a function the function perform that task and returns control to the instruction following the calling instruction. The most important reason to use the function is make program handling easier as only a small part of the program is dealt with at a time.
Advantages of Functions:
i) The length of a source program can be reduced by using functions at appropriate places.
ii) It is easy to locate and isolate a faulty function for further investigations.
iii) A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting all over again from scratch.
iv) It facilitates top-down modular programming. In this programming style, the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later.
v) Its interface to the rest of the program is clean and narrow.
Disadvantages of Functions:
vi) While adding a user function can speed up code that is best written in C rather than a scripting language, it is not always the best choice for implementation:
vii) It requires the programmer to be well versed in C, including pointers, function pointers, dynamic memory allocation, and debugging. Often the headaches C causes, especially for the neophyte, far outweigh any run-time savings. Bugs in the code might not manifest themselves until well after the C function ends, making debugging a nightmare.
viii) It's less portable.
=================================================
=================================================
=================================================
=================================================
you are best blogger. Thanks for such a nice information studyfreevr -help-for-end-term-examination-www.studyfreevr.co.in
ReplyDelete