C Language Blog Job--function

Source: Internet
Author: User
Tags arithmetic control characters function definition

First, PTA experimental work 1:6-3 use function to determine the total square number

The subject requires the realization of a simple function that determines whether an integer is a complete square number.
function Interface Definition:

int issquare (int n);
where n is the parameter passed in by the user, within the range of the long integer type. If n is a full square number, the function Issquare must return 1, otherwise 0 is returned.

1. PTA Submission List

2. Design Ideas
    • The first step: the main () function runs to: if (Issquare (n)), start calling function issquare (int n);
    • The second step: Define the variable I to represent the total square number;
    • The third step: first consider the input integer n, if n=0 or n=1, are eligible, that is, return 1;
    • Fourth Step: Enter the expression of I: I=sqrt (n);
    • Fifth step: If satisfied: I*i==n, that is, the total square number, return 1;
    • Sixth step: otherwise return 0;
3. Problems encountered in commissioning process and PTA submission List status note
    • Although the code is a few lines, but it is written for a long time, there are many errors, one is that the function of the understanding is not deep enough, the main function inside the conditions to move down to run, resulting in the compilation has occurred errors;

    • Second, in the idea of a lot less detail is not taken into account, such as: N=1 and n=0 when the situation, leading to part of the right;



    • Debugging process found when the i==1, do not meet the conditions of return 1, jump directly to return 0, do not perform 100 of the situation, change ideas, define special variable I, mathematical formula;



Topic 2:7-1 to find the number of combinations

The subject requires the writing of procedures, according to the formula
?? Calculates the number of combinations of M elements (m≤n) removed from n different elements.
It is recommended to define and invoke the function fact (n) to compute n!, where n is of type int and the function type is double.
Input format:
The input gives two positive integers m and n (m≤n), separated by a space, in a row.
Output format:
The output is calculated as the result = combined number of formats. The title guarantees that the result is within the double type range.

1. PTA Submission List

2. Design Ideas
    • The first step: the function fact () custom declaration;
    • The second step: Define the variable m,n for storing two positive integers, double result to store the results;
    • The third step: the main () function runs to the combinatorial number, i.e.: Result=fact (n)/(Fact (M) *fact (n-m)) here, starting to invoke the function fact ();
    • Fourth Step: Enter the called function, redefine the new variable I: implementation loop condition, double product: variable product is used to store factorial results;
    • Fifth Step: PRODCT initial value is 1.0, ensure the type is consistent;
    • Sixth step: Enter the cycle, seeking factorial;
    • Seventh Step: Return the result of the operation;
3. Problems encountered in commissioning process and PTA submission List status note
    • Programming problems, the beginning of the program itself is wrong, after a general attention to the function step, when the call function to execute a certain content can not be written correctly, I first thought is the fact (n), fact (M), fact (N-M) three values are calculated by the function and then substituting the total combination number formula, It is too complicated to write, the classmate read the point said to the function or do not understand, not the function to understand, the function of fact () is to calculate the factorial:

    • In the call function on the definition of formal parameters, resulting in compilation error, the other basic no major problem:

Topic 3:6-6 using functions to output daffodil numbers

Narcissus number refers to an n-bit positive integer (n≥3), and its number on each bit of the sum of n is equal to its own. For example:
?? The main requirement is to write two functions, one to determine whether a given integer is Narcissus number, and another to print out the number of daffodils in a given interval (m,n) in the order from small to large.
function Interface Definition:

int narcissistic (int number);
void Printn (int m, int n);
The function narcissistic determines if number is a daffodil, or returns 1, otherwise returns 0.
The function Printn prints the number of daffodils in the open interval (m, n), one row per digit. The topic guarantees 100≤m≤n≤10000.

1. PTA Submission List

2. Design Ideas
    • The first step: the main () function runs to: if (narcissistic (m)), start calling function int narcissistic (int number);
    • The second step: Define the variable I for the loop, count the number of times to hold the power, X to calculate the value of each bit, sum to store each number of bits;
    • Step three: When n! =0, the number of bits of the loop is calculated, and the value of count is obtained;
    • Fourth step: calculates each corresponding definite value of count, implements it with a for () loop, and calculates the sum of the total sums;
    • Fifth step: If satisfied: I*i==n, that is, the total square number, return 1;
    • The sixth step: compare sum with the original given in comparison, see whether equal, if that is to meet Narcissus conditions, return1; otherwise return0;
    • Seventh step: Call the void printn (int m, int n) function to print out the number of daffodils from small to large in order to meet Narcissus conditions;
    • Eighth step: Return to the main function;
3. Problems encountered in the commissioning process and PTA submission List status note
    • After writing the program display compilation error, check found that the power value is not evaluated, just to find the number of bits, modified, in the dev-c run out, do not know why, get a new correct program to compile and run is possible, but the program is not resolved;

    • In their own way of thinking, write to call the second function, do not know how to write, and then see the classmate code to understand the new definition of variables used to cycle the realization (M,N) between the number of daffodils;
Second, peer code pairs of peer review 1. Peer Reviews Photos

2. My Code, mutual evaluation of the student code
3. Where do I differ from my classmates ' code? What are the advantages? What style of code do you prefer? If the classmate code is wrong, please help to point out where the problem
    • The amount of code, my relatively complex, when n<0 and n>0 two cases separate write, two people after each other to know that the code is not too concise, when the n<0 of the direct use of the return value will be much easier, and I say that I use the return is not enough I also always like to define variables to store things, and then substituting the formula and so on, so obviously no need, this is to see her code after the realization of another point;
    • In the way I follow, in the understanding will be easier to understand, plus comments she does not have a good mark, readability is poor; but I tend to prefer her code, easy;
    • Her easy-to-write code is worth the change in my subsequent writing program; she needs to be aware of the annotated problem;
Third, this week's topic set PTA Final Ranking

Iv. Study Summary of the Week 1. What did you learn? What data types are 1.1 C languages?
    • Basic data types are: integer type, such as: int, long integer long[int], short integer short[int],unsigned[int]; character: char, floating point: single-precision float and double-precision doubles;
1.2 Character type data need to be aware of the place?
    • Note that different types of data use different format control characters, such as: int type using%d,float and double use%f,double input format control instructions must be used%LF; output long integer in the format Control description plus qualifier L (long first letter);
    • Different types of data can be mixed, but the data is first converted to the same type, then the operation;
    • scanf () function input when variable name is preceded by &;
1.3 Self-increment decrement operator?
    • The effect is to make the variable value 1 or minus 1,--N and-n-is equivalent to n= n-1,++n and n + + are equivalent to n= n+1, but ++n is the first to execute n = n+1, and then assign the value of N to ++n, and n++ is to assign N to n++ first, then n = n+1;
    • Note that their object can only be a variable, not a constant or an expression;
1.4 Operator Precedence?

Order from high to Low: logical operator:! ;
Arithmetic (monocular) operator: ++,--,+,-,*;
(binocular): +,-,%;
Relational operators:<, <=,>,,>=,
logical operators: &&
| |,
Conditional expression:? :;
Assignment operator: =,+=,-=,* =,/=,%=;
Comma operator:,;

1.5 which expressions are in C language? What do you do wrong in class, please analyze the reason here?
    • Assignment expression: variable = expression; (if the data types on both sides are different, the type on the right will be automatically converted to the left type, and then the variable is assigned);
    • Relational expression: Its value reflects the result of the relational operation, whose value is 1 or 0, and the type is integer;
    • Logical expression: A formula that joins a relational expression or logical amount with a logical operator, such as: (x>=3) && (x<=5);
    • Conditional expression: The general form is: expression 1? Expression 2: Expression 3;
    • Comma-expression: expression 1, expression 2, expression 3 Expression N;
    • Class assignment inside about integer variable division problem, such as class assignment sixth, x = 10/4, my answer at the beginning is 2.5, do the usual arithmetic processing, error;
    • There is a choice problem given some expressions to find the final result, involving the precedence order of operators, unfamiliar with their respective relationships, and prone to error;
1.6 Other content?
    • The whole chapter of the function said: "Understand the use of functions can significantly reduce the amount of code, and functions can also facilitate code reuse, enhance security;"
    • function definition: In order to accomplish different functions, a calculation can be achieved, the result of the calculation is clear; function call: Starting from the main () function, the function calls the main function to stop running, the first to execute the corresponding function, and so on after the completion of the main function, continue to start from the paused place to run; The difference between a function declaration and a function definition: a function declaration is a C statement, which can be followed by a semicolon, and the function definition does not, in order: function declaration-Function call-function definition;
    • The real participation parameter and the distinguishing between them, the real participates in the formal parameter one by one corresponds, the quantity is the same, the program runs to the function call the value of the actual parameter to the formal parameters to implement the argument transfer;
2. This week's content, you are not what?
    • There is no data type mentioned in the binary number conversion and how to express it in code;
    • And the way the letters are incremented, a,b,c. ;
    • Operators are not familiar with the use of precedence, when the operator is mixed and more prone to error, similar to some of the problems in the class is a lot of error: If A is an int type, and its value is 3, then after the expression a+= a-= a*a, the value of A is (? The final answer is-12; My answer is-3; The operation order is wrong; set X, Y, t are all int variables, then execute the statement: x=y=3; t= ++x | | ++y; After, the value of Y is __, my answer originally is 4, now feel calculate is 3, but do not know the answer is not correct;
3. Circular structure Examination Summary 1. What's wrong and how to change it
    • Have to try to write a program to do wrong is that 7-4 is less than m the largest 10 prime number, here to judge the condition of prime number is not clear, in the loop that place will want to use nested loop, is to define two variables,
      One loop outside of M's decrement, the other is the number of internal judgment primes; but from here on there is an error, so that the loop condition within the for and the code within the loop do not know how to express it, in fact only
      Need a large cycle, to complete the decline of M, and then the prime number is considered insufficiently thoughtful, leading to errors, the number of judgements should be i==0 m%;
2. is the exam result satisfactory, how to improve?
    • Not satisfied with the results of the exam, the best way to improve is to more dozen code, read more code, think more about exercise logical thinking, more problems to solve, find their own way of learning C language and other people's differences,
      Trying to correct and try to write the algorithm;
3. Other summaries
    • This time the machine test is similar to the last situation, there is not much improvement, indicating that there are significant problems in the structure of the cycle, the cycle needs to be strengthened;
    • Programming ability of people, some people in the hands of the time to write the first writing algorithm, this will not be easy to jam after the beginning of programming, you can try, gradually develop the habit of writing pseudo-code or algorithm;

C Language Blog Job--function

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.