Modular programming with functions (II.)

Source: Internet
Author: User
Tags decimal to binary

Modular programming with functions (II.)

First, nested calls to functions

The function definition of C language is parallel and independent to each other. That is, functions cannot be nested, but can be nested to call functions. That is, in the process of calling a function, another function can be called

Execution process,

Example: Enter 4 integers to find the largest number. Handled using nested calls to functions.

Problem Solving Ideas:

Call the MAX1 function in main to find the largest of 4 numbers

Call Max2 in Max1 and find the big one in two numbers

/* Enter 4 integers to find the largest number. Handled using nested calls to functions. */

#include "stdio.h"

void Main ()

{

int max1 (int a,int b,int c,int D); /* Declaration on MAX1 * *

int A,b,c,d,max;

printf ("Please enter four numbers:");

scanf ("%d,%d,%d,%d", &a,&b,&c,&d);

Max=max1 (A,B,C,D); /* Call the MAX1 function to get the maximum value and assign the value to max*/

printf ("max=%d \ n", max);

return 0;

}

int max1 (int a,int b,int c,int d)/* Define MAX1 function */

{

int max2 (int a,int b); /*MAX2 's statement * *

int m;

M=max2 (A, b); /* Call the Max function to find the maximum value of a and B */

M=MAX2 (M,C); /* Call the Max function to find the maximum value of M and C */

M=MAX2 (M,D); /* Call the Max function to find the maximum values for M and D */

Return (m); /* The return value of the function is the largest of 4 digits */

}

int max2 (int a,int b)/* Define MAX2 function */

{if (a>=b)

return A;

else/* The return value of the function is large in a and B */

return b;

}

Second, recursive invocation of functions

concept: The function itself is called directly or indirectly in the process of invoking a function, called a recursive invocation of a function.

For example:

int f (int x)

{

int y,z;

Z=f (y);

return (2*Z);

}

in the call F function, it also calls the F function

Practice 1 : 5 students sat together and asked how old the 5th student was? He said 2 years older than the 4th student, asked the 4th student age, he said 2 years older than the 3rd student, asked the 3rd students, and said that more than a 2nd students 2 years old, asked 2nd a student, said that than the 1th students 2 years old, and finally asked the 1th students, he said 10 years old, ask the number of 5th students how big

The idea of solving a problem: requiring a 5th age, you must first know the 4th age

Requires a 4th age to be aware of the 3rd age first.

And so on age (5) =age (4) +2

Age (4) =age (3) +2

........ Age (N) =age (n-1) +2

#include <stdio.h>

void Main ()

{

int age (int n);

printf ("Age of Fifth Person:%d\n", Ages (5));/* Output Age */

}

int age (int n)/* Recursive function for ages */

{

The int c;/* variable c is used as the variable that holds the return value of the function */

if (n==1)

c=10;

Else

C=age (n-1) +2;/* The age function during the execution of the age function */

return (c);

}

Run Result: 18

when the position of the main function is Age function, the main function is no longer Age function to declare

Age function is called altogether 5 times, of which four times is in Age called in a function, that is, a recursive call 4 times, and the other one is used in the main function.

Practice 2 : Using recursive method to find n!.

problem-solving ideas: recursive thinking and recursion is the opposite, is directly from the target to solve, require n! must know n-1, to find out n!= (n-1)!*n. So the recursive formula is:

N!=1 (n=1 or n=0)

n!=n* (n-1)! (n>1)

/* Use recursive method to find n!*/

#include <stdio.h>

int main ()

{INT-FAC (int n);/* to the MODULATED function FAC declaration */

int n; int y;

printf ("Please enter the value of N:");

scanf ("%d", &n);

Y=FAC (n);/* Call the FAC function */

printf ("%d!=%d\n", n,y);

return 0;

}

int FAC (int n)/* Define FAC function */

{

int F;

if (n<0)

printf ("Data Error");/* Incorrect if input n<0 */

else if (n==0| | N==1)

f=1;/* when n=1 or n=0 equals 1*/

else F=fac (n-1) *n;/* recursive call fac*/

return (f);/*f is n!*/.

}

/* Beg again N ! Be sure to remember the range of int values to prevent overflow * /

Iii. internal functions and external functions

1 intrinsic function Concept: If a function can only be called by other functions in this file, it is called an intrinsic function

When defining intrinsic functions, add static to the function name and function type, that is: static type name Function name (formal parameter list)

2 external function Concept: If a function is defined, the leftmost side of the first part of the functions is added with the keyword extern, then this function is an external function that can be called by other files.

If the first part of the function can be extern int fun (int a, int b)

If you omit extern when defining a function, the default is an external function

Conversion between the four and the binary

Decimal to-and-binary: binary as 6, algorithm

The result is: 0110, note: The result should be written from bottom to top.

Binary to-and-decimal: such as 0110 decimal equals: 22*1+21*0+20*0=6.

In short: decimal to binary, except 2 take the remainder; the binary is converted to decimal, multiplied by the power of 2.

Decimal---> Octal

The 10 binary number is converted to a 8 binary method, similar to the method converted to 2, the only change: the divisor is changed from 2 to 8.

Decimal---> Hex

The 10 binary number is converted to a 16 binary method, similar to the method converted to 2, the only change: the divisor is changed from 2 to 16.

    • Decimal: 0-9, full 10 in 1.
    • Octal: 0-7, full 8 in 1, denoted by the beginning of 0.
    • Hex: 0-9,a-f, full 16 in 1, denoted by the beginning of 0x.

Exercises:

/*9 , the programming implementation converts any decimal integer into any R-binary number (R is between 2-16). (c language) */

#include <stdio.h>

void Main ()

{

    // define integer variable s,r representing decimal number and R binary number respectively

int s,r,a;

int i;

     // defines the array to hold the converted data

int cs[100];

    // Prompt user for data entry

printf (" Please enter a decimal integer:");

    // Receive user input

scanf ("%d", &s);

printf (" Please enter the number of inputs that need to be converted (between 2-16):");

scanf ("%d", &r);

if (r<2| | R>16) {

printf (" input out of range \ n");

}else{

    // decimal conversion Arbitrary binary is: decimal/arbitrary input redundancy

for (i=0;s!=0;i++) {

cs[i]=s%r;

S=S/R;

}

}

    // inverse printing of array traversal

for (a=i;a>=0;a--) {

printf ("%d", Cs[a]);

    /* The final result is not quite correct, the first few numbers are garbled, the specific oneself also can not find out the reason in the * *

}

}

Modular programming with functions (II.)

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.