Chapter 2 Functions and Recursion

Source: Internet
Author: User
Tags define local mathematical functions

Objective:

Measure the test taker's knowledge about the definitions and usage of mathematical functions with multiple parameters and single return values.

Learn to use typedef to define struct

Learn to use assert macro for debugging

Measure the test taker's understanding about the process of assigning values to the form parameters using real parameters during function calls.

Learn to define local variables and global variables

Understand the call stack and stack frame, learn to view the call stack with GDB, and select the stack frame

Understanding address and pointer

Understand recursive definitions and recursive functions

Understand the body segment, data segment, and BSS segment in the executable file /*

BSS (block started by symbol) is usually a memory area used to store uninitialized global variables and static variables in the program. Features: read/write, BSS segments are automatically cleared before the program is executed. Therefore, the uninitialized global variables are 0 before the program is executed. Note the difference from the data segment. BSS stores uninitialized global and static variables, and the data segment stores initialized global and static variables. */

Familiar with Stack segments and common causes of Stack Overflow

 

4-1 Combination
Input non-negative integers m and n, and output Combo number C (m, n) = n! /(M! * (N-m )!), M ≤ n ≤ 20.

Code:

#include<stdio.h>int f(int n){    int i,m=1;    for(i=1;i<=n;i++)       m*=i;    return m;}int main(){    int m,n;    scanf("%d %d",&m,&n);    printf("%d\n",f(n)/f(m)*f(n-m));    return 0;}


4-2 twin prime numbers

If n and n + 2 are both prime numbers, they are twin prime numbers. Input M. The output two numbers cannot exceed the maximum twin prime number of M. 5 ≤ m ≤ 10000. For example, when M = 20, the answer is 17, 19, and when M = 1000, the answer is 881, 883.

Code:

# Include <stdio. h> # include <math. h> # include <assert. h> // restrict the call of an invalid function to int is_prime (int x) // is it a prime? {Int I, m; // m avoid repeated SQRT (x) calculation, and avoid floating point errors assert (x> = 0) by rounding; If (x = 1) return 0; M = floor (SQRT (x) + 0.5); for (I = 2; I <= m; I ++) if (X % I = 0) return 0; return 1;} int main () {int I, m; scanf ("% d", & M); For (INT I = m-2; I> = 3; I --) if (is_prime (I) & is_prime (I + 2) {printf ("% d \ n", I, I + 2); break ;} return 0 ;}


4-5 swap variables with functions

Pointer and stack required. Otherwise, the swap function cannot be called successfully.

Code:

# Include <stdio. h> void swap (int * a, int * B) {int T = * A; * A = * B; * B = T;} int main () {int A = 3, B = 4; swap (& A, & B); // & changes the value of the real parameter printf ("% d \ n ", a, B); Return 0 ;}

4-6 use recursion to calculate factorial

Mathematical functions can also be recursively defined: factorial function f (n) = n! It can be defined as: {f (0) = 1

F (n) = f (n-1) * n (n ≥ 1)

Code:

# Include <stdio. h> int F (int n) {return n = 0? 1: F (n-1) * n; // easy to use} int main () {printf ("% d \ n", F (3 )); return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.