Object oriented programming -- functions and arrays

Posted by Arab Prince on Mon, 17 Jan 2022 00:03:10 +0100

1, Purpose of the experiment:

  1. Master the definition and use of functions; Master the method of function call; Master the mechanism of function parameter transfer; Master the use methods of global variables, local variables and static variables; Master the method of function overloading.

  2. Master the definition, reference and initialization of one-dimensional array and two-dimensional array; Master the relationship between character array and string and the representation of string variables, and be proficient in the application of string processing functions.

  3. Master the definition, reference and operation of various types of pointers; Master array pointers and pointer variables pointing to arrays, as well as string pointers and pointer variables pointing to strings.

2, Experiment content:

  1. Enter 10 integers, exchange the smallest number with the first number, and exchange the largest number with the last number.
    (1) prompt
    The input 10 integers are stored in the array. The smallest of the 10 integers is exchanged with the first element of the array, and the largest number is exchanged with the 10th element of the array. Others remain unchanged.
    (2) requirements
     define 3 functions:
void input(int number[ ]);     /*This function implements the input of 10 elements*/
void max_min_value(int array[ ]); 
/*This function exchanges the corresponding elements of the array*/
void output(int array[ ]);  /*This function implements the output of 10 elements*/

In the main function, the above 3 functions are invoked to realize the function of the title request.
Source code:

#include <iostream>
using namespace std;
//Declare three functions to be used
void input(int number[]);
void max_min_value(int array[]);
void output(int array[]);

int main()
{
    int number[10]={};
//Defines a one-dimensional array that stores ten integers
    input(number);
    max_min_value(number);
    output(number);
//Call three functions
    return 0;
}
//Define three functions
void input(int number[])
{
    int i;

    cout<<"Please enter ten integers"<<endl;
    for(i=0;i<10;i++)
        cin>>number[i];
}
//Complete the input of ten integers with the for loop
void max_min_value(int array[])
{
    int i,max_,min_,temp1,temp2,max_num,min_num;
//Define the auxiliary variable i, and store the variables max_ min_,
//Define null variables temp1 and temp2 for exchanging positions, which are used to store the position of array elements at the maximum and minimum values
    max_=array[0];
    min_=array[0];
    for(i=1;i<10;i++)
        if(array[i]>=max_)
            {
                max_=array[i];
                max_num=i;
            }
    for(i=1;i<10;i++)
        if(array[i]<=min_)
            {
                min_=array[i];
                min_num=i;
            }
//Complete the determination of maximum and minimum values
    temp1=array[max_num];
    array[max_num]=array[9];
    array[9]=temp1;
    temp2=array[min_num];
    array[min_num]=array[0];
    array[0]=temp2;
//Complete the exchange between the maximum and minimum values and the last and first bits of the array
}
//Use the for loop to output the completed array, with elements separated by spaces
void output(int array[])
{
    int i;

    for(i=0;i<10;i++)
        cout<<array[i]<<" ";
}

Operation screenshot:

  1. Write a program to count the number of words in a string "C++ is beginer's language", and store each word in a row in the two-dimensional array [] [], that is, the elements in the array: array[0] = "C + +", array[1] = "is", array[2] = "beginer's", array[3] = "language". Finally, output each word.

Source code:

#include <iostream>
using namespace std;

int main()
{
    int i,m=0,n=0,k=0;
    char str[]="C++ is beginer's language";
//Define the string required by the topic and auxiliary variables i, m, n,k
    for(i=0;;i++)
        {
            if(str[i]==' '&&str[i+1]!=' ')
                k++;
            if(str[i]=='\0')
                break;
        }
    char array[k][10]={};
//Use the for loop to count the number of words k, so as to define a two-dimensional array and initialize it
    for(i=0;i<26;i++)
    {
        if(str[i]!=' ')
        {
            array[m][n]=str[i];
            n++;
        }
        else {array[m][n]='\0';m++;n=0;}
    }
//The character of the string is transferred to a two-dimensional array. When a space is encountered,
//End input to one line of the two-dimensional array and add the end character '\ 0'
    for(m=0;m<4;m++)
        for(n=0;n<10;n++)
            if(array[m][n]!='\0')
                cout<<array[m][n];
            else {cout<<endl;break;}
//Output the two-dimensional array with two for loops. When meeting the '\ 0' added above,
//Stop outputting one line, add a newline at the end, jump out of the loop, and start outputting the next line
    return 0;
}

Operation screenshot:

  1. n people form a circle, start counting from the first person, and the person who checks in 5 will exit the circle (the white in the figure indicates the exit circle), then start counting from the next person, and ask the last person who is left who is the first.

    Tips:

(1) Create a structure:
struct Node / / defines a node structure type
{int num; / / sequence number
char name[10]; // full name
struct Node *next;
} stNode;
And set the header pointer to the global variable stNode *head;

(2) It is required to establish several functions in the program:

  • void initStLink() / / initialize the linked list
  • void CreateStLink() / / create a circular linked list
  • void dispStLink() / / output linked list information
  • stNode *find() / / people with a message count of 5 leave the queue

The above functions are invoked in the main function to fulfill the program requirements.

Learning and mastering:

When calling a function and passing array parameters, pay special attention to the format of formal parameters and arguments. When calling a function, the array is passed with an address. A new array will be created inside the function, but the size and content are the same as the arguments. Therefore, the return of the array can not be directly returned by return, but by reference or pointer, To directly return the original array. Strings end with "\ 0", so they can be used as the termination of traversal strings. When spaces are encountered, you can directly match them with = = "", a space in double quotation marks.

When writing a program, we should be good at using functions to complete some operation processes that can be encapsulated and reused for many times, so as to make the program more concise and clear. When the program is improved, for some input operations or other parts, you can add more output prompt statements to the source code to make the program run more step-by-step.

Topics: C++