A case study of computer-based construction algorithm for postgraduate examination

Source: Internet
Author: User
Tags repetition

Construction algorithm: Case Study 1 (counter control repetition)

To demonstrate how to develop the algorithm, we have to solve the problem of the average grade of several class. Consider the following questions:

There are 10 students in the class who take the quiz and can provide test scores (integer values from 0 to 100) to determine the average grade in the class.

The average class grade equals the sum of the whole class divided by the number of classes. The algorithm that solves this problem on the computer is the result of each person, the average calculation, and then the printing results.

The following pseudo-code lists the actions to be performed, specifying the order in which these operations are performed. We use counters to control repetition (counter-conttrolled repetition) each time one loses each person's score. This method uses counter (counter) variables to control the number of times a set of statements are executed. In this case, the counter is more than 10 o'clock and stops repeating. This section describes the pseudo-code algorithm (2.6) and the corresponding program (2.7). The following section describes how to develop this pseudo-code algorithm. Counter control repetition is often referred to as determining repetition (definiterepetition), since the loop executes before the known number of repetitions.

Note that the total and counter are referenced in the algorithm. Total variables are used to accumulate the sum of a series of values. The counter variable is used for counting, where the number of results of the loser is calculated. The total number of variables should usually be initialized to 0 before being used in the program, otherwise the sum will include the total number of memory addresses stored in the original value.

  Set total to zero   set grade counter to one while      grade counter are less than or equal to ten              Input the next GRA De Add the grade i.to the total Add one to the              grade counter      Set the class average to the total divided by ten      Print the class average

Figure 2.6 Pseudo-code algorithm for repetitive resolution of the average grade problem in the class with counter control

Fig. 2.7:fig0207.cpp//Class Average program with counter-controlled repetition#include <iostream.h>int main () { int total,          //Sum of Gradesgradecounter,  //number of grades Enteredgrade,        //one gradeaverage;      Average of grades//initialization phasetotal = 0;                           Clear totalgradecounter = 1;                     Prepare to loop//processing phasewhile (Gradecounter <=) {          //loop timescout << "Enter grade:"; c7/>//Prompt for inputcin >> grade;                   Input gradetotal = total + grade;            Add grade to Totalgradecounter = Gradecounter + 1;    increment counter}//termination phaseaverage-total/10;             Integer divisioncout << "Class average is" << average << endl;return 0;  Indicate program ended successfully}

Output Result:

    Enter grade:98    Enter grade:76    enter grade:71    enter grade:87    enter grade:83    enter grade:90    En ter grade:57    Enter grade:79    enter grade:82    enter grade:94    Class average is 81
Figure 2.7 C + + program and sample output with counter control for repetitive resolution of the class's average score problem

Depending on the usage, the counter variable should normally be initialized to 0 or 1 (the following examples are illustrated separately). Uninitialized variables will contain garbage value "garbage" value, also known as undefined value (undefined

Value), which holds the last values stored in the memory address for the variable.

Common Programming Error 2.6

If you do not initialize the counter and the sum variable, the result of the program may be incorrect. This is a logic error.

Programming Tips 2.7

Be sure to initialize the counter and the sum variable.

Programming Tips 2.8

Each variable is declared in a separate row. Note that the average calculation in the program produces an integer result. In fact, the sum of the results in this example is 817, divided by 10 should be 81.7, is a number with a decimal point, the next section will describe how to deal with this value (called floating point).

Common Programming Error 2.7

In the counter control loop, because the loop counter (each person loops plus 1 o'clock) is 1 more than the maximum legal value (for example, from 1 to 10 o'clock 11). Therefore, the calculation of the counter value after the loop usually results in a difference of 1 error.

In Figure 2.7, if line 21st is calculated with Gradecounter instead of 10, the output of this program shows the value 74.

2016 Postgraduate policy http://www.kyjxy.com/yuanxiao/zhengce/
2016 Graduate Examination Mathematics data http://www.kyjxy.com/shuxue/ziliao/
2016 Postgraduate Review Method http://www.kyjxy.com/zhuanshuo/

A case study of computer-based construction algorithm for postgraduate examination

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.