首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
program编程讲解、辅导c++程序、C++程序辅导 辅导Database|辅导R语言程序
项目预算:
开发周期:
发布时间:
要求地区:
Important message on plagiarism
The single most important point for you to realize before the beginning of your studies
at ShanghaiTech is the meaning of “plagiarism”:
Plagiarism is the practice of taking someone else's work or ideas and passing them off
as one's own. It is the misrepresentation of the work of another as your own. It is
academic theft; a serious infraction of a University honor code, and the latter is your
responsibility to uphold. Instances of plagiarism or any other cheating will be reported
to the university leadership, and will have serious consequences. Avoiding any form of
plagiarism is in your own interest. If you plagiarize and it is unveiled at a later stage
only, it will not only reflect badly on the university, but also on your image/career
opportunities.
Plagiarism is academic misconduct, and we take it very serious at ShanghaiTech. In the
past we have had lots of problems related to plagiarism especially with newly arriving
students, so it is important to get this right upfront:
You may…
• … discuss with your peers about course material.
• … discuss generally about the programming language, some features, or abstract lines
of code. As long as it is not directly related to any homework, but formulated in a
general, abstract way, such discussion is acceptable.
• … share test cases with each other.
• … help each other with setting up the development environment etc.
You may not …
• … read, possess, copy or submit the solution code of anyone else (including people
outside this course or university)!
• … receive direct help from someone else (i.e. a direct communication of some lines
of code, no matter if it is visual, verbal, or written)!
• … give direct help to someone else. Helping one of your peers by letting him read
your code or communicating even just part of the solution in written or in verbal form
will have equal consequences.
• … gain access to another one’s account, no matter if with or without permission.
• … give your account access to another student. It is your responsibility to keep your
account safe, always log out, and choose a safe password. Do not just share access to
your computer with other students without prior lock-‐out and disabling of automatic
login functionality. Do not just leave your computer on without a lock even if it is just
for the sake of a 5-‐minute break.
• … work in teams. You may meet to discuss generally about the material, but any work
on the homework is to be done individually and in privacy. Remember, you may not
allow anyone to even just read your source code.
With the Internet, "paste", and "share" are easy operations. Don’t think that it is easy to
hide and that we will not find you, we have just as easy to use, fully automatic and
intelligent tools that will identify any potential cases of plagiarism. And do not think
that being the original author will make any difference. Sharing an original solution
with others is just as unethical as using someone else’s work.
CS100 Homework 7 (Spring, 2021)
Submission deadline:
2021-06-09 23:59
Problem 1. Warm-up: Rational Operations and CMake
In this warm-up problem, you will implement a class for rational numbers in separated header and
source files, and compile a multiple-file project using CMake. CMake is the only new knowledge
required for this problem beyond the scope of last homework (homework 6).
All rational numbers can be written as quotients p/q, where p is an integer and q a positive
integer. The result of addition, subtraction, multiplication, and division of two rational numbers
will still be a rational number.1 You need to implement these operations for the Rational class, as
well as a few other functions, as shown in this template:
class Rational
{
public:
// Constructors
Rational();
Rational(int value);
Rational(int p, unsigned int q);
// Assignment operator
Rational& operator=(const Rational& that);
// Arithmetic operators
Rational operator+(Rational that) const;
Rational& operator+=(Rational that);
Rational operator-(Rational that) const;
Rational& operator-=(Rational that);
Rational operator*(Rational that) const;
Rational& operator*=(Rational that);
Rational operator/(Rational that) const;
Rational& operator/=(Rational that);
// Comparison operators: equal and less than
bool operator==(Rational that) const;
bool operator<(Rational that) const;
// Output
friend std::ostream& operator<<(std::ostream& os, const Rational& number);
private:
int m_numerator;
unsigned int m_denominator;
};
- There are 3 constructors for this class. They respectively create a rational number of 0, value,
and p/q.
If q is 0 in the third constructor, you should print a message
"ERROR: initializing with a denominator of 0!" with a new line, and set this
rational number to 0 instead.
The rational number you created should be simplified. For example, the line
Rational r(-6, 10); should instead initialize the number to -3/5.
1 In sense of mathematics, a set with such properties (the set of all rational numbers) is called a field.
- The assignment operator is self-explanatory.
- The arithmetic operators are also self-explanatory. The only notes are:
When a division by 0 happens, you should print a message "ERROR: dividing by 0!"
with a new line, and you can set the result to any value. In fact, such case would not
appear in either Autolab tests or usage for problem 2. If in problem 2 you see your error
message, your calculations are probably wrong.
Any result of these operators should be simplified. For example, the expression
Rational(1, 3) + Rational(1, 6) should evaluate to 1/2.
Pay attention to the difference of operator+ and operator+=. Hint: the operator+=
modifies the number itself and returns a reference.
- At least the two comparison operators == and < are needed for next problem.
- The operator<< of std::ostream should print the number into os in the form of p/q, p for
numerator and q for denominator.
If the number is an integer, you should print as an integer instead, i.e. omit the “/q” part.
Instruction on multiple files and CMake:
- Your “rational.hpp” file should only contain the definition of Rational class and its functions.
- Your “rational.cpp” file should contain implementation of functions in your Rational class.
- You should write a “CMakeLists.txt”, in which you should:
Set the minimum version limit to "VERSION 2.8".
Create a project named “rational”.
Generate a build system for your computer such that it compiles “rational.cpp” along
with a file “main.cpp” (you can create this file when debugging, but don’t submit it—we
will provide a “main.cpp” when grading.), and creates an executable named “rational”.
It’s okay if CMake behaves differently on your computer (for example, creates a
Visual Studio solution). On the Autolab server (Linux), running cmake will generate
a “Makefile”. Then running make will build the executable “rational”.
- Your “rational.hpp” should have a proper include guard. That is, if your code is compiled
with a “main.cpp” of the following content, it must compile:
#include "rational.hpp"
#include "rational.hpp"
int main(){ return 0; }
Submission:
Please submit to Autolab a tarball (*.tar) file that contains three files: “rational.hpp”,
“rational.cpp”, and “CMakeLists.txt”. The name of this tarball file could be arbitrary.
If you have trouble making a tarball, you can download any archiver software.
(Recommended: Bandizip)
Problem 2. Templated Gauss-Jordan Elimination
In this problem, you will write a class template of Matrix to create matrices for both double and
Rational, and perform a Gauss-Jordan Elimination (also known as Gaussian Elimination).
Gauss-Jordan Elimination is a method to solve systems of linear equations by transforming it into
row echelon form with row operations. This method can also be used to compute the rank of a
matrix, the determinant of a square matrix, and the inverse of an invertible matrix.2 We will
provide a detailed pseudocode for the algorithm so that you could implement even if you are
unfamiliar with linear algebra.
For now, let’s look at the structure of the templated class Matrix:
template
class Matrix
{
public:
Matrix();
Matrix(unsigned int rows, unsigned int cols, const T& value = T());
T& operator()(unsigned int r, unsigned int c);
void Print();
void GaussJordan();
private:
// Your implementation here.
};
- Your Matrix should store its data in a vector of vectors.
- The second constructor creates a matrix with given rows and cols, with every entry
initialized to the given value. Note that a default argument of T(), the empty constructor, is
assigned to value, so that you may omit it when declaring a Matrix with default entries.
- The operator() is used for assigning to a particular entry in the matrix.
- The Print() method prints the matrix to std::cout, in the simple format below:
For each entry, print it to std::cout, followed by a space " ".
After each line, print a newline. It’s allowed that each line ends with a trailing space,
and a new line is after the last row.
No setw() or setprecision() is needed.
- Finally, the method GaussJordan(). This method does the Gauss-Jordan Elimination in-place
(altering contents of the matrix), so that the matrix is in row echelon form (not reduced row
echelon form). As the resulted matrix is not unique, you should follow the given pseudocode
to implement the algorithm:
2 Gaussian elimination - Wikipedia
h := 1 /* Initialization of the pivot row */
k := 1 /* Initialization of the pivot column */
while h ≤ m and k ≤ n
/* Find the k-th pivot */
/* record the max and min of k-th column after h-th row */
i_max := argmax (i = h ... m, A[i, k])
i_min := argmin (i = h ... m, A[i, k])
if A[i_max, k] = 0
/* max = 0, swap max with min */
i_max := i_min
if A[i_max, k] = 0
/* max = min = 0, no pivot in this column, pass to next */
k := k+1
else
swap rows(h, i_max)
/* Do for all rows below pivot: */
for i = h + 1 ... m:
f := A[i, k] / A[h, k]
/* Fill with zeros the lower part of pivot column: */
A[i, k] := 0
/* Do for all remaining elements in current row: */
for j = k + 1 ... n:
A[i, j] := A[i, j] - A[h, j] * f
/* Increase pivot row and column */
h := h + 1
k := k + 1
Note that the highlighted parts have been modified: the original algorithm chooses the
row with greatest absolute value to swap as pivot, but the Rational class from problem 1
cannot use the same method as double to calculate absolute values, so we need to
instead check both max and min, prioritizing max.
Testing:
We have provided two simple test functions on your matrix in the given file “tests.cpp”. The
expected output of your program is included in comments.
Instruction on multiple files and CMake:
- As declaration and implementation of a templated class are tricky to separate into header files
and source files (because member functions of different types are generated and compiled
given the template types), you should write all your code in a single file named “matrix.hpp”.
- You should write a “CMakeLists.txt”, in which you should:
Set the minimum version limit to "VERSION 2.8".
Create a project named “matrix”.
Generate a build system for your computer such that it compiles “rational.cpp” along
with a file “main.cpp” (you can create this file when debugging, but don’t submit it—we
will provide a “main.cpp” when grading.), and creates an executable named “matrix”.
You do not need to compile “matrix.hpp” along, as it’s a header file and it will be
included in “main.cpp”.
It’s okay if CMake behaves differently on your computer (for example, creates a
Visual Studio solution). On the Autolab server (Linux), running cmake will generate
a “Makefile”. Then running make will build the executable “matrix”.
- Your “matrix.hpp” should have a proper include guard. That is, if your code is compiled
with a “main.cpp” of the following content, it must compile:
#include "matrix.hpp"
#include "matrix.hpp"
int main(){ return 0; }
- Your “matrix.hpp” should not #include "rational.hpp".
Submission:
Please submit to Autolab a tarball (*.tar) file that contains four files: “matrix.hpp”, “rational.hpp”,
“rational.cpp”, and “CMakeLists.txt”. The name of this tarball file could be arbitrary.
Problem 3. Parallel K-Means Clustering
The K-Means Clustering Algorithm is one of the most straight-forward clustering algorithms in
data science. It aims at solving Vector Quantization problems.
We will be dealing with K-Means on a 2-Dimensional data set. As shown in the left-hand side of
the following figure, given N points (coordinates) and a number K, you need to classify these
points into K clusters. For example, the right-hand side of following figure shows the K-Means
clustering result of an example data set, with number of data sample points N=300,000 and
desired number of clusters K=15 (each color indicates a cluster).
This is the procedure of K-Means algorithm:
1. Each center point c represents the center (i.e. mean) of a cluster.
2. For every sample point p and every center point 𝑐𝑐𝑖𝑖, calculate the distance between p and 𝑐𝑐𝑖𝑖: d(
p, 𝑐𝑐𝑖𝑖).
3. Assign every sample point p to the cluster x, where d(p, 𝑐𝑐x) is the smallest among all d(p, 𝑐𝑐𝑖𝑖)s.
4. Update the center point 𝑐𝑐𝑖𝑖 of each cluster i to be the current mean of all points 𝑝𝑝𝑖𝑖 within the cl
uster.
5. Goto 2. and repeat, unless no updates happened in this iteration (i.e. converges).
This is the pseudo-code for K-Means algorithm, while it may be hard to understand:
In this homework, we provide you a framework for this algorithm. In this framework, each data
point is an instance of struct Point. Some functions of struct Point is provided for you. You
only need to implement these 3 functions:
- double Point::Distance(Point& other)
Calculate the Euclidian distance between this point and “other”
- std::ostream& operator<<(std::ostream& os, Point& pt)
Output the content of Point& pt to the os, the format is: first, output pt.x, then output a
space (“ ”), then output pt.y. Notice you don’t need to start a new line (i.e., no “\n” will
be printed). For example, you should output the Point(3, 4.5) by “3 4.5”.
- std::istream& operator>>(std::istream& is, Point& pt)
Scan “x” and “y” of Point& pt from the std::istream& is, the input is two doubles
separated by a single space (“ ”).
For the K-Means class, you need to implement the constructor and the function Kmeans::Run .
- Kmeans::Kmeans(const std::vector
&, const std::vector
&
initialCenters)
Constructor of K-Means class, points is a vector contains coordinates of data sample
points, initialCenters contains coordinates of initial centers.
- std::vector
Kmeans::Run(int maxIterations)
Run the K-Means algorithm. Returns the index (color) of all data points. For example, if
you have 1,000 data points, and you want to classify them into 5 clusters. Then, the
return value of this function is a vector with size=1000, indicates which cluster of each
point belongs to. The value of returned vector is in {0, 1, 2, 3, 4}. (e.g., result[9]=3
means points[9] was divided to cluster 3).
The clustering loop runs at most maxIterations times. If it does not converge in
maxIterations times, just break from the loop and return the current result.
IMPORTANT: YOU MUST USE MULTI-THREADING TO SPEED IT UP.
As discussed in class, we can use std::thread to speed up programs by multithreading.
Notice that the distance calculation of points and centers is individual, so it’s
easy to calculate them in parallel. We suggest you to use 4 threads on Autolab, using
too many threads will make it even slower.
There are 6 files provided for you. You need to modify code in “kmeans.cpp” and “kmeans.hpp”.
DO NOT modify any code in “main.cpp”, or your code may not accept by OJ. You can use
“CMakeLists.txt” for building this project, “generate.py” to generate samples for testing and
debugging, and use “visualize.py” to visualize your clustering result.
Hope you have already installed Python3 on your own computer. For this homework, you may
need to install dependencies by the following command:
python -m pip install numpy sklearn matplotlib
After that, you can test your program by these 3 commands (suppose your executable file is
“./kmeans”):
python generate.py in.txt
./kmeans out.txt < in.txt
python visualize.py out.txt
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!