C language function: recursion and iteration

Keywords: C Programming

A brief introduction to C language functions

C language function is a function used to compile c language. Its library function is ctype.h, which is divided into classification function, mathematical function, directory function, process function, diagnosis function, operation function, etc.

On the surface, when using a function, it must be enclosed with parentheses. If necessary, it must pass parameters. The execution result of the function can also be assigned to other variables

For Library and custom functions

C language has encapsulated many functions for us at the time of release. They are put into different header files by category (for the moment, I think so first). When using functions, we can introduce corresponding header files. These functions are written by experts, with high efficiency, and considering various boundary conditions, please feel free to use them.

The functions in C language are called library functions. Library is a basic concept in programming. It can be simply considered as a collection of a series of functions. It is often a folder on disk. The library of C language is called Standard Library, and the library developed by other companies or individuals is called Third-Party Library.

The difference between recursion and iteration

The basic concept of recursion: the programming skill of program calling itself is called recursion, which is the function calling itself

A function directly or indirectly calls its own method in its definition. It usually transforms a large and complex problem into a smaller problem similar to the original one, which can greatly reduce the code amount. The ability of recursion is to define infinite sets of objects with limited statements

There are two points to note when using recursion:

1) Recursion is to call itself in a procedure or function;

2) When using recursion, there must be a definite end condition of recursion, which is called recursion exit

Iteration: use the original value of the variable to calculate A new value of the variable. If the recursion calls itself, the iteration is A continuous call to B

There must be iterations in recursion, but not in iteration, most of them can be converted to each other. Those that can use iteration don't need recursion, call function recursively, waste space, and recursion is too deep, which is easy to cause stack overflow

The following examples of function calls include the application of recursive functions and iterative functions.

  1. Implement a function to print multiplication table, and specify the number of rows and columns of the table

For example: input 9, output 99 formula table, output 12, output 1212 multiplication formula table.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>
int main()
{
int i = 0, j = 0, input = 0;
scanf_s("%d", &input);
for (i = 1; i <= input; i++)
{
for (j = 1; j <= i; j++)
printf("%dx%d=%d ", i, j, i * j);
printf("\n");
}
return 0;
}
  1. The implementation function determines whether year is a good year.

Requirement: print leap years from 1000 to 2000 using the functions implemented above.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void print_run_year(int year);

int main()

{

int year = 0;

for (year = 1000;year <= 2000;year++)

print_run_year(year);

return 0;

}

void print_run_year(int year)

{

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d It is a leap year.  ", year);

}




  1. Implement a function to determine whether a number is a prime number.

Requirement: print the prime numbers between 100 and 200 with the function implemented above.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void print_sushu(int i);

int main()

{

int i = 0;

for (i = 100;i <= 200;i++)

print_sushu(i);

return 0;

}

void print_sushu(int i)

{

int j = 0;

for (j = 2;j < i;j++)

if (i % j == 0)

break;

else

{

printf("%d ", i);break;

}

}

Application of recursion and iterative function.

  1. Recursion and non recursion to find the nth Fibonacci number respectively

For example:

Input: 5 Output: 5

Input: 10, output: 55

Input: 2, output: 1

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int Fib(int i);

int main()

{

int i = 0;

int num = 0;

printf("Enter the required number\n");

scanf_s("%d", &i);

num = Fib(i);

printf("%d\n", num);

return 0;

}

int Fib(int i)//Recursive writing

{

if (i <= 2)

return 1;

else

return Fib(i - 1) + Fib(i - 2);

}







#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int Fib(int i);

int main()

{

int i = 0;

int num = 0;

printf("Enter the required number\n");

scanf_s("%d", &i);

num = Fib(i);

printf("%d\n", num);

return 0;

}

int Fib(int i)//Non recursive writing

{

int j = 0, k = 0,m=0,n=0;

if (i <= 2)

return 1;

else

{

for (j = 1, k = 1, m = 1;k <= i - 2; k++, j = m, m = n)

{

n = j + m;

}

return n;

}

}


  1. Write a function to realize the k power of n, using recursion.

Requirement: k can be positive or negative

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

float print_nk(int n,int k);

int main()

{

int n = 0,k = 0;

float num = 0;

printf("The inputs are n Of k Secondary power\n");

scanf_s("%d%d", &n, &k);

num=print_nk(n, k);

printf("%f\n", num);

return 0;

}

float print_nk(int n, int k)

{

if (k == 0)

return 1;

else if (k > 0)

return n * print_nk(n, k - 1);

else

return (1.0 / n) * print_nk(n, k + 1);



}




  1. Write a recursive function DigitSum(n), input a non negative integer, and return the sum of the numbers that make up it

For example, if you call DigitSum(1729), you should return 1 + 7 + 2 + 9, and its sum is 19

Input: 1729, output: 19

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

int DigitSum(int n);

int main()

{

int i = 0;

int num = 0;

scanf_s("%d", &i);

num = DigitSum(i);

printf("%d\n", num);

return 0;

}

int DigitSum(int n)

{

    int m=0;

if (n < 10)

return n;

else

m = n % 10;

return m + DigitSum(n / 10);

}


  1. Write a function reverse string (char * string) (recursive implementation)

Implementation: reverse the characters in the parameter string.

Requirement: cannot use string operation function in C function library.

#define _CRT_SECURE_NO_WARINGS 1

#include<stdio.h>

void reverse_string(char * string,int length);

int main()

{

char str[] = "abcde";

int length = 5;

reverse_string(str,length);



return 0;

}

void reverse_string(char * string,int length)

{

if (length > 0)

{

printf("%c", string[length - 1]);

reverse_string(string, length - 1);

}

else

printf("\n");

}





#include<stdio.h>

int Sumstrlen(char *str);

int main()

{

int sum = 0;

char str[] = {0};

printf("Input string\n");

scanf("%s", str);

sum = Sumstrlen(str);

printf("The number of characters is%d\n", sum);

return 0;

}

int Sumstrlen(char *str)

{

if (*str != '\0')

return 1 + Sumstrlen(str+1);

else

return 0;

}




  1. strlen is implemented recursively and non recursively.
#include<stdio.h>

int Sumstrlen(char *str);

int main()

{

int sum = 0;

char str[] = {0};

printf("Input string\n");

scanf("%s", str);

sum = Sumstrlen(str);

printf("The number of characters is%d\n", sum);

return 0;

}

int Sumstrlen(char *str)

{

if (*str != '\0')

return 1 + Sumstrlen(str+1);

else

return 0;

}


Therefore, we can see that the use of functions can greatly reduce the complexity of our code when we write code. The proper use of = recursion and iterative thinking in functions can help us solve many complex problems.

Published 5 original articles, won praise 0, visited 39
Private letter follow

Posted by like_php on Sun, 02 Feb 2020 06:19:50 -0800