C Language Programming-2nd Chapter algorithm-the Soul of the program

Source: Internet
Author: User
Tags greatest common divisor

A program mainly includes the following two aspects of information:

1) Description of the data. In the program to specify which data to use and the type of data and the organization of the data, this is the data structure (data structure).

2) Description of the operation, which is the procedure that requires the computer to operate, that is, the algorithm (algorithm).

Algorithm + Data structure = Program

The algorithm is the soul, the data structure is the processing object, the language is the tool, the programming needs to adopt the suitable method.

The methods and steps taken to solve a problem are called "algorithms".

Example 1. Determine whether each year in the 2000-2,500 year is a leap years and output the results.

Year is set to be tested. The algorithm can represent:

S1:2000->year

S2: If year cannot be divisible by 4, the value of year is output and "not a leap year". Then go to S6 and check the next vintage.

S3: If year can be divisible by 4 and cannot be led by 100, the value of year is output and "is a leap year". Then go to S6.

S4: If year can be divisible by 400, output year's value and "is leap year", then go to S6.

S5: Output year value and "not leap year"

S6:year+1->year

S7: When year<=2500, go to S2 to continue execution, otherwise the algorithm stops.

Example 2.

If sign represents the numeric symbol in front of the item currently being processed, the term represents the value of the current item. Sum represents the summation of the current item, Deno is the denominator of the current entry, and this example algorithm can be written as:

S1:sign=1

S2:sum=1

s3:deno=2

S4:sign= ( -1) *sign

s5:term=sign* (1/deno)

S6:sum=sum+term

S7:deno=deno+1

S8: If Deno<=100 returns S4, the algorithm ends.

Example 3. Give a positive integer greater than or equal to 3 to determine if it is a prime number.

The idea of solving a problem: the so-called prime (prime) is a number that cannot be divisible by any other integer except 1 and the number itself. For example, 13 is a prime because it cannot be divisible by 2,3,4,...., 12.

The method of judging whether a number n (n>=3) is prime is very simple: n is used as a dividend, each integer is 2~n-1 as a divisor, and n is prime if it cannot be divisible.

The algorithm can be represented as follows:

S1: Enter the value of n

s2:i=2 (i as divisor)

S3:n is removed by I, the remainder R

S4: If r=0, indicates that n can be divisible by i, then output n "is not prime", the algorithm ends; otherwise execute S5

S5:i+1->i

S6: If i<=n-1, returns S3, otherwise outputs N and "is prime", then ends.

In fact, N does not have to be 2~n-1 integer except that it is only necessary to be 2~n/2 between integers, even if only the integer between the 2~n can be removed.

S6 steps can be changed to:

S6: If I<=n, returns S3, otherwise the algorithm ends.

2.3 Characteristics of the algorithm

1) Poor sex

2) Certainty

3) with 0 or more inputs

4) has one or more outputs

5) Validity

2.4 How to represent an algorithm

2.4.1 using natural language representation algorithm

2.4.2 using flowchart to express algorithm

The role of a diamond box in Figure 2.3 is to judge a given condition and determine whether a given condition is established to perform the subsequent operation. It has an entrance, two exits.

Connection points (small circles) are used to connect the process lines drawn in different places.

A flowchart consists of the following sections:

1) The box that represents the corresponding action. 2) process line with arrows. 3) necessary text description inside and outside the box

Note: The process line does not forget to draw the arrows, as it reflects the sequence of the process.

2.4.33 basic structures and improved flowchart

1. Disadvantages of traditional flowchart

2. Three basic structures:

1) Sequential structure.

2) Select the structure.

3) Cycle structure.

Also known as repeating structure, that is, repeated execution of a certain part of the operation. There are two types of loop structure: 1. When type (while type) loop structure. 2. Until type (until type) cycle structure

2.4.4 using n-S flow chart representation algorithm

2.4.5 using pseudo-code representation algorithm

Pseudo-code describes an algorithm using text and symbols between natural and computer languages.

The pseudo-code writing algorithm does not have a fixed, strict grammar rules, can be used in English, can also be mixed in Chinese and English. As long as the meaning is clear, easy to write and read, the written format should be written in a clear and easy-to-read form.

2.4.6 algorithm for computer language representation

4. Will ask 5! In C language

 #include <stdio.h> #include  <stdlib.h> int   main () { int      i,t;    T  =1  ;    I  =2  ;  while  (I<=5          =t*i;    I  =i+1  ; } printf (  " %d\n  "  ,t);  return  0  ;}  

5. The above question 2 is expressed in C language

#include <stdio.h>#include<stdlib.h>intMain () {intsign=1; Doubledeno=2.0, sum=1.0, term;  while(deno<= -) { sign=-Sign ; term=sign/Deno; Sum=sum+term ; Deno=deno+1; } printf ("%f\n", sum); return 0;}

2.5 Structured programming methods

The basic idea of the structured programming method is that the solving process of a complex problem is carried out in stages, and the problems in each stage are open to the extent that people are easy to understand and deal with.

Specifically, the following methods are used to ensure that structured procedures are achieved:

1) from top to bottom;

2) gradual refinement;

3) Modular design;

4) structured coding.

6. The prime number between the 100~200 is output.

7. Ask for a greatest common divisor of two m and N.

C Language Programming-2nd Chapter algorithm-the Soul of the program

Related Article

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.