C # array of structures exercises

Scheduling

morning

review

----------------------------------------------------

 1. Review: Bubble Sort

 

 Problems: There is a set of data, before ordering: -110--20-30

      Requested to sort in ascending order

 ---------------------------------------------------

 answer:

 1) sorting process:

 

 The first round: 3, maximum value, take the last 10

 

         1) -110 exchange are not -110--20-30

         2) 10-20 exchange -1-2010-30

         3) 10-30 exchange -1-20-3010

-------------------------------------------------

  NOTE: After the first round, the array becomes: -1-20-3010

-----------------------------------------------  

 The second round: 2, -1-20-30 

          1)-1>-20      -20  -1     -30

          2) -1>--30     -20  -30    -1

------------------------------------------------    

   NOTE: After the first round, the array becomes: -20-30-110   

-----------------------------------------------

 

 Third round: -20-30-110

       1) -20 > -30    -30  

 

           -30  -20   -1    10

-----------------------------------------------

   NOTE: After the third round, the array becomes: -30-20-110

-----------------------------------------------  

  2) Summary: 4 number in ascending sort order, the time for sorting a double loop:

 

  Round 1: 3

  Round 2: 2

  Round 3: 1

 

  

  // Note: bubble sort, after each round, will find that a large number of

    The first round to find the maximum number, the second round of the second large number of reciprocal found,

    The third round found a large number of third countdown

    .... Therefore, the inner loop cycle conditions do not always have to compare to last

    After each comparison round, found a large number, the next time, do not need to compare

    Numbers at this position, so j <n.Length -1-i

 

  3) core code:

  for ( int i = 0 ; i < n.Length-1 ; i++ )//1

  {   

     for( int j = 0 ; j < n.Length-1-i ; j++ )//1 2

     {

         if( n[j]>n[j+1] )

         {

             int temp = n[j];   

             n [j] = n [j + 1];

             n [j + 1] = n [j];

         }

     }                  

  }

 

 ---------------------------------------------------

 2. Review of two-dimensional array

 

 

 1) Definition: int [,] score;

      

 

 2) Initialization: The number and value of each element of the element

 

   Static:  

       int[,]  score = { {50,60},{60,70},{59,40} }

   Dynamic: new

       int[,]  score = new  int[3,2];

 

 3) Features:

 1 represents a two-dimensional array: rows and columns [row, column]   

 2. Each element in the two-dimensional array of rows and columns with the description described

    Array name [index value of a row, column index]

 3. The two-dimensional array traversal

    // number of rows in a two-dimensional array

 

    for (int i = 0 ; i < n.GetLength(0) ; i++ )

    {// number of columns in a two-dimensional array

        for( int j = 0 ; j <n.GetLength(1); j++ )

        {

             Console.WriteLine( score[i,j] );

        }

    }

  Note: The array name .GetLength (0) to obtain two-dimensional array is the number of lines

      Array name .GetLength (1) to obtain a two-dimensional array has how many columns

 

 

Exercise: Defining a two-dimensional array of three rows and three columns,

        And find the two-dimensional array diagonal sum.

 

   See Code: DAY06_01_Array02 project LianXi2.cs

 

 

  4) the standard two-dimensional array, usage scenarios:

     Rules for operation of rows and columns of data: for example:

        Kind of game Sanxiaodan

        Minesweeper

 

  

 

  ---------------------------------------------------

    

 Site verification code

 

   Supplementary features: four verification can not have repeating characters

   Description:

 

   See Code: DAY06_01_Array02 next project:

             Two classes under the "Site Code" folder:

             CodeHomeWork1.cs --- untreated repeat

             CodeHomeWork2.cs --- codes the process repeated

     

  -----------------------------------------------------

in the afternoon

method

  

  The method also known as "function"

  Code reuse and reduce redundancy

 

  Definitions 1. Method

    static  void  Main(string[] args)

    {         

    }   

   - method name: Main (),

       Naming convention: Pascal, every single word capitalized

 

   - Methods Return Value: void empty, there is no return value. In addition to

     You can also use any type of value, which particular type of use, in accordance with

     Method to complete the function is defined.

       This means that when executed after method, there is no

       The corresponding content back to the caller.

 

   --parameter list.

         When multiple parameters, multiple parameters, separated

         These parameters constitute the argument list.

         Parameters are: parameters form, also known as "parameter"

                   The actual parameters, also known as the "argument"

       Formal parameters: refers to the method is defined, the method name in parentheses behind

           Write parameters. Of course, you can write you can not write;

 

           static void  IsNarcNumber( int number )

           {  }

 

           The above methods have a parameter, number, form parameters

 

      The actual parameters: refers to when calling certain methods in other ways,

           Some parameters of the actual value after the method names are added when calling

           As needed not need to pass parameters to be defined according to the method of

           To decide.

            int n = 60 ;

            IsNarcNumber( n );

 

  Note: The actual parameters passed to the formal parameters in the transfer process.

       The type and number of parameters to be matched. Otherwise it will error.

 

   Exercise: narcissistic number narcissistic number

   All within a few daffodils 1000

   Requirements: a write method which all data required for the 1000 daffodils

   Name of the method: FindNarcNumber ()

   Return value: void  

       370    153     371   407

       370 = 3^3  +  7^3  +  0^3  = 370

 

  See Code: Under DAY07_01_Method01 project LianXi1.cs

--------------------------------------------------------

   Exercise 2:

  

   In the above class again add a method, substantially in the form:

   void   IsNarcNumber ( int number )

   The return value of the method is void ---

   --- method has one parameter, the parameter of type int named number.

   --- Method action: determining whether a specified number is a number for daffodils.

    

 

   - Procedure: a set of methods intermediate brace block content

     The method body can write a statement, you can write multiple statements,

     Nor can one write.

 

   --....

  2. The method of calling

 

        Methods defined, in the right place you can go call.

        When you call, how to call it?

       

            The method name (); or

            Method name (parameter list);

  3. The method Return value

        After the end of the method execution, or need to return results to the caller.

        If not, return value definition method should be written: void.

        If desired, you can not write void, depends on the specific circumstances. It can be:

        int  float  double 

 

  Exercise: Define a Tools, Tools in a few ways:

        Completion of the four operations of two numbers, such as: the addition of charge of methods have

         int   Add( int a, int b )

        Then call the method in the main method, and print the results.

       

  Under CalculateUtil.cs DAY07_01_Method01 Project: Code See

 

----------------------------------------------------

  Exercise: define an array, and assigned to the array elements, then

        Find highest value in the array. Claim:

 

        Define a method: int GetMaxNumber ()

        For finding the largest element in the array.

        After finding the value of this element is returned.

                     

   Ideas:

       1. Prepare an array

       2. Find the maximum number of array

 

         1) the definition of variable max, max is variable assignment

            It assumes the entire first element of the array is the maximum value

            The value of the first element is assigned to the variable max

                int max =  n[0]  ;

 

         2) Construction of a for loop, the loop body is completed:

                if statement, take max begin with the first element determination,

                 If this element is larger than the max, max is reassigned

                 Not, no assignment to continue max value of the second determination element

                If the second element is larger than the max, max reassigned

                 Not, without values, continue to max value and the third element ...

            max is the maximum value is always stored the entire array.

 

 

       3. Return the maximum number of

  

  See Code: Under DAY07_01_Method01 project LianXi2.cs

 

----------------------------------------------------

 

  Supplementary knowledge: local variables and global variables

 

  See Code: DAY07_01_Method01 next project:

                  VarDemo1.cs

                  VarDemo2.cs

 

  The method of overloading

  5.out  ref   params

day07 Wednesday job

 

A Code title 

  :( defines three methods will do)

  1) Write a method, seeking an int and float, and a number of 

      float sum(int a,float b)

  2) a writing method, it is determined whether the number is a multiple of the input keyboard 8

      bool  isEightNumber( int number)

  3) Write a method for seeking the digital keyboard input factorial

      long  GetFac(int number)

 

  4) Find the maximum value has been achieved pm array in the array,

     Please continue to function, to find the minimum value of the array.

 

 Finally, request: call and tested in Main () method.

 

 

II. Write the operating results of the following programs (will do)

 

 class  MethodDemo1

 {

    static  void   Main(string[] args)

    {

         int a = 10 ;

         Change( a );

         Console.WriteLine( a );

    }

    static void Change( int a )

    {

         a = 20 ;

    }  

 }

 

 (2)

 class  MethodDemo2

 {

    static void Main(string[] args)

    {

         int a = 10; // a local variable

          Change( a );

         Console.WriteLine( a );/10

    }

 

    static int Change( int  a )

    {

         a += 20 ; //a =a +20 =30

         return a;

    }  

 }

 

 (3)

 class  MethodDemo3

 {

    static void Main(string[] args)

    {

         int a = 10 ;

         a = Change( a );

         Console.WriteLine( a );

    }

    static int Change( int b )

    {

         b+=20 ;

         return b;

    }  

 }

 

Third, the programming problem

 

 1. test: application of the array (will do)

      Define a two-dimensional array of 3 rows 3 columns, using a random number assigned to an array element

      Random number (0,10) between.

      Requirements: find an array of maximum number and minimum number.

      Tip: Find the maximum number of two-dimensional array and minimum number, and are one-dimensional arrays

      the same. Max is the definition of variables, and then build a double for the cycle,

      Judgment in the inner body of the loop.

 

 2. test: Application (OPTIONAL) array

    Define a method, a method for:

    Inserted into the specified position in the array element values ​​specified

    

    int [] num = {1, -100,2, -200,3};

    int[] newNum = {*,*,*,*,*,* }

    Please enter the location you want to insert elements: 2

    Enter to insert the element values: 4

    

   1. Define a new array, a new array length of the original array length +

   2. Copy the num array of all the elements into a new array

     int[] newNum = {1,-100,2,-200,3,* }

   3. The position at which the user is prompted to insert elements: 2

     It prompts the user to insert elements: 1000

   4. Finally traverse the new array:

          {  1,-100,2,-200, 3,  0 }

 

 

 Fourth, to expand the study questions.

  Methods using lessons learned:

  When we use the method will find that you want to define a method to complete one

  Functional form, the method can have a variety to write, how to find the most reusability

  High, and more readable, higher quality way to do that?

  Generally follow two rules:

 

  1) The method is only responsible for completion, the function which represents the package code block period,

      When you define, just give it the appropriate parameters. In use, as long as the caller

      Suitable actual value transmitted to the form definition.

 

  2) In the method, generally do not have a printout statements. If you really can not

      Write printout statement, then if the method is to calculate the corresponding

      As a result, the results tell the caller how to do? ---- use the return value.

 

      That is, when defining a method, the method according to the completion of the function, as far as possible to

      Methods appropriate return value, it is best not to be empty.

      The advantage is: when the caller invokes this method, as long as the delivery type

      Same, the same number of parameters, the method can help us realize the function,

      As for how much value is passed, in fact, the method does not care.

      After the operation method done, will the results are returned to the caller.

      If the caller needs the results, you can define variables or do other processing.

      If not, it will not affect the robustness of the program.

 

 

 The following three methods are defined in the Test class, calculating two functions are performed

  Number and. According to the rules of the use of methods, analysis methods which are integrated

  The most appropriate, and then call these methods in the Main () method, respectively, experience

  Use methods.

 

 

 class  Test

 {

   static void Main(string[] args)

   {

 

 

   }

 

   static  void   Add1( int a, int b )

   {

        int  sum  = a + b ;

        Console.WriteLine( sum );

   }

 

   static  int   Add2( int a, int b )

   {

        return  a + b ;

   }

 

   static  int   Add3( )

   {

        int a = 10 ;

        int b = 20 ;

        int sum = a + b ;

        Console.WriteLine( sum ); ;

   }

 

 }

  

How to introduce BigInteger

Note:

If the factorial of a large number, long type of the received result becomes 0,

 The reason: factorial results have exceeded the maximum range of long type. How to solve?

 Reference BigInteger.

 The first step: Right-click on the current "reference" in the project --- Add Reference,

  Search Numerics content in the dialog box that opens, and then find the check

  Finally, select "OK."

 Step Two: In the current top .cs file, add: using System.Numerics; this sentence.

 The third step: modifying GetFac () method returns the value of type: BigInteger,

  Meanwhile, this type of modification defined in the method of the reception result variable sum is

  BigInteger type.

  NOTE: If the Main () method requires a result, the type of the result should be defined

  A BigInteger type.

 

Guess you like

Origin www.cnblogs.com/winward996/p/11502506.html