Claude 3 Opus's Performance in C Language Exam
FillintheBlank Questions

The working principle of computers is (A).
A = stored program and program control

Integers are represented in memory using binary (B) code.
B = two’s complement

There are (C) types of relational operators.
C = 6 (<, <=, >, >=, ==, !=)

There are (D) forms of loop statements.
D = 3 (while, dowhile, for)

Using functions requires three steps: (E), definition, and calling.
E = declaration

Array elements are stored (F) in memory.
F = contiguously

The two elements of a pointer are address and (G).
G = data type

To use math library functions, the header file to be included is (H).
H = <math.h>

Variables defined outside of functions are called (I) variables.
I = global

The keyword for defining structure types is (J).
J = struct
Short Answer Questions

The roles of break and continue statements:
The break statement is used to jump out of the current loop body and terminate the loop execution. When the break statement is encountered, the program immediately exits the loop it is in and starts executing the statement following the loop.
The continue statement is used to skip the remaining statements of the current iteration and directly proceed to the next iteration. When the continue statement is encountered, the program immediately jumps to the loop control expression and begins the next iteration.

Solution for returning multiple values from a function:
Pointers can be used as function parameters to pass the addresses of multiple variables. By modifying the values of these variables inside the function, the effect of returning multiple values can be achieved.
For example, write a function to calculate the sum and difference of two integers and return the results through pointers:
1 2 3 4
void calc(int a, int b, int *sum, int *diff) { *sum = a + b; *diff = a  b; }
Calling the function in main:
1 2 3 4 5 6
int main() { int x = 10, y = 5, sum, diff; calc(x, y, &sum, &diff); printf("Sum: %d, Diff: %d\n", sum, diff); return 0; }

Situations that require dynamic memory allocation:
When the required memory size can only be determined at runtime, or when memory needs to be frequently allocated and released, dynamic memory allocation should be used.
For example, reading a file and storing its content in memory. Since the file size is unknown in advance, memory needs to be dynamically allocated based on the actual number of bytes read:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
FILE *fp = fopen("data.txt", "r"); char *buffer = NULL; int size = 0; int capacity = 0; char ch; while ((ch = fgetc(fp)) != EOF) { if (size + 1 > capacity) { capacity = (capacity == 0) ? 1 : capacity * 2; buffer = (char *)realloc(buffer, capacity); } buffer[size++] = ch; } buffer[size] = '\0'; fclose(fp);

The basic idea of the exhaustive method:
The exhaustive method is a fundamental algorithmic approach. Its basic idea is to enumerate all possible cases, check each case one by one to determine if it satisfies the problem requirements, and thus obtain the solution to the problem.
For example, to find all prime numbers between 1 and 100. The exhaustive method can be used to traverse each number from 1 to 100 and determine whether it is a prime number:
1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 2; i <= 100; i++) { int isPrime = 1; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrime = 0; break; } } if (isPrime) { printf("%d ", i); } }

Practical problems suitable for storing data using arrays:
(1) Student grade management system: Arrays can be used to store each student’s scores in various subjects, which is convenient for score input, query, and statistics.
(2) Image processing: An image can be regarded as a twodimensional array, where each element represents the pixel value at the corresponding position. Using arrays facilitates image reading, modification, and display operations.
Application Questions

A threedigit decimal integer is called a “narcissistic number” if the sum of the cubes of its three digits equals the number itself (for example, $153 = 1^3 + 5^3 + 3^3$). Please define a function to determine whether an integer is a narcissistic number, and use this function to print out all narcissistic numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <stdio.h> #include <math.h> int isNarcissistic(int num) { int sum = 0; int temp = num; while (temp > 0) { int digit = temp % 10; sum += pow(digit, 3); temp /= 10; } return (sum == num); } int main() { printf("All narcissistic numbers:\n"); for (int i = 100; i <= 999; i++) { if (isNarcissistic(i)) { printf("%d ", i); } } return 0; }

Given the iterative relation $x_{k+1} = \frac{4}{3}x_{k}  \frac{a}{3x^{2}_{k}}$, where $a \in \mathbb{R}$, and $\{ x_k \}$ converges. Write a program to estimate the limit value of $x_k$.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <stdio.h> #include <math.h> #define EPSILON 1e6 double iterate(double x, double a) { return (4.0 / 3.0) * x  a / (3 * x * x); } int main() { double a; printf("Please enter the value of parameter a: "); scanf("%lf", &a); double x0 = 1.0; // Initial value double x1 = iterate(x0, a); while (fabs(x1  x0) > EPSILON) { x0 = x1; x1 = iterate(x0, a); } printf("The estimated limit value of the iterative sequence is: %.6f\n", x1); return 0; }

There are n students, and the information of each student includes name and score. Use dynamic memory allocation to store the data and complete the following operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME 20 typedef struct { char name[MAX_NAME]; int score; } Student; void inputStudents(Student *students, int n) { for (int i = 0; i < n; i++) { printf("Please enter the name and score of student %d: ", i + 1); scanf("%s %d", students[i].name, &students[i].score); } } void printStudents(Student *students, int n) { printf("Student information:\n"); for (int i = 0; i < n; i++) { printf("Name: %s\tScore: %d\n", students[i].name, students[i].score); } } void sortStudents(Student *students, int n) { for (int i = 0; i < n  1; i++) { for (int j = 0; j < n  i  1; j++) { if (students[j].score < students[j + 1].score) { Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } } int main() { int n; printf("Please enter the number of students: "); scanf("%d", &n); Student *students = (Student *)malloc(n * sizeof(Student)); inputStudents(students, n); printStudents(students, n); sortStudents(students, n); printf("Student information sorted by score in descending order:\n"); printStudents(students, n); free(students); return 0; }
The above program implements the following functions:

Defined a student structure that includes name and score fields.

Created a student structure array of size n using dynamic memory allocation.

Provided functions for inputting student information, printing student information, and sorting student information by score in descending order.

In the main function, first input the number of students, then dynamically allocate memory to create a student array.

Call the corresponding functions to input student information, print student information, sort students by score in descending order, and print the sorted results.

Finally, free the dynamically allocated memory.
