2016-level Computer C + + assistant work (5) First lecture content __c++

Source: Internet
Author: User
Tags generator rand
Warm knowledge points First chapter II1. Code specification issues P18 indentation and Space 2. The definition of the declaration P26 P30 3. Identifier hump-style nomenclature example numberofstudent, the hump refers to a lowercase uppercase capital nested form, starting with the second word and capitalizing on the first letter. This will clearly show the words of the phrase. 4. Named constants #define Usage (refer to the previous blog fourth) 5. Numeric Type conversions ASCII P45 directory b Extra: P118 Code an ASCII table is an integer map to a character or a command class mentioned, writing is too troublesome. 6. Common mistakes: 1,2,3,4, Chapter III:1. Relational operators, conditional expression P39 operator precedence, P61 bool data type, P88 precedence, binding law here special attention is given to the problem of data type expansion, in which all types of data in the same level of expression are extended to the highest level of data type ever seen in the expression.     such as int, short appears at the same time, all variables are expanded to int, and if a double is present, all the participating operations extend to the double type. Examples of the same level of expression are as follows: 1.0 + 2 * true + 1.0*2; Multiplication First: Then 2*true the first operation true is extended to int, 1.0*2 2 is extended to double (float) Addition: the first two multiplication is computed after the 1.0+2+2.0, 2 is extended to 2.0 to compute 2.switch usage P84 3.i F Statement P62 4. Common error P53 5. Random number P76 pseudo-random (supplementary below)

Extra: sizeof usage (finish the pointer part again) GetChar use below to supplement getline usage
Other questions: 1. Workaround for array bounds
The main reasons for the array to cross out are 1. The memory space is not enough so corresponding is to increase the memory space, the array to open a little larger as possible with the global array variable, the following value, avoid the use of local definition methods such as name int value[1000000];
		int main () {int cost[10000];
		int n;
		cin>>n;
   int name[n];
The global variable uses a heap that can open up a larger memory space, such as value, where the variables inside the function use the stack, so the amount of memory opened up is small, such as cost.
	2. Logic Error example 1:for (int i = 0;i < n; i++) cost[i] = Cost[i-1] + cost[i];
	When i = 0, i-1 =-1 occurs in a transboundary case 2:for (int i = 0;j < n; j + +) cin>>cost[i];
	Here, because the condition is J < N, it should be I < N, so the program goes into the dead loop, while the case 3:string x;
	cin>>x;
	int cost[x.length ()];
	COST[1] = 0;
	When the length of x is 0 o'clock, the access to cost[1] is clearly out of bounds .... There are many possibilities of logical errors, mainly considering boundary problems and special situations.
	
The only way to solve this problem is to think for yourself. Again, the problems that may arise from array bounds. as follows 1.   
    Question 1, which occurs at the location of the dead loop int x[100],i;
            Note that the global variable is defined here, if the local variable is based on the compiler, the memory allocation policy of the operating system,//The result may be different, resulting in an analysis of any error int main () {for (i = 0;i < i++) {
        X[100] = 0;
    The location of the following integer stores in the memory space is contiguous, x[0],x[1],x[2],......, x[99],i so x[100] = 0 The result of the operation is i = 0 so the program goes into a dead loop. The result here is to send a dead loop.
But the more complex scenario is that it causes a change in the value of a location in memory to cause a position disturbance to the final result. 2.
    No obvious error int x[100],y[100];
            Note that the global variable is defined here, and if the local variable is based on the compiler, the memory allocation policy of the operating system,//The result may not be the same, resulting in an analysis of any error int main () {for (int i = 0;i < i++)
        X[i] = i;
    cout<<x[150]<<endl;
With 1, because the memory space in which the x,y array is stored is contiguous, the following x[0],x[1],....., x[99],y[0],y[1],......, y[99] Therefore, there is no impact error.
        3. Simply out of bounds int main () {int x[100];
    X[1000] = 1;

This occurs directly across the border, and the program crashes.
    4. Access to illegal functions.
    This section involves the operating system, where the computer instruction is stored in memory, so accessing the memory space in one location may be the access to a function or instruction. For example: the location of memory address 10000 corresponds to system ("pause"), the entry of the instruction X[10], the operating system assigns x the starting memory address is 9900, then x[100] corresponds to System ("pause");
    Access to x[100] causes the program to pause.
    System ("pause") here can be replaced by any function. Of course....
 Here is just an example to help you understand that the actual situation will be more complicated or even different.


2. Can you talk about the three exercises in Chinese: easy problems
These three questions are very simple.     The first question: input to judge, and then statistics can be. The second question: For each query, for each number in the interval to determine whether is prime, and then statistics, each of the number x is the prime price of the decision is sqrt (x) Optimization: 1. Using the above primes to judge the method, preprocessing 1-1000, know each number is prime, with I                                spri[x] Records whether this x is a prime number, and then asks to count the results of this interval.                               2. Preprocessing sum[i on a 1 basis] means that the first number has several primes, then the output SUM[R]-sum[l-1].    3. The prime number Sieve method own Baidu, has the Nlog (n) and the linear sieve method. The third question: Judge the case, then pay attention to the situation of z->c (corresponding to the modulo problem)
GetChar and Getline
Getline function (reference Baidu Encyclopedia)
     Getline is not a C library function, but a C + + library function. It generates a string of characters to be read from the input stream
     until the following occurs, which causes the resulting string to end. 1 to the end of the file, 2 encounters the function of the delimiter,
     3 input to reach the maximum. The function
     ends when the function encounters a character that is equal to the end delimiter, and the function pulls the delimiter, in which
     case the delimiter is neither put back into the input stream nor put into the string to be generated.
GetChar (Reference Baidu Encyclopedia)
    reads characters from the Stdio stream, equivalent to GETC (stdin), which reads a character from the standard input. The return type is int, the
    return value is the Ascⅱ code entered by the user, and the error returns-1.
cin Cin
    ends the character input when it reads EOF, wraps, and spaces, and returns this terminator to the input stream. In
    the common use of CIN and GetChar, Getline should pay particular attention to the
following example:
int main ()
{
    int n;
    cin>>n;
    char x;
    x = GetChar ();
    Char y;
    y = GetChar ();
    cout<<n<<x<<y<<endl;
    The input 123 y
    //output 123 y
}
reads 123 through CIN, and when the space-time grid is left in the input stream, the next read gets the character x or the space,    
so when you do the first practice fourth 1153 Word reversal,
when you read the case with CIN, you should immediately use GetChar () to read ' \ n ' this line break,
or you can immediately read a line with getline, because ' \ n ' is the end of a line.
int main () {
    int n;
    cin>>n;
    string x;
    GetChar (); or getline (cin,x);
    for (int i = 0;i < n; i++) {
        getline (cin,x);
        ....
    }
}






Random number generation code: Implement a random number generating function that is the same as the effect in the library
/** here is a method for generating random numbers in a Windows system * Reference: https://www.zhihu.com/question/23705240 * Install Visual Studio to view header files Source code Microsoft Visual Studio 10.0\vc\crt\src This path, 10.0 is the version number * Implementation of Srand and the RAND function of the file for RAND.C search can be found **/#include <iostream


> #include <cstdio> #include <stdlib.h> using namespace std;

unsigned long long seed = 0;

Set seed void Test_srand (unsigned Long value) {seed = value;}
  Random number generation formula int Test_rand () {seed = seed * 214013L + 2531011L;
  unsigned int result = ((Seed >>) & 0X7FFF);
return result;

    int main () {//Output RAND function value range cout<<rand_max<<endl;

    To determine the seed that generates random numbers = 2;
    Setting up the seed, and outputting the first 10 digits srand (seed);
    for (int i = 0;i < i++) {Cout<<rand () << "";

    } cout<<endl;

    Using the same formula to generate random numbers, and output 10 digital test_srand (SEED);
    for (int i = 0;i < i++) {Cout<<test_rand () << "";
} cout<<endl; }
Code in RAND.C file
/*** *rand.c-random Number Generator * * Copyright (c) Microsoft Corporation.
All rights reserved. * *purpose: * defines rand (), Srand ()-Random number generator * ************************************************** /#include <cruntime.h> #include <mtdll.h> #include <stddef.h> # Include <stdlib.h>/*** *void srand (Seed)-seed the random number generator * *purpose: * Seeds the random n  Umber generator with the int given.
Adapted from the * BASIC random number generator.
* *entry: * unsigned seed-seed to seed Rand # generator with * *exit: * None. * *exceptions: * *******************************************************************************/void __cdecl Srand
(unsigned int seed)


{_getptd ()->_holdrand = (unsigned long) seed;}
/*** *int rand ()-Returns a random number * *purpose: * Returns a pseudo-random number 0 through 32767.
* *entry: * None.* *exit: * Returns a pseudo-random number 0 through 32767.
        * *exceptions: * *******************************************************************************/int __cdecl rand (

        void) {_ptiddata ptd = _getptd ();
Return ((Ptd->_holdrand = Ptd->_holdrand * 214013L + 2531011L) >>) & 0X7FFF);
 }





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.