C + + learning notes (array, function)

Posted by timclaason on Thu, 27 Feb 2020 09:21:49 +0100

This note is mainly from the tutorial https://www.bilibili.com/video/av41559729?p=1

5 x array

5.1 summary

An array is a collection in which data elements of the same type are stored.

Feature 1: each data element in the array is the same data type.
Feature 2: array is composed of continuous memory locations.

5.2 one dimensional array

5.2.1 definition method of one-dimensional array

1. Data type array name [array length]
2. Data type array name [array length] = {value 1, value 2 }; / / if you do not fill in all the initialization data, 0 will be used to fill in the remaining data.
3. Data type array name [] = {value 1. Value 2 }; / / when defining an array, it must have an initial length (at least one data in {})
The subscript of an array element is indexed from 0.

5.2.2 one dimensional array name

Purpose of one-dimensional array name:

  1. You can count the length of the entire array in memory.
  2. You can get the first address of the array in memory.
#include< iostream>
using namespace std;

int main()
{
    //Array usage
    //1. It can count the length of the whole array in memory.
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
    cout << "The memory space occupied by the whole array is:" << sizeof(arr) << endl;
    cout << "The memory space occupied by each element is: " << sizeof(arr[0]) << endl;
    cout << "The number of elements in the array is: " << sizeof(arr) / sizeof(arr[0]) << endl;


    //2. Get the first address of the array in memory.
    cout << "Array first address is: " << arr << endl;//16 binary system
    cout << "Array first address is: " << (int)arr << endl;//Strong to decimal
    cout << "The address of the first element in the array is: " <<  (int)&arr[0] << endl;
    cout << "The address of the second element in the array is: " << (int)&arr[1] << endl;//Next to the first, it's only four bytes short.



    //The array name is a constant and cannot be assigned.
    //arr = 100; error reporting




    system("pause");
     return 0;

}

The array name is a constant and cannot be assigned.

Exercise case: element inversion

#include< iostream>
using namespace std;

int main()
{
    //Implement array element inversion

    //1. Create array
    int arr[5] = { 1,3,2,5,4 };
    cout << "Element array before inversion:" << endl;
    for (int i = 0; i < 5; i++)
        cout << arr[i] << endl;

    //2. Reverse
    //2.1 record the position of the starting subscript
    //2.2 record the position of the end subscript
    //2.3 exchange of elements of start subscript and end subscript
    //2.4 start position + +, end position--
    //2.5 cycle 2.1 until start position > = end position
    int start = 0;//Start subscript
    int end = sizeof(arr) / sizeof(arr[0]) - 1;//End subscript

    while (start < end) //Important: stop the condition, I didn't think of it before.
    {
        //Interchange code
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        //Subscript update
        start++;
        end--;
    }


    //3. Print the inverted array
    cout << "After array element inversion:" << endl;
    for (int i = 0; i < 5; i++)
        cout << arr[i] << endl;



    system("pause");
     return 0;

}

Stop condition attention

5.2.3 bubble sorting

Function: the most commonly used sorting method to sort the elements in the array.

  1. Compare adjacent elements, and if the first is larger than the second, exchange them.
  2. Do the same for each pair of adjacent elements, and find the first maximum value after execution.
  3. Repeat the above steps for - 1 times of comparison until no comparison is required.

Example: sort the array {4, 2, 8, 0, 5, 7, 1, 3, 9} in ascending order

#include< iostream>
using namespace std;

int main()
{
    //Bubble sorting is used to implement ascending sequence.
    int arr[9] = { 4,2,8,0,5,7,1,3,9 };

    cout << "Before sorting: " << endl;
    for (int i = 0; i < 9; i++)
    {
        cout << arr[i] << " ";

    }
    cout << endl;

    //Start bubble sorting
    //The total number of sorting rounds is the number of elements - 1
    for (int i = 0; i < 9 - 1; i++)
    {
        //Inner layer cycle contrast times = number of elements - number of current rounds - 1
        for (int j = 0; j < 9 - i - 1; j++) 
        {
            //If the first number is larger than the second, exchange two numbers
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;


            }

        }
    }
    //Results after sorting
    cout << "After sorting: " << endl;
    for (int i = 0; i < 9; i++)
    {
        cout << arr[i] << " ";

    }
    cout << endl;

    system("pause");
     return 0;

}

Note that the total number of sorting rounds is element number - 1, while the number of inner layer cycle comparison = element number - current round number - 1

5.3 2D array

Two dimensional array is to add one more dimension to one dimensional array.

5.3.1 definition of 2D array

There are four ways to define a 2D array:

  1. Data type array name [number of rows] [number of columns]
  2. Data type array name [number of rows] [number of columns] = {data 1, data 2}, {data 3, data 4}}
  3. Data type array name [number of rows] [number of columns] = {data 1, data 2, data 3, data 4}
  4. Data type array name [] [number of columns] = {data 1, data 2, data 3, data 4}
Suggestion: the above four definition methods, the second is more intuitive, improve the readability of the code.

5.3.2 name of 2D array

Purpose:

  • View the memory space occupied by the binary array.
  • Gets the first address of the 2D array.

Example

#include< iostream>
using namespace std;

int main()
{
    //2D array array name
    int arr[2][3] = 
    {
        {1,2,3},
        {4,5,6}
    };
    cout << "2D array size: " << sizeof(arr) << endl;
    cout << "One row size of 2D array: " << sizeof(arr[0]) << endl;
    cout << "2D array element size: " << sizeof(arr[0][0]) << endl;

    cout << "Rows of 2D array: " << sizeof(arr) / sizeof(arr[0]) << endl;
    cout << "Number of columns in 2D array: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

    //address
    cout << "First address of 2D array" << (int) arr << endl;
    cout << "First row address of 2D array" << arr[0] << endl;
    cout << "Address of the second line of 2D array" << arr[1] << endl;

    cout << "First element address of 2D array" <<(int) &arr[0][0] << endl;
    cout << "Address of the second element of the 2D array" << &arr[0][1] << endl;


   

    system("pause");
     return 0;

}

6 function

6.1 summary

Function: encapsulate a piece of frequently used code to reduce repetitive code
A large program is generally divided into several program blocks, each of which realizes specific functions.

6.2 definition of function

There are five steps to define a function:

  1. return type
  2. Function name
  3. parameter list
  4. Function body statement
  5. return expression
    Syntax:
Return value type function name (parameter list)
{
    Function body statement




    return expression

}
    //Definition of function
    //Syntax:
    //Return value type function name (parameter list) {function body syntax return expression}
    //Add function to add two integers and return the result
    int add(int num1, int num2)
    {
        int sum = num1 + num2;
        return sum;
    }

6.3 function call

Function: use the defined function.
Syntax: function name (parameter)

#include< iostream>
using namespace std;

//Define addition function
//When a function is defined, num1 and num2 have no actual data
//It's just a formal parameter. It's called a formal parameter for short
int add(int num1, int num2)
{
    int sum = num1 + num2;
    return sum;
}


int main()
{
    //The add function is called in the main function.
    int a = 10;
    int b = 20;
    //Function call syntax: function name (parameter) is defined without parameters
    //a and b are called actual parameters, which are called actual parameters for short
    //When a function is called, the value of the argument is passed to the parameter
    int c = add(a, b);
    cout << "c= " << c << endl;

    system("pause");
    return 0;
}

6.4 value transfer

  • The so-called value passing is that the real parameter passes the value to the parameter when the function is called
  • When a value is passed, if a parameter changes, it does not affect the actual parameter
#include< iostream>
using namespace std;

//pass by value
//Define function and realize the exchange function of two numbers

//If the function does not need to return a value, void can be written when declaring
void swap(int num1, int num2)
{
    cout << "Before exchange: " << endl;
    cout << "num1= " << num1 << endl;
    cout << "num2= " << num2 << endl;

    int temp = num1;
    num1 = num2;
    num2 = temp;

    cout << "After the exchange: " << endl;
    cout << "num1= " << num1 << endl;
    cout << "num2= " << num2 << endl;


    //Return: when the return value is not needed, you can leave it blank
}


int main()
{
    int a = 10, b = 20;
    cout << "a= " << a << endl;
    cout << "b= " << b << endl;

    //When we do the value transfer, the parameter of the function changes and does not affect the actual parameter
    swap(a, b);
    cout << "a= " << a << endl;
    cout << "b= " << b << endl;//a and b have not changed

    system("pause");

    return 0;
}

6.5 common forms of functions

There are four common function forms:

  1. No return without reference
  2. Return without reference
  3. Return without reference
  4. Yes, there are references.

6.6 function declaration

Function: tells the compiler the function name and how to call the function. The actual body of a function can be defined separately

A function can be declared more than once, but a function can only be defined once
#include
using namespace std;

//Function declaration
//Compare function to compare two integer numbers and return a larger value


//Tell the compiler the existence of the function in advance, and use the function declaration
//Function declaration
//A declaration can be written more than once, but a definition can only be written once
int max(int a, int b);
int max(int a, int b);
int max(int a, int b);
int max(int a, int b);

int main()
{
    int a = 10;
    int b = 20;

    cout << max(a, b) << endl;

    system("pause");

    return 0;
}


//Definition
int max(int a, int b)
{
    return a > b ? a : b;
}

6.7 document preparation of functions

Function: make the structure of the code clearer.
There are four steps to write function files:

  1. Create header file with suffix. h
  2. Create a source file with the suffix. cpp.
  3. Write the declaration of the function in the header file.
  4. Write the definition of the function in the source file.

Header file swap.h

#include< iostream>
using namespace std;

//Function declaration
void swap(int a, int b);

Source file swap.cpp

#include"swap.h"

//Definition of function
void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;

    cout << "a= " << a << endl;
    cout << "b= " << b << endl;
}

program

#include
using namespace std;
#include"swap.h"

//1. Create header file with suffix. h
//2. Create a source file with the suffix. cpp.
//3. Write the declaration of the function in the header file.
//4. Write the definition of the function in the source file.


int main()
{
    int a = 10;
    int b = 20;
    swap(a, b);

    system("pause");

    return 0;
}




 
Finally, welcome to my personal blog Frog listening to Zen blog

Published 3 original articles, praised 0 and visited 16
Private letter follow