C Programming Final Assignment
Semester: 2022 – 2023 (1)
CourseName: ProgrammingforEngineers(I)
Class:
Name:
Sussex Candidate No.
ZJGSU Number:
C Programming Final Assignment
Instructions
1) Design and write 6 C programs for the problems, and submit 6 C source code
files with the .c extension. Please do not use the .cpp extension, that is, you are
not allowed to use the C++ syntax.
2) Write a report which includes your commented C code, screenshots of test
results for each problem (run your code and show results). You can prepare
your report in a Word file, convert it into a pdf file, and submit the report pdf file.
3) Reminder again: you should submit 6 commented C source code files and 1
report pdf file on Canvas, i.e., a total of 7 files. You can either submit 7 individual
files, or compress the files in a zip file and submit the compressed file.
4) The submission link is Software Exercise XVAC Week 3
https://canvas.sussex.ac.uk/courses/25884/assignments/100788
5) The due time is 1 Jan 2023 by 17:00 (the UK time)
6) Please also upload your files to OneDrive at the same time if there are problems
with the Canvas server. You can get access to OneDrive through your Sussex
account.
About plagiarism
1) You are not allowed to discuss your coursework with other students.
2) Do not copy the code of other students. Do not let other students copy your code.
3) Do not ask other students for their code.
4) Do not show or send your code to other students. Do not allow other students to
take photos of your code.
5) Protect your personal computer and electronic products. Set passwords
wherever necessary to prevent your documents from being stolen. Report to the
police ASAP if your computers and electronic products get stolen.
Problem 1 (15 marks)
Write a C program to calculate the natural number e with:
lim𝑛→∞
(1 +
1
𝑛
)
𝑛
Print the results with five different values of n which are set according to your
Sussex candidate number. If your Sussex candidate number is 123456, then the
values of n are set as 12, 123, 1234, 12345 and 123456. You can use the loop
structure to solve the problem. You are NOT allowed to use the standard
functions, for instance, the pow function.
Answer:
1.1 Commented C code
1.3 Screenshots of test results
Problem 2 (15 marks)
Write a C program that asks the user to input a M * N matrix. M, N, and the
elements of the matrix are entered from the console. The matrix must be created as
a 2-dimensional integer array. Then complete the following tasks:
1) Create and print a submatrix from element (m1, n1) to element (m2, n2) of the
original matrix, where 1≤m1<m2≤M, and 1≤n1<n2≤N. m1, n1, m2 and n2 are
entered from the console by the user. Given an example 4 * 3 matrix A, then the
submatrix from element (2,1) to element (4,2) is B:
𝐴 = [
12 5 22
38 9 40
14 2 54
11 11 33] 𝐵 = [
38 9
14 2
11 11
]
Note: Please do not forget that the indices of the arrays start from 0. Therefore, the index of
element (2,1) in the matrix is (1,0) in the array.
2) After doing task 1), change the values of the four elements in the lower right
corner of the original M * N matrix according to your Sussex candidate number.
If your Sussex candidate number is 123456, the four elements are changed to 12,
34, 56 and 123456. For the above example, the matrix A is changed to Anew, as
shown below. Then print the new submatrix from element (m1,n1) to element
(m2,n2) again.
𝐴𝑛𝑒𝑤 =
[
12 5 22
38 9 40
14 𝟏𝟐 𝟑𝟒
11 𝟓𝟔 𝟏𝟐𝟑𝟒𝟓𝟔]
Answer:
2.1 Commented C code
2.2 Screenshots of test results
Problem 3 (20 marks)
Write a C program that defines two unsigned integer variables and assigns your
Sussex candidate number (6 digits) and ZJGSU student number (10 digits) to the
two variables, respectively. Then accomplish the following tasks:
1) Extract the digits of the two variables, i.e. your Sussex candidate number and
ZJGSU student number, and stores each digit as an element of an array of size 16.
The digits of Sussex candidate number are put on the left, followed by the digits of
ZJGSU student number on the right. For instance, if your Sussex candidate number
is 123456, and ZJGSU student number is 2212345678, then the result is
{1,2,3,4,5,6,2,2,1,2,3,4,5,6,7,8}.
2) Count and print the occurrence number of each digit in the array.
Answer:
3.1 Commented C code
3.2 Screenshots of test results
Problem 4 (15 marks)
Write three user-defined functions:
1) The first function returns the distance of two points in the 2-dimensional
Cartesian coordinate system.
2) The second function calculates the slope and the third function calculates the
intercept of the straight line that passes through the two points in the 2-
dimensional Cartesian coordinate system.
The coordinates of the two points are given in the main function according to your
Sussex candidate number and they are passed as arguments to the user-defined
functions. If your Sussex candidate number is 123456, then the coordinates of the
two points are (12, 3) and (4,56), respectively.
Call the three functions in the main function and print the results.
Answer:
4.1 Commented C code
4.2 Screenshots of test results
Problem 5 (20 marks)
Write a function that computes the roots of the quadratic equation ax2 + bx + c = 0.
This function has four arguments: a, b, c which are the coefficients of the quadratic
equation, and an array of size 2 that stores the two roots. The function returns an
integer variable flag which is set as the following values, according to the number
and type of roots:
0: any x is a solution (a = b = c = 0);
1: wrong equation (a = b = 0, c 0);
2: one root (a = 0, b 0, so the root is -c/b);
3: two roots (b2 – 4ac 0);
4: no roots (b2 – 4ac < 0);
Call the function in the main function and print the coefficients, flag, and roots with
the coefficients below.
(a) 0, 0, 0
(b) 0, 0, 1
(c) 0, 2, 2
(d) 3, 6, 3
(e) 2, 2, -12
(f) 12, 34, 56 (if your Sussex candidate number is 123456)
Answer:
5.1 Commented C code
5.2 Screenshots of test results
Problem 6 (15 marks)
Please accomplish the following tasks:
Create an integer array with the following initial values in the main function:
a b c 25 44 1 -27 -119 31 -13 0 23 17 101 -2 324 -7 -39
where a, b, and c are from your Sussex candidate numbers, for example, if your
Sussex candidate number is 123456, then a= -12, b= -34, c= -56.
Write a program that
1) finds the numbers whose absolute values are larger than 20 and stores them
into a second array,
2) finds the negative odd numbers and stores them into a third array,
3) finds the numbers that are divisible by 4 and stores them into a fourth array.
4) creates a text file (.txt file) and writes the four arrays into the file, one line per
array. The file name is your Sussex candidate number. Print an error
message if the file cannot be opened successfully.
Answer:
6.1 Commented C code
6.2 Screenshots of test results
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。