Sie sind auf Seite 1von 5

CS217 Object Oriented Programming

Assignment No. 1
January 27, 2020

Deadline: February 7, 2020 at 5:00 PM

Attention
• Make sure that you read and understand each and every instruction. If you have any questions or
comments you are encouraged to discuss with your colleagues and instructors on Piazza (https://
piazza.com/nu.edu.pk/spring2020/cs217/home).

• Create each problem solution in a separate .cpp file, i.e. you must name the file containing solution of
Q1 as ’q1.cpp’, Q2 as ’q2.cpp’ and Q3 as ’q3.cpp’.
– Combine all your work in one .zip file.
– Name the .zip file as ROLL-NUM SECTION.zip (e.g. 19i-0001 B.zip).
– Submit the .zip file on Google Classroom within the deadline.
• For all the problems, use ” char * ”, instead of string. The usage of string is strictly prohibited.
• Start early otherwise you will struggle with the assignment.
• You must follow the submission instructions to the letter, as failing to do so will get you
a zero in the assignment.
• All the submitted evaluation instruments (quizzes, assignments, lab work, exams, and the
project) will be checked for plagiarism. If found plagiarized, both the involved parties
will be awarded zero marks in the relevant evaluation instrument, all of the instruments,
or even an F grade in the course.

Q1: String Manipulation


Write the implementation of the following functions:
1 i n t S t r l e n ( char ∗ s1 )
2 /∗ Returns t h e l e n g t h o f t h e s t r i n g i n number o f c h a r a c t e r s . ∗/
3 {
4 }

1 c h a r ∗ S t r c p y ( c h a r ∗ s1 , c o n s t c h a r ∗ s 2 )
2 /∗ C o p i e s s t r i n g s 2 i n t o a r r a y s 1 . The v a l u e o f s 1 i s r e t u r n e d . ∗/
3 {
4 }

1 c h a r ∗ Strncpy ( c h a r ∗ s1 , c o n s t c h a r ∗ s2 , i n t n )
2 /∗ C o p i e s a t most n c h a r a c t e r s o f s t r i n g s 2 i n t o a r r a y s 1 .
3 The v a l u e o f s 1 i s r e t u r n e d . ∗/
4 {
5 }
1 c h a r ∗ StrCat ( c h a r ∗ s1 , c o n s t c h a r ∗ s 2 )
2 /∗ Appends s t r i n g s 2 t o a r r a y s 1 .
3 The f i r s t c h a r a c t e r o f s 2 o v e r w r i t e s t h e t e r m i n a t i n g n u l l c h a r a c t e r o f s 1 .
4 The v a l u e o f s 1 i s r e t u r n e d . ∗/
5 {
6 }

1 c h a r ∗ StrnCat ( c h a r ∗ s1 , c o n s t c h a r ∗ s2 , i n t n )
2 /∗ Appends a t most n c h a r a c t e r s o f s t r i n g s 2 t o a r r a y s 1 .
3 The f i r s t c h a r a c t e r o f s 2 o v e r w r i t e s t h e t e r m i n a t i n g n u l l c h a r a c t e r o f s 1 .
4 The v a l u e o f s 1 i s r e t u r n e d . ∗/
5 {
6 }

1 i n t StrCmp ( c o n s t c h a r ∗ s1 , c o n s t c h a r ∗ s 2 )
2 /∗ Compares t h e s t r i n g s 1 with t h e s t r i n g s 2 .
3 The f u n c t i o n r e t u r n s 0 , l e s s than 0 o r g r e a t e r than 0 i f s 1 i s e q u a l to ,
4 l e s s than o r g r e a t e r than s2 , r e s p e c t i v e l y . ∗/
5 {
6 }

1 i n t StrnCmp ( c o n s t c h a r ∗ s1 , c o n s t c h a r ∗ s2 , i n t n )
2 /∗ Compares up t o n c h a r a c t e r s o f t h e s t r i n g s 1 with t h e s t r i n g s 2 .
3 The f u n c t i o n r e t u r n s 0 , l e s s than 0 o r g r e a t e r than 0 i f s 1 i s e q u a l to ,
4 l e s s than o r g r e a t e r than s2 , r e s p e c t i v e l y . ∗/
5 {
6 }

1 c h a r ∗∗ StrTok ( c h a r ∗ s1 , c o n s t c h a r s 2 )
2 /∗A c a l l t o StrTok b r e a k s s t r i n g s 1 i n t o ' ' t o k e n s ' '
3 ( l o g i c a l p i e c e s such a s words i n a l i n e o f t e x t ) s e p a r a t e d by c h a r a c t e r
4 c o n t a i n e d i n c h a r s 2 ∗/
5 {
6 }

1 i n t S t r F i n d ( c h a r ∗ s1 , c h a r ∗ s 2 )
2 /∗ S e a r c h e s t h e s t r i n g s 1 f o r t h e f i r s t o c c u r r e n c e o f t h e s t r i n g s 2
3 and r e t u r n s i t s s t a r t i n g index , i f s 2 not found r e t u r n s −1. ∗/
4 {
5 }

1 c h a r ∗ SubStr ( c h a r ∗ , i n t pos , i n t l e n )
2 /∗ This f u n c t i o n r e t u r n s a newly c o n s t r u c t e d s t r i n g with i t s v a l u e i n i t i a l i z e d
3 t o a copy o f a s u b s t r i n g o f t h i s v a r i a b l e .
4 The s u b s t r i n g i s t h e p o r t i o n o f t h e s t r i n g t h a t s t a r t s a t c h a r a c t e r p o s i t i o n
5 ' ' pos ' ' and s p a n s ' ' l e n ' ' c h a r a c t e r s
6 ( o r u n t i l t h e end o f t h e s t r i n g , w h i c h e v e r comes f i r s t ) . ∗/
7 {
8 }
Q2: Text Analysis
The availability of computers with string-manipulation capabilities has resulted in some rather interesting
approaches to analyzing the writings of great authors. This exercise examines three methods for analyzing
texts with a computer. You have to use char * for the following exercises.
1. Write a function that receives a string consisting of several lines of text and returns an array indicating
the number of occurrences of each letter of the alphabet in the text. For example, the phrase “To be,
or not to be: that is the question”: contains one “a,” two “b’s,” no “c’s,” and so on.
1 v o i d c o u n t L e t t e r s ( c h a r ∗ s t r i n g , i n t ∗&array , i n t & s i z e )
2 /∗ Parameters :
3 Input :
4 char ∗ : a m u l t i l i n e s t r i n g
5 Output :
6 i n t ∗ : an a r r a y c o n t a i n i n g c o u n t s o f each l e t t e r ,
7 t o be a l l o c a t e d i n f u n c t i o n
8 i n t : a r r a y s i z e ∗/
9 {
10 }

2. Write a function that receives a string consisting of several lines of text and returns an array indicating
the number of one-letter words, two-letter words, three-letter words, and so on, appearing in the text.
For example, the phrase “Whether this nobler in the mind to suffer” contains 2, 3, 4, etc. length words.
1 v o i d countWordsBasedOnLength ( c h a r ∗ s t r i n g , i n t ∗&a r r a y /∗ t o be a l l o c a t e d ∗/ ,
2 i n t & s i z e /∗ updated a r r a y s i z e ∗/ )
3 /∗ Parameters :
4 Input :
5 c h a r ∗ : a multi −l i n e s t r i n g
6 Output :
7 i n t ∗ : an a r r a y c o n t a i n i n g c o u n t s o f each d i f f e r e n t l e n g t h words ,
8 t o be a l l o c a t e d i n f u n c t i o n
9 i n t : a r r a y s i z e ∗/
10 {
11 }

3. Write a function that receives a string consisting of several lines of text and returns arrays indicating
unique words and the number of occurrences of each unique word in the text along with their size.
1 v o i d countingUniqueWords ( c h a r ∗ s t r i n g , c h a r ∗∗&uwords /∗ l i s t o f unique words ; ∗/ ,
2 i n t ∗&a r r a y /∗ t o be a l l o c a t e d ∗/ , i n t & s i z e /∗ updated a r r a y s i z e ∗/ )
3 /∗ Parameters :
4 Input :
5 char ∗ : a m u l t i l i n e s t r i n g
6 Output :
7 c h a r ∗ ∗ : an a r r a y o f unique words
8 i n t ∗ : t h e i r counts
9 i n t : number o f unique words ∗/
10 {
11 }
Q3: Matrix Operations
A matrix is a collection of values in the form of rows and columns. In this question, you are required to
implement the following matrix functions using C++. You can only use int** data type and DMA to
create a matrix

1. Matrix Multiplication
1 i n t ∗∗ MatrixMul ( i n t ∗∗ MatrixA , i n t rowsA , i n t colsA ,
2 i n t ∗∗ MatrixB , i n t rowsB , i n t c o l s B ) {
3 /∗ MatrixMul implements MatrixA x MatrixB
4 Both t h e m a t r i c e s a r e s t o r e d u s i n g DMA.
5 Number o f rows a r e columns o f both t h e m a t r i c e s
6 a r e a v a i l a b l e i n rowsA , colsA , rowsB , and c o l s B v a r i a b l e s .
7 ∗/
8 }

2. Matrix Addition
1 i n t ∗∗ MatrixAdd ( i n t ∗∗ MatrixA , i n t rowsA , i n t colsA ,
2 i n t ∗∗ MatrixB , i n t rowsB , i n t c o l s B ) {
3 /∗ MatrixAdd implements MatrixA + MatrixB
4 Both t h e m a t r i c e s a r e s t o r e d u s i n g DMA.
5 Number o f rows a r e columns o f both t h e m a t r i c e s
6 a r e a v a i l a b l e i n rowsA , colsA , rowsB , and c o l s B v a r i a b l e s .
7 ∗/
8 }

3. Matrix Subtraction
1 i n t ∗∗ MatrixSub ( i n t ∗∗ MatrixA , i n t rowsA , i n t colsA ,
2 i n t ∗∗ MatrixB , i n t rowsB , i n t c o l s B ) {
3 /∗ MatrixSub implements MatrixA − MatrixB
4 Both t h e m a t r i c e s a r e s t o r e d u s i n g DMA.
5 Number o f rows a r e columns o f both t h e m a t r i c e s
6 a r e a v a i l a b l e i n rowsA , colsA , rowsB , and c o l s B v a r i a b l e s .
7 ∗/
8 }

4. Matrix Transpose
1 i n t ∗∗ MatrixTranspose ( f l o a t ∗∗ Matrix , i n t rows , i n t c o l s ) {
2 /∗ MatrixTranspose implements t r a n s p o s e o f a Matrix
3 The matrix i s s t o r e d u s i n g DMA.
4 Number o f rows a r e columns o f t h e matrix
5 a r e a v a i l a b l e i n rows and c o l s .
6 ∗/
7 }

5. Matrix Rotate
1 i n t ∗∗ MatrixRotate ( i n t ∗∗ Matrix , i n t rows , i n t c o l s ) {
2 /∗ MatrixRotate r o t a t e s a matrix 90 d e g r e e c l o c k w i s e .
3 The matrix i s s t o r e d u s i n g DMA.
4 Number o f rows a r e columns o f t h e matrix
Figure 1: Matrix rotation 90 degree clockwise

5 a r e a v a i l a b l e i n rows and c o l s .
6 ∗/
7 }

6. Matrix Determinant
See: https://people.richland.edu/james/lecture/m116/matrices/determinant.html
See: https://www.math10.com/en/algebra/matrices/determinant.html
1 i n t MatrixDet ( i n t ∗∗ Matrix , i n t rows , i n t c o l s ) {
2 /∗ MatrixDet implements d e t e r m i n a n t o f a s q u a r e Matrix
3 The matrix i s s t o r e d u s i n g DMA.
4 Number o f rows a r e columns o f t h e matrix
5 a r e a v a i l a b l e i n rows and c o l s .
6 ∗/
7 }

7. Matrix Inverse
See: https://people.richland.edu/james/lecture/m116/matrices/inverses.html
1 f l o a t ∗∗ M a t r i x I n v e r s e ( i n t ∗∗ Matrix , i n t rows , i n t c o l s ) {
2 /∗ M a t r i x I n v e r s e implements i n v e r s e o f a Matrix
3 The matrix i s s t o r e d u s i n g DMA.
4 Number o f rows a r e columns o f t h e matrix
5 a r e a v a i l a b l e i n rows and c o l s .
6 ∗/
7 }

Das könnte Ihnen auch gefallen