C language - the third assignment

homework requirement one

Problem 6-1 Output the English name of the month

Topic 6-2 Finding the day of the week

Problem 6-3 Calculating the longest string length

Problem 6-4 Specifying the position to output a string

Homework: C Advanced third homework (2) There is no idea, the knowledge point is not understood, and it is not completed.

homework requirement two

Problem 6-1 Output the English name of the month

1. Design ideas
(1) Mainly describe the problem algorithm
Step 1: The problem requires the design of the getmonth function to return the English words of the input n corresponding months.
The second step: define the variable i, define the string array, containing the English words of each month.
Step 3: Use the for loop to traverse the array, and return the corresponding month of n through the if judgment. Because the input n is a month, the corresponding month must be greater than or equal to 1, so the if condition in the loop is n=i+1.
Step 4: Finally, if n is not a month, return NULL.
(2) Flowchart
Main function:

Call function:

2. Experimental code

char *getmonth( int n )
{
  int i;
  char *month[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
  for(i=0;i<12;i++)
  {
    if(n==i+1)
    {
      return *(month+i);
    }
  }
  if(n<=0||n>=13)
  {
    return NULL;
  }
}

3. Problems encountered in the debugging process of this question and solutions There is
no problem with this question, but when the first submission was made, there was a semicolon marked with a comma, which caused a compilation error. I found it through dev-c++ compilation, and it has been corrected. I should pay attention to it next time.

Topic 6-2 Finding the day of the week

1. Design ideas
(1) Mainly describe the problem algorithm
Step 1: This problem requires the design of the getindex function to find the serial number corresponding to the input week.
Step 2: Define the variable i, and define the return variable "xuhao" with a value of -1, which is used as the return value. Define an array of strings, containing the English names of the seven weeks. Pay attention to put Sunday at the top according to the corresponding serial number.
Step 3: Use the for loop to traverse the array, use if to judge, and assign i to "xuhao" when the input string is found to be consistent with the string in the array.
Step 4: Return to "xuhao".
2. Experimental code

int getindex( char *s )
{
  int i;
  int xuhao=-1;
  char *week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  for(i=0;i<7;i++)
  {
    if(strcmp(s,*(week+i))==0)
    {
      xuhao=i;
    }
  }
  return xuhao;
}

3. Problems encountered during the debugging process of this question and their solutions
Question: The if judgment condition uses "==" to directly judge whether the strings are equal, and the answer is wrong.
Solution: It is found through online query that the use of relational operators is not supported to compare strings in C language, and functions are required. Use the strcmp function to compare and the answer is correct.

Problem 6-3 Calculating the longest string length

1. Design ideas
(1) Mainly describe the problem algorithm
Step 1: This problem requires the realization of a function to calculate the length of the longest string in the pointer array s with n elements.
Step 2: Define the loop variable i, the maximum value max and the length len.
Step 3: Use the for loop to traverse the array, use the strlen function to find the length of each string, max is initially the length of the first string, and if it is longer than it, assign len to max.
2. Experimental code

int max_len( char *s[], int n )
{
  int i;
  int max=0,len=0;
  for(i=0;i<n;i++)
  {
    len=strlen(*(s+i));
    if(max<len)
    {
      max=len;
    }
  }
  return max;
}

3. Problems encountered in the debugging process of this question and their solutions
have been corrected due to the typo of letters, which resulted in compilation errors.

Problem 6-4 Specifying the position to output a string

1. Design ideas
(1) Mainly describe the problem algorithm
Step 1: This problem requires the realization of a function to output specific characters in a given string, and define variables i, j, and length len. Pointer variable p, calculate the length of the given string and assign it to len.
Step 2: Traverse the array, first use if to find the same character as ch1, and assign its address to p. Use the loop to find the same character as ch2, if it exists, output the character and wrap it, if it does not exist, output the character.
Step 3: If ch1 is not found, output a newline, and output a newline after the whole loop.
2. Experimental code

char *match( char *s, char ch1, char ch2 )
{
    int i=0,j=0;
    int len=0; 
    char *p;
    len=strlen(s);
    for(i=0;i<len;i++)
    {
        if(s[i]==ch1)
        {
            p=&s[i];
            for(j=i;j<len;j++)
            {
                if(s[j]!=ch2)
                {
                    printf("%c",s[j]);
                }
                if(s[j]==ch2)
                {
                    printf("%c\n",s[j]);
                    return p;
                }
            }
            printf("\n");
            return p; 
        }
    }
    printf("\n");
    return s+i;
}

3. There are problems and solutions in the debugging process of
this question. There is no idea for this question. After finding the code on the Internet, according to the ideas written in the code, the error "ch1 can't be found, ch2 can be found" when submitting, the reason is that the last return is wrong. Return p, change p to s+i, correct.

a programming question

There is an array of axb, which sequentially stores numbers from 1 to a*b. Where a is the first three digits of your university number, and b is the last four digits of your university number. For example, if your student number is 2017023936, then the array size is 201 x 3936, and the array is sequentially stored from 1 to 791136 (201 and The integer of the product of 3936). It is required to use the screening method to find out the prime numbers in the array and print them out. The print format is 5 prime numbers on one line, and the numbers are separated by spaces.

The specific method of the screening method is to first arrange the N natural numbers in order. 1 is not a prime number, nor is it a composite number, it should be crossed out. The second number 2 is a prime number left over, and all numbers that are divisible by 2 after 2 are crossed out. The first number that is not crossed out after the 2 is 3, leave the 3, and then cross out all the numbers after the 3 that are divisible by 3. The first uncrossed number after the 3 is 5, leave the 5, and then cross out all the numbers after the 5 that are divisible by 5. Doing this all the time will filter out all composite numbers up to N, leaving all prime numbers up to N.

homework requirement three

1. Summarize the knowledge points learned in the past two weeks and answer the following questions?
(1) How to understand an array of pointers, and how does it relate to pointers and arrays? Why can you operate on arrays of pointers with secondary pointers?
Answer: An array of pointers is an array that stores pointer elements, and it stores pointers in the form of arrays. Arrays of pointers can be used as parameters when used, similar to how ordinary arrays are used.
(2) Change any subject of the third PTA assignment (1) of C advanced to use the second-level pointer to operate on the pointer array.
(3) What are the advantages of handling multiple strings with an array of pointers? Is it possible to directly input multiple strings to an uninitialized array of pointers? Why?
2. Git address: https://git.coding.net/ZJY15/ZJY15.git

3. Comment on 3 classmates' homework this week (give the link to the commented classmate's blog in the homework), and invite 3 classmates to comment on your homework

4. Please use tables and line charts to present the number of lines of code and time spent this week (4/9 8:00~4/23 8:00), the number of blog words and time spent

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651508&siteId=291194637