CS100 Polynomial evaluation Problem
Problem 1. Basic knowledge Problem 2. Polynomial evaluation
Input formatOutput formatExampleNotesProblem 3. GCC argument descriptorProblem backgroundProblem 1. Basic knowledge
- Suppose c is a variable of type char. If c is a decimal digit character, how
can we obtain its numeric value?int(c)
c - 48
c - '0'
c - '\0'
*(int *)&c
- Regarding undefined behaviors, which of the following statements is/are
true?An undefined behavior results in one of a set of valid results. For example,the following code (assuming int is 32-bit) will assign the variable x withsome value, but the value is not valid.int ival = 10000000;int x = ival * ival;An undefined behavior means that there are no restrictions on the behaviorof the program. The standarddoes not require such programs to do
anything meaningful.An undefined behavior means that two or more behaviors are permitted by
the standard and that the compiler will choose a meaningful one. Thebehavior remains unchanged when the program is run again, although wedon't know what the ehavior is.Correct C programs shall not have undefined behaviors.
- Suppose int is 32-bit and long long is 64-bit. Select the code snippet(s
The parameter declarations of swap can be rewritten as int *pa, *pb,because pa and pb are of the same type.The function swap will output the values of *pa and *pb.None of the above.3) Write down the type of each expression. For pointer types, place exactly onespace between the pointee type and the first asterisk, and no spaces betweenonsecutive asterisks (e.g. int *, int **, or int ***, but not int* or int * *).Expression pa *pa &pa x &x
Type
- Read the following code.
int min_element(int *array, int l, int r) {
int pos = l;
while (l < r) {
if (array[l] < array[pos])
pos = l;
++l;
}
return pos;
}
int a[] = {1, 4, 2, 8, 5, 7};
(1) Which of the following statements is/are true?
min_element(array, l, r) returns array[i] .
min_element(array, l, r) returns array[i] .
min_element(array, l, r) returns the index of the first occurrence of
array[i] .
min_element(array, l, r) prints the index of the first occurrence of
array[i] to the standard output.
To make min_element(array, l, r) return the index of the last occurrence
of array[i] , replace array[l] < array[pos] (on line 4)
with array[l] <= array[pos].
(2) Suppose we have the following main function.
int main(void) {
printf("%d\n", min_element(a, 0, 6));
}
min{ ∣ i ∈ [l,r)}
min{ ∣ i ∈ [l,r]}
min{ ∣ i ∈ [l,r)}
min{
∣ i ∈ [l,r)}
min{ ∣ i ∈ [l,r)}Which of the following statements is/are true?
The function main contains undefined behavior.
The type of a is int [6].
The type of a is int *.The type of the parameter array is int [6].The type of the parameter array is int [], where the length is determinedat run-time and may change.The type of the parameterarray is int *.
ince we want to find the minimum element in the entire array a, we can
simply write min_element(a), and the parameters l and r will be assignedwith 0 and 6 automatically.
- Given the functions swap and min_element as defined in the previous two
questions. Read the following code.
void sort(int *array, int n) {
for (int i = 0; i < n - 1; ++i) {
int min_pos = min_element(array, i, n);
swap(&array[i], &array[min_pos]);
// Note: If you have difficulty understanding this function, uncomment the
following lines, run the code and look at the output.
// for (int i = 0; i < n; ++i)
// printf("%d ", array[i]);
// printf("\n");
}
}
int main(void) {
int a[] = {8, 5, 7, 1, 4, 2};
sort(a, 6);
for (int i = 0; i < 6; ++i)
printf("%d ", a[i]);
printf("\n");(1) Regarding the sort function, which of the following statements is/are true?
swap(&array[i], &array[min_pos]) is equivalent to swap(array + i,
array + min_pos).
At each iteration, the smallest element in {array[i], ..., array[n - 1]}
is found and then placed at index i by swapping with array[i].
The effect of sort on the array is not changed if we rewrite the loop
condition as i < n.sort(array, n) sorts the elements {array[0], ..., array[n - 1]} in
non-decreasing order.
(2) Which of the following statements is/are true?
The program prints 1 2 4 5 7 8.
If we replace sort(a, 6) as sort(a, 4), the output becomes 1 2 4 5 8 7.
If we replace sort(a, 6) as sort(a, 3), the output becomes 5 7 8 1 4 2.
If we replace sort(a + 2, 4), the output becomes 8 5 1 2 4 7, i.e. only
the last four elements are sorted.Problem 2. Polynomial evaluation Given a polynomial P(x) of degree n
n P(x) = a0 + a1x + ⋯ + anx n = ∑ aix i ,i=0we want to evaluate the polynomial at several points x0, x1, ⋯ , xm−1.Input format On the first line, a nonnegative integer n, which is the degree of the polynomial.On the second line, n + 1 numbers a0 , a1 , ⋯ , an separated by space, which arethe coefficients of the polynomial.
Notes It is guaranteed that n ⩽ 30. An array is enough to store the coefficients. Do notuse heap memory.Your program will not be compiled and linked against the math library, so
do not use the functions in <math.h>. The evaluation of P (xi) at a given point xi should be done using only one loopwithout call to standard library functions. Think about how to do this efficiently.
Problem 3. GCC argument descriptor
Problem background
To compile C/C++ code, we need to execute a command that calls the compilerwith some arguments. For example:gcc hello.c -o hello.exe -std=c17 -Wall -Wpedantic -Wextra
This command compiles the C source code file hello.c with gcc, setting theoutput file to be hello.exe, setting the language standard to be ISO C17, andenabling some kinds of warnings and errors by -Wall -Wpedantic -Wextra.Taking a look at GCC's manual, you will find an overwhelmingly long list ofsupported arguments. Is there a tool that can generate some explanations ofthe arguments, or at least for some of the most common ones? For example,
suppose the name of that tool is gcc_descriptor.exe (without .exe on Mac OSX and Linux). It would be helpful if the following command (with .\ replaced by
./ on Mac OS X and Linux)
.\gcc_descriptor hello.c -o hello.exe -std=c17 -Wall -Wpedantic -Wextra
could print
hello.c: C source code as input file.
-o hello.exe: Place the primary output in file hello.exe.
-std=c17: Set the language standard to ISO C17.
-Wall: Enable all the warnings about constructions that some users consider
questionable, and that are easy to avoid (or modify to prevent the warning).
-Wpedantic: Issue all the warnings demanded by strict ISO C and ISO C++ and reject all programs that use forbidden extensions.
-Wextra: Enable some extra warning flags that are not enabled by -Wall.Your task is to implement such a tool that generates explanations for some verycommon GCC arguments.
Problem description Write a program that accepts command-line arguments, which are thearguments that we want to pass to GCC, and print the descriptions for them.The supported kinds of arguments are as follows.
- -WallDescription:-Wall: Enable all the warnings about constructions that some users considerquestionable, and that are easy to avoid (or modify to prevent the warning).
- -WpedanticDescription:-Wpedantic: Issue all the warnings demanded by strict ISO C and ISO C++ and rejectall programs that use forbidden extensions.
- -WextraDescription:-Wextra: Enable some extra warning flags that are not enabled by -Wall.
- -WerrorDescription:
-Werror: Make all warnings into errors.
- -o xxx
Description:
-o xxx: Place the primary output in file xxx.where xxx is replaced by the actual file name (see the example below). Note that
this option consists of two arguments, where the first one is -o and the second
one is the file name.
- -I xxx
Description:
-I xxx: Add the directory xxx to the list of directories to be searched for header
files during preprocessing.
where xxx is replaced by the actual directory name (see the example below).
Note that this option consists of two arguments, where the first one is -I and
the second one is the directory name.
- -std=xxx
Description:
-std=xxx: Set the language standard to yyy.
where xxx is replaced by the actual value (see the example below) and yyy is
replaced by the name of the standard according to the following table.
xxx
yyy
example example output
cN
ISO CN
c17
ISO C17
c++N
ISO C++N
c++20
ISO C++20
gnuN
GNU dialect of CN
gnu11GNU dialect of C11gnu++N GNU dialect of C++N gnu++14GNU dialect of C++14N consists of two digits. For example, the 2003 ISO C++ standard inameC++03.
- OtherAnything that does not match the patterns above is treated as an input fileDescription:xxx: yyy as input file.
C source code
hello.c
*.h
C/C++ header file stdio.h
*.cpp, *.C, *.cc, *.cxx C++ source code
checker.cc, game.cpp*.hpp, *.hxx
C++ header fileutils.hpp
Notes There is no input for this problem. The arguments to be explained are given asthe command line arguments of your program.It is guaranteed that the arguments are valid: Anything that does not match thepatterns in 1 ~ 7 must be a valid C/C++ source or header file name as describedin the table above. For the language standards (pattern 7), you don't need tocheck whether the standard that it refers to actually exists. Things like -std=c++100000 won't appear.The names of the files and directories involved in the arguments do not containwhitespaces or quotes.The name following -o or -I is not a C/C++ source or header file.Print the explanations in order of the arguments.Please be extremely careful about the output contents, especially the
spaces, newlines and punctuations.Example Suppose the name of your program (executable) is my_program.exe (without.exe on Mac OS X and Linux). If the following command (with .\ replaced by ./on Mac OS X and Linux) is executed,.\my_program -std=c++20 -Wall -Wpedantic -Wextra a.c b.hxx c.cpp -Werror -ooutput_file -I /usr/local/boost_1_80_0/
the output is
-std=c++20: Set the language standard to ISO C++20.-Wall: Enable all the warnings about constructions that some users considerquestionable, and that are easy to avoid (or modify to prevent the warning).-Wpedantic: Issue all the warnings demanded by strict ISO C and ISO C++ and rejectall programs that use forbidden extensions.-Wextra: Enable some extra warning flags that are not enabled by -Wall.
a.c: C source code as input file.
b.hxx: C++ header file as input file.
c.cpp: C++ source code as input file.
-Werror: Make all warnings into errors.
-o output_file: Place the primary output in file output_file.
-I /usr/local/boost_1_80_0/: Add the directory /usr/local/boost_1_80_0/ to the list
of directories to be searched for header files during preprocessing.
Problem 4. 6174 The number 6174 is known as Kaprekar's constant after the Indianmathematician D. R. Kaprekar. This number is renowned for the following rule:
- Take any four-digit number, using at least two different digits (leading zerosare allowed).Arrange the digits in descending and then in ascending order to get twofour-digit numbers, adding leading zeros if necessary.
- Subtract the smaller number from the bigger number.
- Go back to step 2 and repeat.The above process, known as Kaprekar's routine, will always reach its fixed point,
6174, in at most 7 iterations. Once 6174 is 代写CS100 Polynomial evaluation Problem reached, the process will continueyielding 7641 − 1467 = 6174. For example, choose 1459:Write a program that simulates this process. Note thatThe input number should not contain more than 4 digits and shouldcontain at least two different digits (i.e. not a repdigit like , , ...).These numbers are said to be invalid.The input number may contain less than 4 digits. For example, start with :Hint The functions given in [Problem 1] Basic knowledge may be helpful for thisproblem
xi xidigits but is a repdigit, print xxx is a repdigit. where xxx is replaced with xi .A step in the Kaprekar's routine should be printed in the form xxx - yyy = zzz,where xxx, yyy and zzz are replaced with the corresponding numbers. Note thatleading zeros are not printed (see the example below). The process stops whenzzz reaches 6174.
If the input is already 6174, you should print nothing and start processing next
input.
You don't have to start printing after all inputs are consumed! Do not wasteefforts saving the things to be printed.
Example
NoteYour program will not be compiled and linked against the math library, so
do not use the functions in <math.h>. Problem 5: CPU emulator
Description
To understand how the computer executes your program, let me give a short
introduction to CPU. The CPU basically contains 2 parts: registers and ALU. You
can consider registers as an array. Each cell of this array has a name (for
example, x0, x1, x2, …, x31 in RISC-V), and can store a single number. Arithmetic
calculations (addition, subscription, multiplication, division, ...) are done by ALU.
When executing your program, the assembly codes (written in binary) are sent
to CPU. Each line of assembly code specifies some operation of registers, andCPU will use a special register named PC (program counter) to identify thecurrentexecution line number. For example, if the code is add x3 x1 x2, thenCPU will take thevalues in registers x1 and x2, add them up and store the resultin the register x3. This is shown in the figure below (only 5 registers shown here).Then, the CPU increments PC by 1 to execute the next instruction.In this problem, you arerequired to implement a toy emulator which is able toexecute a very simple assembly language (similar to RISC-V). You only need toconsider 6registers, named x0, x1, x2, x3, x4 and x5, all in lowercase, and each
egister stores a 16-bit integer. The x0 register is a special one, whose value is
always zero. Also, there are only has 6 instructions in this assembly language:add, sub, mul, div, let and print, which are introduced below.In this problem, we use a 16-bit machine code. An example of a 16-bitinstruction is as follows.Theinstructions add, sub, mul and div are similar. The syntax is shown in thefollowing table. Note that in these operations, the imm part is discarded.
Instruction Syntax
Explanation
OpCode
addd r1 r2 value in r1 += value in r2 000
subsub r1 r2 value in r1 -= value in r2001mulmul r1 r2 value in r1 *= value in r2010divdiv r1 r2 value in r1 /= value in r2011The let instruction is used to assign value to a register. The syntax is:Instruction Syntax Explanation OpCode letlet r1 imm value in r1 = imm 100where r1 is the name of some register (x0 to x5), and imm is an integer within therange . In this instruction, the r2 part is discardedand the imm part isused. For example, the execution of instruction let x1 80 is shown below.Also, we need a print instruction to print the value in some register so that wecan test whetherthe execution result is correct. For example, if the value in
register x1 is 5, your emulator should print x1 = 5 on the screen when theinstruction print x1 is executed. The r2 part is also discarded in this instruction.
Instruction Syntax
Explanation OpCode printprint r1 print value in r1 101[0, 2 7 − 1]Notes:
- In the div instruction, the division result is truncated towards zero, and thedenominator will always be non-zero.Value in all registers should be initialized to 0 before execution.
- Remember that the value in x0 is always zero. Any instruction that attemptsto modify it should have no effect.
We only consider unsigned immediate in this problem.
Input format
The first line of the input is a number n, indicating the total lines of code.The following n lines are the instructions written in hexadecimal.
Output format Your emulator should execute the input program, and only print the result
when a print instruction is executed. It is guaranteed that the input andoutput are both non-empty.
Explanation:0x8401 is 1000010000000001 in binary, whose opcode is 100, r1 is 001 (x1), r2is 000 (x0) and imm is 0000001 (1). From the opcode we know that this is alet instruction, so the instruction is let x1 1.0xA400 is 1010010000000000 in binary, whoseopcode is 101 and r1 is 001(x1). This is a print instruction, so the instruction is print x1.Note Your program will not be compiled and linked against the math library, so do not use the functions in <math.h>.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix