SlideShare uma empresa Scribd logo
1 de 12
COMP 2103X1 Assignment 6
Due Thursday, March 2 by 7:00 PM
General information about assignments (important!):
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/General-
info.html
Information on passing in assignments:
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/Pass-in-
info.html
Information on coding style:
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/C-
coding-style-notes
[1] Searching for strings is an extremely common operation in a
lot of programs. For this problem
you will write a program which accepts two strings as
command-line arguments and output some
information as described below.
Consider the following text (written by a famous
mathematician/logician)
There was one who was famed for the number of things
He forgot when he entered the ship:
His umbrella, his watch, all his jewels and rings,
And the clothes he had bought for the trip.
(I’ve written this on four lines, but it is really one string with a
“n” character following each of
the lines.) Now suppose our search string is “forgotten”. In this
case, the word we want is not in
the text, but various prefixes of the string are. Your program
should find the rightmost occurrence
of the search string, if it exists, and otherwise of all instances
of the longest prefix of the search
string in the text, it should find the rightmost one.
In either case, your program then outputs two numbers, as
shown below in the examples. The first
number is the number of characters of the search string which
match, and the second number is
how many characters from the right end of the text to go
backwards (to find the match).
For example, the prefix “forgot” (of “forgotten”) is found 128
characters back from the end of the
string, and the length of this longest matched prefix is 6. Thus
your program will output “6;128”.
Here are some examples of how your program should operate.
You must include these samples,
as well as some of your own creation, when you create your
script file.
$ a6p1 ’twas brillig’ ’was not’
4;11
$ a6p1 ’aaron aardvark’ ’aaabbb’
2;8
$ a6p1 ’aaron aardvark’ ’qwerty’
0;q
$ a6p1 hicdefghi hi
2;2
$ a6p1 ’A two-line
text string’ inky
2;3
1
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/General-
info.html
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/Pass-in-
info.html
http://cs.acadiau.ca/~jdiamond/comp2103/assignments/C-
coding-style-notes
You will notice that if there is no (non-empty) prefix of the
search string in the text that the program
outputs 0 for the matched length (as expected) but instead of
outputting the distance from the end,
it outputs the first character of the search string. Your program
must do this as well.
What other unusual cases should your program deal with? If
there are any that you can think of,
the comment at the top of your program should describe these
cases and what your program does
with them. Your program should never “blow up”.
Finally, notice that by surrounding the text and search string
with single quotes, the spaces be-
come part of their respective command-line argument, rather
than acting as argument separators.
Similarly, an embedded newline character becomes part of the
argument when enclosed in quotes,
rather than completing the command.
[2] Write a program which does the following things.
— Declares a 4�4 matrix of ints.
— Reads in such a matrix (from stdin).
— Prints out the matrix in a rectangular array (see examples and
further directions below).
— Prints out “It is upper triangular.” if the matrix is all zeroes
below the main diagonal.
— Prints out “It is lower triangular.” if the matrix is all zeroes
above the main diagonal.
— Prints out “It is not triangular” if it is not all zeroes either
above or below the main diagonal.
— Prints out a blank line and then the transpose of the matrix.
— Prints out a blank line and then “It is symmetric.” if the
transpose of the matrix is equal to the
matrix, otherwise “It is not symmetric.”
You should test your program on enough matrices to show that
your program detects all the different
possibilities. Naturally, you should use good programming
practices when writing your program.
You might want to store test data in files, rather than repeatedly
typing matrix data in. If so, in your
script file you should display the contents of your test files with
cat.
If your program is reading from the keyboard, you must print
out a prompt, asking the user for
the data. If the program is reading from a re-directed file (e.g.,
a6p2 < test1) or from a pipe
(e.g., cat test1 | a6p2 ) then DO NOT print out a prompt. You
can use the isatty() library
function (see man 3 isatty) to determine whether you are
reading from the keyboard or not.
Note that isatty() takes fd (“file descriptor”) as its parameter.
You can get the correct fd by
using the return result of fileno(stdin).�
Here is an example of what running this program might look
like.
% a6p2
Please enter 16 integers:
1 0 3 4 0 4 5 6 0 0 9 0 0 0 0 8
The matrix is:
1 0 3 4
0 4 5 6
� Note that some historically significant early computer
terminals were made by Teletype Corpora-
tion, and consequently “tty” became a generic short form for
“computer terminal”. Thus “isatty”
means “is a tty”, i.e., “is a terminal”.
2
0 0 9 0
0 0 0 8
It is upper triangular.
Its transpose is:
1 0 0 0
0 4 0 0
3 5 9 0
4 6 0 8
It is not symmetric.
%
% a6p2 < test2
The matrix is:
1 233 6666 -4
0 4 5 236
0 0 9 0
77 0 0 8
It is not triangular.
Its transpose is:
1 0 0 77
233 4 0 0
6666 5 9 0
-4 236 0 8
It is not symmetric.
For this assignment, you should ensure that you were able to
correctly read 16 integers, but you
do not need to do sophisticated error handling; if stdin does not
have 16 consecutive integers for
you, write out an error message and terminate the program.
You should write a function which takes two 4 � 4 matrices and
stores the transpose of the first
matrix into the second matrix.
Your code to perform the tests on the matrix should be in one or
more functions, not in the main
program. Similarly, good design practices should lead you to
put the code to read the matrix in its
own function.
When printing the matrix, make the columns as narrow as
possible, as shown above. That is, find
the number with the largest printed width, and print all of the
numbers in fields that wide. (And
separate columns with one space.)
3
[3] When you study algorithm analysis, you will find (or
attempt to find) mathematical functions which
describe how long a given algorithm will take to work on a
problem of a given size. Sometimes this
is easy, sometimes it is more difficult, and sometimes it may be
extremely difficult or impossible.
Sorting is a very important and well-studied problem in
computer science. Consider the following
algorithm, which sorts an array of n numbers, using the
following idea. First, find the smallest
number in the array and save it as the first sorted number. Then,
find the smallest number in the
rest of the array and save that as the smallest number. Continue
doing this until there are only two
numbers left; when you have found the smallest of those two
numbers, the remaining number is
the largest.
This idea can be formalized as follows:
SelectionSort(array[1..n])
for i = 1 to n-1
minpos = i
for j = i+1 to n
if array[j] < array[minpos]
minpos = j
if minpos != i
swap(array[minpos], array[i])
In sorting algorithms we often count the number of comparisons
of data items and use that as the
measure of how long an algorithm takes. In this case, we can
see that the inner for loop does n�1
comparisons the first time, n � 2 comparisons the second time,
and so on down to 1 comparison
the last time. Thus we can say that the amount of time used by
selection sort to sort n numbers is
T .n/ D
n�1X
iD1
i for n > 0
which is a sum we can solve with elementary mathematics,
giving us T .n/ D n.n � 1/=2.
I told you that story so I can tell you this story.
Some algorithms operate by breaking down a big problem into
two or more smaller problems,
of more or less equal size. (This breaking down may or may not
require some work to be done,
depending on the situation.) Then the sub-problems are solved,
which takes some time, and the
solutions are combined to give us a solution to the original
problem, which may also take some
time. Such techniques fall into a class of algorithms known as
divide and conquer.
The equations describing divide and conquer algorithms (and
some other classes) are much more
complex than the one given above. For example, the time
required by one well-known algorithm
can be described as follows
T .n/ D
�
0 if n < 2
T .dn=2e/ C T .bn=2c/ C n � 1 otherwise
.�/
In other words, we break the problem into two sub-problems
whose sizes are as equal as possible,
we solve those problems, and we use another n � 1 operations
in the “break apart” and/or “put
together” phases.
4
These equations can be difficult to solve sometimes. Since this
is COMP 2103, instead of solving
equation .�/, write a program which
(i) prompts the user for two numbers,
(ii) inputs two numbers, and
(iii) outputs a table of n versus T .n/ for all numbers between
the two input numbers.
For example, here is a possible terminal session, user input in
red:
$ a6p3
Enter two numbers: 2 4
n | T(n)
------+--------
2 | 1
3 | 3
4 | 5
Your table doesn’t have to look like mine, but try to make it
something you’d be proud of, not
something you’d try to hide. Also, keep in mind that T .n/ will
get large sooner or later, so try to
keep that in mind while designing your table layout.
To solve this problem, create a recursive function which, when
given a non-negative integer n,
returns T .n/. Then call this function as needed from the place in
your program where you are
printing out the table.
When creating your script file run your program a few times
with different inputs to test boundary
conditions, invalid inputs, and so on. And, of course, at least
one case where everything is correct.
Did you use functions in any of these questions? Should you
have? Did you document them
correctly?
Does you program “blow up” on unexpected input, or does it
deal with bad input in a “graceful”
way?
How does your program deal with boundary conditions, if there
are any?
Did you remember to put all required comments in? Does your
program call out for any other
comments in the body of the code?
5
COMP 2103X1 Assignment 6Due Thursday, March 2 by 700 PM.docx

Mais conteúdo relacionado

Semelhante a COMP 2103X1 Assignment 6Due Thursday, March 2 by 700 PM.docx

Semelhante a COMP 2103X1 Assignment 6Due Thursday, March 2 by 700 PM.docx (20)

CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Pascal programming lecture notes
Pascal programming lecture notesPascal programming lecture notes
Pascal programming lecture notes
 
Analyzing algorithms
Analyzing algorithmsAnalyzing algorithms
Analyzing algorithms
 
data.txtInternational Business Management l2 Cons.docx
data.txtInternational Business Management       l2        Cons.docxdata.txtInternational Business Management       l2        Cons.docx
data.txtInternational Business Management l2 Cons.docx
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++11 and 64-bit Issues
C++11 and 64-bit IssuesC++11 and 64-bit Issues
C++11 and 64-bit Issues
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Seminar on MATLAB
Seminar on MATLABSeminar on MATLAB
Seminar on MATLAB
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
 
Mixed Effects Models - Descriptive Statistics
Mixed Effects Models - Descriptive StatisticsMixed Effects Models - Descriptive Statistics
Mixed Effects Models - Descriptive Statistics
 
Project
ProjectProject
Project
 
Oct.22nd.Presentation.Final
Oct.22nd.Presentation.FinalOct.22nd.Presentation.Final
Oct.22nd.Presentation.Final
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Programming Hp33s talk v3
Programming Hp33s talk v3Programming Hp33s talk v3
Programming Hp33s talk v3
 

Mais de donnajames55

KATIES POST The crisis case I chose to discuss this week is th.docx
KATIES POST The crisis case I chose to discuss this week is th.docxKATIES POST The crisis case I chose to discuss this week is th.docx
KATIES POST The crisis case I chose to discuss this week is th.docxdonnajames55
 
Kate Chopins concise The Story of an Hour.  What does Joseph.docx
Kate Chopins concise The Story of an Hour.  What does Joseph.docxKate Chopins concise The Story of an Hour.  What does Joseph.docx
Kate Chopins concise The Story of an Hour.  What does Joseph.docxdonnajames55
 
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docx
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docxKadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docx
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docxdonnajames55
 
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docx
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docxK-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docx
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docxdonnajames55
 
JWI 505 Business Communications and Executive Presence Lect.docx
JWI 505 Business Communications and Executive Presence Lect.docxJWI 505 Business Communications and Executive Presence Lect.docx
JWI 505 Business Communications and Executive Presence Lect.docxdonnajames55
 
Just Walk on By by Brent Staples My firs.docx
Just Walk on By by Brent Staples               My firs.docxJust Walk on By by Brent Staples               My firs.docx
Just Walk on By by Brent Staples My firs.docxdonnajames55
 
Just make it simple. and not have to be good, its the first draft. .docx
Just make it simple. and not have to be good, its the first draft. .docxJust make it simple. and not have to be good, its the first draft. .docx
Just make it simple. and not have to be good, its the first draft. .docxdonnajames55
 
JUST 497 Senior Seminar and Internship ExperienceInternationa.docx
JUST 497 Senior Seminar and Internship ExperienceInternationa.docxJUST 497 Senior Seminar and Internship ExperienceInternationa.docx
JUST 497 Senior Seminar and Internship ExperienceInternationa.docxdonnajames55
 
July 2002, Vol 92, No. 7 American Journal of Public Health E.docx
July 2002, Vol 92, No. 7  American Journal of Public Health E.docxJuly 2002, Vol 92, No. 7  American Journal of Public Health E.docx
July 2002, Vol 92, No. 7 American Journal of Public Health E.docxdonnajames55
 
Journals are to be 2 pages long with an introduction, discussion and.docx
Journals are to be 2 pages long with an introduction, discussion and.docxJournals are to be 2 pages long with an introduction, discussion and.docx
Journals are to be 2 pages long with an introduction, discussion and.docxdonnajames55
 
Judgement in Managerial Decision MakingBased on examples fro.docx
Judgement in Managerial Decision MakingBased on examples fro.docxJudgement in Managerial Decision MakingBased on examples fro.docx
Judgement in Managerial Decision MakingBased on examples fro.docxdonnajames55
 
Joyce is a 34-year-old woman who has been married 10 years. She .docx
Joyce is a 34-year-old woman who has been married 10 years. She .docxJoyce is a 34-year-old woman who has been married 10 years. She .docx
Joyce is a 34-year-old woman who has been married 10 years. She .docxdonnajames55
 
Journal Write in 300-500 words about the following topic.After .docx
Journal Write in 300-500 words about the following topic.After .docxJournal Write in 300-500 words about the following topic.After .docx
Journal Write in 300-500 words about the following topic.After .docxdonnajames55
 
Journal Supervision and Management StyleWhen it comes to superv.docx
Journal Supervision and Management StyleWhen it comes to superv.docxJournal Supervision and Management StyleWhen it comes to superv.docx
Journal Supervision and Management StyleWhen it comes to superv.docxdonnajames55
 
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55 Ava.docx
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55  Ava.docxJournal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55  Ava.docx
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55 Ava.docxdonnajames55
 
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docx
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docxJournal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docx
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docxdonnajames55
 
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docx
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docxJournal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docx
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docxdonnajames55
 
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docx
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docxJournal of Personality 862, April 2018VC 2016 Wiley Perio.docx
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docxdonnajames55
 
Journal of Personality and Social Psychology1977, Vol. 35, N.docx
Journal of Personality and Social Psychology1977, Vol. 35, N.docxJournal of Personality and Social Psychology1977, Vol. 35, N.docx
Journal of Personality and Social Psychology1977, Vol. 35, N.docxdonnajames55
 
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docx
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docxJournal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docx
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docxdonnajames55
 

Mais de donnajames55 (20)

KATIES POST The crisis case I chose to discuss this week is th.docx
KATIES POST The crisis case I chose to discuss this week is th.docxKATIES POST The crisis case I chose to discuss this week is th.docx
KATIES POST The crisis case I chose to discuss this week is th.docx
 
Kate Chopins concise The Story of an Hour.  What does Joseph.docx
Kate Chopins concise The Story of an Hour.  What does Joseph.docxKate Chopins concise The Story of an Hour.  What does Joseph.docx
Kate Chopins concise The Story of an Hour.  What does Joseph.docx
 
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docx
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docxKadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docx
Kadyr AkovaCosc 1437D. KirkEnemy.javaimport java.util..docx
 
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docx
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docxK-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docx
K-2nd Grade3rd-5th Grade6th-8th GradeMajor Concepts,.docx
 
JWI 505 Business Communications and Executive Presence Lect.docx
JWI 505 Business Communications and Executive Presence Lect.docxJWI 505 Business Communications and Executive Presence Lect.docx
JWI 505 Business Communications and Executive Presence Lect.docx
 
Just Walk on By by Brent Staples My firs.docx
Just Walk on By by Brent Staples               My firs.docxJust Walk on By by Brent Staples               My firs.docx
Just Walk on By by Brent Staples My firs.docx
 
Just make it simple. and not have to be good, its the first draft. .docx
Just make it simple. and not have to be good, its the first draft. .docxJust make it simple. and not have to be good, its the first draft. .docx
Just make it simple. and not have to be good, its the first draft. .docx
 
JUST 497 Senior Seminar and Internship ExperienceInternationa.docx
JUST 497 Senior Seminar and Internship ExperienceInternationa.docxJUST 497 Senior Seminar and Internship ExperienceInternationa.docx
JUST 497 Senior Seminar and Internship ExperienceInternationa.docx
 
July 2002, Vol 92, No. 7 American Journal of Public Health E.docx
July 2002, Vol 92, No. 7  American Journal of Public Health E.docxJuly 2002, Vol 92, No. 7  American Journal of Public Health E.docx
July 2002, Vol 92, No. 7 American Journal of Public Health E.docx
 
Journals are to be 2 pages long with an introduction, discussion and.docx
Journals are to be 2 pages long with an introduction, discussion and.docxJournals are to be 2 pages long with an introduction, discussion and.docx
Journals are to be 2 pages long with an introduction, discussion and.docx
 
Judgement in Managerial Decision MakingBased on examples fro.docx
Judgement in Managerial Decision MakingBased on examples fro.docxJudgement in Managerial Decision MakingBased on examples fro.docx
Judgement in Managerial Decision MakingBased on examples fro.docx
 
Joyce is a 34-year-old woman who has been married 10 years. She .docx
Joyce is a 34-year-old woman who has been married 10 years. She .docxJoyce is a 34-year-old woman who has been married 10 years. She .docx
Joyce is a 34-year-old woman who has been married 10 years. She .docx
 
Journal Write in 300-500 words about the following topic.After .docx
Journal Write in 300-500 words about the following topic.After .docxJournal Write in 300-500 words about the following topic.After .docx
Journal Write in 300-500 words about the following topic.After .docx
 
Journal Supervision and Management StyleWhen it comes to superv.docx
Journal Supervision and Management StyleWhen it comes to superv.docxJournal Supervision and Management StyleWhen it comes to superv.docx
Journal Supervision and Management StyleWhen it comes to superv.docx
 
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55 Ava.docx
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55  Ava.docxJournal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55  Ava.docx
Journal of Soc. & Psy. Sci. 2018 Volume 11 (1) 51-55 Ava.docx
 
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docx
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docxJournal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docx
Journal of Social Work Values & Ethics, Fall 2018, Vol. 15, No.docx
 
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docx
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docxJournal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docx
Journal of Policy Practice, 9220–239, 2010 Copyright © Taylor &.docx
 
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docx
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docxJournal of Personality 862, April 2018VC 2016 Wiley Perio.docx
Journal of Personality 862, April 2018VC 2016 Wiley Perio.docx
 
Journal of Personality and Social Psychology1977, Vol. 35, N.docx
Journal of Personality and Social Psychology1977, Vol. 35, N.docxJournal of Personality and Social Psychology1977, Vol. 35, N.docx
Journal of Personality and Social Psychology1977, Vol. 35, N.docx
 
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docx
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docxJournal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docx
Journal of Pcnonaluy and Social Psychology1»M. Vd 47, No 6. .docx
 

Último

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Último (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

COMP 2103X1 Assignment 6Due Thursday, March 2 by 700 PM.docx

  • 1. COMP 2103X1 Assignment 6 Due Thursday, March 2 by 7:00 PM General information about assignments (important!): http://cs.acadiau.ca/~jdiamond/comp2103/assignments/General- info.html Information on passing in assignments: http://cs.acadiau.ca/~jdiamond/comp2103/assignments/Pass-in- info.html Information on coding style: http://cs.acadiau.ca/~jdiamond/comp2103/assignments/C- coding-style-notes [1] Searching for strings is an extremely common operation in a lot of programs. For this problem you will write a program which accepts two strings as command-line arguments and output some information as described below. Consider the following text (written by a famous mathematician/logician) There was one who was famed for the number of things He forgot when he entered the ship: His umbrella, his watch, all his jewels and rings, And the clothes he had bought for the trip. (I’ve written this on four lines, but it is really one string with a “n” character following each of the lines.) Now suppose our search string is “forgotten”. In this case, the word we want is not in
  • 2. the text, but various prefixes of the string are. Your program should find the rightmost occurrence of the search string, if it exists, and otherwise of all instances of the longest prefix of the search string in the text, it should find the rightmost one. In either case, your program then outputs two numbers, as shown below in the examples. The first number is the number of characters of the search string which match, and the second number is how many characters from the right end of the text to go backwards (to find the match). For example, the prefix “forgot” (of “forgotten”) is found 128 characters back from the end of the string, and the length of this longest matched prefix is 6. Thus your program will output “6;128”. Here are some examples of how your program should operate. You must include these samples, as well as some of your own creation, when you create your script file. $ a6p1 ’twas brillig’ ’was not’ 4;11 $ a6p1 ’aaron aardvark’ ’aaabbb’ 2;8 $ a6p1 ’aaron aardvark’ ’qwerty’ 0;q $ a6p1 hicdefghi hi 2;2
  • 3. $ a6p1 ’A two-line text string’ inky 2;3 1 http://cs.acadiau.ca/~jdiamond/comp2103/assignments/General- info.html http://cs.acadiau.ca/~jdiamond/comp2103/assignments/Pass-in- info.html http://cs.acadiau.ca/~jdiamond/comp2103/assignments/C- coding-style-notes You will notice that if there is no (non-empty) prefix of the search string in the text that the program outputs 0 for the matched length (as expected) but instead of outputting the distance from the end, it outputs the first character of the search string. Your program must do this as well. What other unusual cases should your program deal with? If there are any that you can think of, the comment at the top of your program should describe these cases and what your program does with them. Your program should never “blow up”. Finally, notice that by surrounding the text and search string with single quotes, the spaces be- come part of their respective command-line argument, rather than acting as argument separators. Similarly, an embedded newline character becomes part of the argument when enclosed in quotes, rather than completing the command. [2] Write a program which does the following things.
  • 4. — Declares a 4�4 matrix of ints. — Reads in such a matrix (from stdin). — Prints out the matrix in a rectangular array (see examples and further directions below). — Prints out “It is upper triangular.” if the matrix is all zeroes below the main diagonal. — Prints out “It is lower triangular.” if the matrix is all zeroes above the main diagonal. — Prints out “It is not triangular” if it is not all zeroes either above or below the main diagonal. — Prints out a blank line and then the transpose of the matrix. — Prints out a blank line and then “It is symmetric.” if the transpose of the matrix is equal to the matrix, otherwise “It is not symmetric.” You should test your program on enough matrices to show that your program detects all the different possibilities. Naturally, you should use good programming practices when writing your program. You might want to store test data in files, rather than repeatedly typing matrix data in. If so, in your script file you should display the contents of your test files with cat. If your program is reading from the keyboard, you must print out a prompt, asking the user for the data. If the program is reading from a re-directed file (e.g., a6p2 < test1) or from a pipe (e.g., cat test1 | a6p2 ) then DO NOT print out a prompt. You can use the isatty() library function (see man 3 isatty) to determine whether you are reading from the keyboard or not. Note that isatty() takes fd (“file descriptor”) as its parameter. You can get the correct fd by using the return result of fileno(stdin).�
  • 5. Here is an example of what running this program might look like. % a6p2 Please enter 16 integers: 1 0 3 4 0 4 5 6 0 0 9 0 0 0 0 8 The matrix is: 1 0 3 4 0 4 5 6 � Note that some historically significant early computer terminals were made by Teletype Corpora- tion, and consequently “tty” became a generic short form for “computer terminal”. Thus “isatty” means “is a tty”, i.e., “is a terminal”. 2 0 0 9 0 0 0 0 8 It is upper triangular. Its transpose is: 1 0 0 0 0 4 0 0
  • 6. 3 5 9 0 4 6 0 8 It is not symmetric. % % a6p2 < test2 The matrix is: 1 233 6666 -4 0 4 5 236 0 0 9 0 77 0 0 8 It is not triangular. Its transpose is: 1 0 0 77 233 4 0 0 6666 5 9 0 -4 236 0 8 It is not symmetric. For this assignment, you should ensure that you were able to
  • 7. correctly read 16 integers, but you do not need to do sophisticated error handling; if stdin does not have 16 consecutive integers for you, write out an error message and terminate the program. You should write a function which takes two 4 � 4 matrices and stores the transpose of the first matrix into the second matrix. Your code to perform the tests on the matrix should be in one or more functions, not in the main program. Similarly, good design practices should lead you to put the code to read the matrix in its own function. When printing the matrix, make the columns as narrow as possible, as shown above. That is, find the number with the largest printed width, and print all of the numbers in fields that wide. (And separate columns with one space.) 3 [3] When you study algorithm analysis, you will find (or attempt to find) mathematical functions which describe how long a given algorithm will take to work on a problem of a given size. Sometimes this is easy, sometimes it is more difficult, and sometimes it may be extremely difficult or impossible. Sorting is a very important and well-studied problem in computer science. Consider the following algorithm, which sorts an array of n numbers, using the following idea. First, find the smallest
  • 8. number in the array and save it as the first sorted number. Then, find the smallest number in the rest of the array and save that as the smallest number. Continue doing this until there are only two numbers left; when you have found the smallest of those two numbers, the remaining number is the largest. This idea can be formalized as follows: SelectionSort(array[1..n]) for i = 1 to n-1 minpos = i for j = i+1 to n if array[j] < array[minpos] minpos = j if minpos != i swap(array[minpos], array[i]) In sorting algorithms we often count the number of comparisons of data items and use that as the measure of how long an algorithm takes. In this case, we can see that the inner for loop does n�1 comparisons the first time, n � 2 comparisons the second time, and so on down to 1 comparison the last time. Thus we can say that the amount of time used by selection sort to sort n numbers is T .n/ D
  • 9. n�1X iD1 i for n > 0 which is a sum we can solve with elementary mathematics, giving us T .n/ D n.n � 1/=2. I told you that story so I can tell you this story. Some algorithms operate by breaking down a big problem into two or more smaller problems, of more or less equal size. (This breaking down may or may not require some work to be done, depending on the situation.) Then the sub-problems are solved, which takes some time, and the solutions are combined to give us a solution to the original problem, which may also take some time. Such techniques fall into a class of algorithms known as divide and conquer. The equations describing divide and conquer algorithms (and some other classes) are much more complex than the one given above. For example, the time required by one well-known algorithm can be described as follows T .n/ D � 0 if n < 2 T .dn=2e/ C T .bn=2c/ C n � 1 otherwise .�/
  • 10. In other words, we break the problem into two sub-problems whose sizes are as equal as possible, we solve those problems, and we use another n � 1 operations in the “break apart” and/or “put together” phases. 4 These equations can be difficult to solve sometimes. Since this is COMP 2103, instead of solving equation .�/, write a program which (i) prompts the user for two numbers, (ii) inputs two numbers, and (iii) outputs a table of n versus T .n/ for all numbers between the two input numbers. For example, here is a possible terminal session, user input in red: $ a6p3 Enter two numbers: 2 4 n | T(n) ------+-------- 2 | 1 3 | 3 4 | 5 Your table doesn’t have to look like mine, but try to make it something you’d be proud of, not
  • 11. something you’d try to hide. Also, keep in mind that T .n/ will get large sooner or later, so try to keep that in mind while designing your table layout. To solve this problem, create a recursive function which, when given a non-negative integer n, returns T .n/. Then call this function as needed from the place in your program where you are printing out the table. When creating your script file run your program a few times with different inputs to test boundary conditions, invalid inputs, and so on. And, of course, at least one case where everything is correct. Did you use functions in any of these questions? Should you have? Did you document them correctly? Does you program “blow up” on unexpected input, or does it deal with bad input in a “graceful” way? How does your program deal with boundary conditions, if there are any? Did you remember to put all required comments in? Does your program call out for any other comments in the body of the code? 5