Question 2: Please write the function fun. Its function is to find all integers between 1 and 1000 that are divisible by 7 or 11, but not divisible by 7 and 11 at the same time, and put them in the array pointed to by a , returns the number of these numbers by n.

Please add picture description

C test questions

Question 2: Please write the function fun. Its function is: find the divisible by 7 or 11 within 1 to 1000, but not divisible by 7 and 11 at the same time All integers and put them in the array pointed to by a, and return the number of these numbers through n.

ChatGPT answers as follows:

Mainly examine the following knowledge points:

  1. Loop statement: The program needs to traverse each integer from 1 to 1000 in turn, so it needs to use a loop statement to achieve.

  2. Conditional statement: The program needs to judge whether each integer meets the condition, so it needs to use conditional statement to realize.

  3. Array: The program needs to store qualified integers, so it is necessary to use an array to store these integers.

  4. Output: The program needs to output integers that meet the conditions, so it needs to use the output statement to achieve.

In general, this question comprehensively examines the mastery of basic grammar, and also requires a certain understanding of the use of arrays.

The problem-solving ideas of this program:

First, we need to traverse each integer from 1 to 1000 in turn to determine whether they meet the conditions. In the question, it is required to find an integer that is divisible by 7 or 11 but not at the same time, that is, it meets the following conditions:

  1. Divisible by 7, but not divisible by 11 at the same time;
  2. Divisible by 11, but not also divisible by 7.

Based on these two conditions, we can use an if statement to determine whether each integer meets the condition. If the condition is met, it is stored in an array.

Finally, we need to output the integers that meet the condition. We can first output the number of integers that meet the conditions, and then output each integer in turn. Since we are storing eligible integers in an array, we can traverse the array in reverse order and output each integer in turn.

In general, this topic mainly examines the mastery of loops and conditional statements, and also requires a certain understanding of the use of arrays.

1. The following is the C language code implementation:

#include <stdio.h>

int fun(int a[], int n) {<!-- -->
    int count = 0;
    for (int i = 1; i <= 1000; i ++ ) {<!-- -->
        if ((i % 7 == 0 || i % 11 == 0) & amp; & amp; i % 77 != 0) {<!-- -->
            a[count] = i;
            count + + ;
        }
    }
    return count;
}

int main() {<!-- -->
    int a[1000];
    int n = fun(a, 1000);
    printf("There are %d numbers that can be divided by 7 or 11 but not both:\\
", n);
    for (int i = 0; i < n; i ++ ) {<!-- -->
        printf("%d ", a[i]);
    }
    printf("\\
");
    return 0;
}

In the fun function, we traverse all the integers between 1 and 1000, judge whether they are divisible by 7 or 11 and cannot be divisible by 7 and 11 at the same time, if they meet the conditions, store them in the a array, and calculate The number of integers that meet the condition. In the main function, we define an array a containing 1000 elements, and call the fun function to calculate all eligible integers and print them out.

2. Another C language implementation, you can use the pointer to return the qualified integer array. The following is the code implementation:

#include <stdio.h>
#include <stdlib.h>

int* fun(int* n) {<!-- -->
    int count = 0;
    int* a = (int*)malloc(1000 * sizeof(int));
    for (int i = 1; i <= 1000; i ++ ) {<!-- -->
        if ((i % 7 == 0 || i % 11 == 0) & amp; & amp; i % 77 != 0) {<!-- -->
            a[count] = i;
            count + + ;
        }
    }
    *n = count;
    return a;
}

int main() {<!-- -->
    int n;
    int* a = fun( &n);
    printf("There are %d numbers that can be divided by 7 or 11 but not both:\\
", n);
    for (int i = 0; i < n; i ++ ) {<!-- -->
        printf("%d ", a[i]);
    }
    printf("\\
");
    free(a);
    return 0;
}

In the fun function, we use the dynamic memory allocation function malloc to allocate a space of 1000 integers, and store the qualified integers in the array a. At the same time, we use the pointer n to return the number of integers that meet the condition. In the main function, we call the fun function to obtain the qualified integer array and its number, print them out, and finally release the memory space occupied by the a array.

Compared with the first method, this method has better flexibility and scalability, but it should be noted that after using the dynamically allocated memory space, it needs to be released in time to avoid memory leaks.

3. There is also a more concise implementation method, which can use dynamic array to store qualified integers. The following is the code implementation:

#include <stdio.h>
#include <stdlib.h>

int main() {<!-- -->
    int count = 0;
    int* a = NULL;
    for (int i = 1; i <= 1000; i ++ ) {<!-- -->
        if ((i % 7 == 0 || i % 11 == 0) & amp; & amp; i % 77 != 0) {<!-- -->
            count + + ;
            a = (int*)realloc(a, count * sizeof(int));
            a[count - 1] = i;
        }
    }
    printf("There are %d numbers that can be divided by 7 or 11 but not both:\\
", count);
    for (int i = 0; i < count; i ++ ) {<!-- -->
        printf("%d ", a[i]);
    }
    printf("\\
");
    free(a);
    return 0;
}

In this implementation, we first initialize an integer variable count and a dynamic array a pointing to integers. In the loop, we judge the integers that meet the conditions and store them in the array a. When the number of stored integers exceeds the size of the array a, we use the dynamic memory allocation function realloc to reallocate a larger memory space, and copy the original integers to the new memory space. Finally, we print out the qualified integers and free the allocated memory space.

This implementation method is relatively simple. Instead of using pointers or static arrays, it directly uses dynamic arrays to store qualified integers, which has better flexibility and scalability, but it needs to be noted that after using dynamically allocated After the memory space is allocated, they need to be released in time to avoid memory leaks.

4. Another implementation is to use static array to store qualified integers. The following is the code implementation:

#include <stdio.h>

int main() {<!-- -->
    int count = 0;
    int arr[1000];
    for (int i = 1; i <= 1000; i ++ ) {<!-- -->
        if ((i % 7 == 0 || i % 11 == 0) & amp; & amp; i % 77 != 0) {<!-- -->
            arr[count + + ] = i;
        }
    }
    printf("There are %d numbers that can be divided by 7 or 11 but not both:\\
", count);
    for (int i = count - 1; i >= 0; i--) {<!-- -->
        printf("%d ", arr[i]);
    }
    printf("\\
");
    return 0;
}

In this implementation, we define a static array arr with a size of 1000 to store qualified integers. In the loop, we judge the qualified integers and store them in the array arr. We use the count variable to keep track of the number of integers stored in the array. Finally, we traverse the array arr in reverse order and output the qualified integers.

This implementation uses a static array to store qualified integers, which has better traversal efficiency and space utilization than the previous two implementations. However, its disadvantage is that the required space needs to be estimated in advance. If the array size is not enough, the array size needs to be redefined, which will easily lead to increased code complexity. At the same time, if the array is too large, it may cause stack overflow.

5. Another implementation is to use the linked list to store qualified integers. The following is the code implementation:

#include <stdio.h>
#include <stdlib.h>

typedef struct node {<!-- -->
    int data;
    struct node* next;
} Node;

int main() {<!-- -->
    int count = 0;
    Node* head = NULL;
    for (int i = 1; i <= 1000; i ++ ) {<!-- -->
        if ((i % 7 == 0 || i % 11 == 0) & amp; & amp; i % 77 != 0) {<!-- -->
            count + + ;
            Node* p = (Node*)malloc(sizeof(Node));
            p->data = i;
            p->next = head;
            head = p;
        }
    }
    printf("There are %d numbers that can be divided by 7 or 11 but not both:\\
", count);
    Node* p = head;
    while (p) {<!-- -->
        printf("%d ", p->data);
        Node* q = p;
        p = p->next;
        free(q);
    }
    printf("\\
");
    return 0;
}

In this implementation, we define a structure Node to represent the nodes of the linked list. In the loop, we determine the integers that meet the condition and store them in a new node p. We insert this new node p into the head of the linked list, so that the nodes in the linked list store qualified integers in reverse order. Finally, we print out the qualified integers and free the allocated memory space.

This implementation uses a linked list to store qualified integers, which is more scalable and flexible than the previous implementation. At the same time, it also avoids the need to resize the array frequently when using static or dynamic arrays. However, its disadvantage is that it requires additional time and space to maintain the linked list structure, and it also needs to traverse the entire linked list to output qualified integers, which is less efficient.

5. If you are satisfied with the answer, please correct me.