Assignment 2
Assignment 2
Instructions:
1. Assignments are to be done individually. You must complete this assignment by yourself.
You cannot work with anyone else in the class or with someone outside of the class. The
code you write must be your own and you must understand each part of your code. You
are encouraged to get help from the instructional staff through google classroom.
2. Do not use any String or math libraries (such as cmath, cstring, string etc) and also do not
use built-in function (such as strlen, strcmp etc). Caution: zero marks will be awarded.
3. Do not edit Function Prototypes. Caution: zero marks will be awarded.
4. The usage of string is strictly prohibited.
5. Your code must be generic.
6. Marks distribution and test Cases are provided for each question. Your code will be
evaluated with similar test cases. If the required output is generated, you will be awarded
full marks. Failing to generate the correct output will result in zero marks. Total Marks:
200.
7. Static and global variables are not allowed.
8. Plagiarism: Plagiarism of any kind (copying from others, copying from the internet, etc)
is not allowed. If found plagiarized, you will be awarded zero marks in the assignment.
Repeating such an act can lead to strict disciplinary actions and failure in the course.
9. Please start early otherwise you will struggle with the assignment.
10. Test cases: Test cases (in gtest) will be shared with you on Google Classroom. We will be
running your code against our test cases, and a test case failure or a segmentation
fault/incorrect result or even syntax error will result in ZERO marks. Question 1 on Gtest
, Question 2 will be checked on demo and Question 3 on codeforces.
11. Submission Guidelines: Dear students, we will be using auto-grading tools (gtest), so
failure to submit according to the below format would result in ZERO marks in the relevant
evaluation instrument.
a. Make separate files for each question in format i24xxxx_Qx.cpp
b. Your files must contain your name, student-id, and assignment # on the top of the
in the comments.
c. Move all the cpp files in one folder. The folder must contain only cpp files (no
binaries, no exe files etc.,). If we unable to download your submission due to any
reason you will be awarded zero mark.
d. Run and test your program on a lab machine before submission. If there is a syntax
error, ZERO marks will be awarded in that specific question.
e. Rename the folder as ROLL-NUM_SECTION (e.g. 24i-0001_A) and compress the
folder as a zip file. (e.g. 24i-0001_A.zip). Only zip file will be acceptable.
f. Submit the .zip file on Google Classroom within the deadline.
g. Submission of Question 3 will be on Codeforces. The link is attached with the
submission on GCR. The submission will also be closed on Codeforces on deadline.
h. Submission other than Google classroom (e.g. email etc.) will not be accepted.
1
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
i. The student is solely responsible to check the final zip files for issues like corrupt
files, viruses in the file, mistakenly exe sent. If we cannot download the file from
Google classroom due to any reason it will lead to zero marks in the assignment.
You are required to use Visual Studio 19 or above for the assignment.
Note: Follow the given instruction to the letter, failing to do so will result in a zero.
Since this is your own String class, you should know how things work in this and create all the
functionalities for the correct implementation of many features in it. Following functionalities will be
required for your class.
class String{
// think about the private data members...
public:
// provide definitions of following functions...
String(); // a default constructor
String(const char*);
String(const char*);
String(const String&);
String add(const String&); // appends a String to the end
String add(const char*); // appends a String to the end
String add(char*); // appends a String to the end
String add(const int&); // if the stored String is an integer e.g. “123” it adds the given
integer into it and returns the answer. If not then returns “NaN”
String remove(const String&); //removes a substring anywhere in the String
String remove(const char*); //removes a substring anywhere in the String
String remove(char*); //removes a substring anywhere in the String
String remove(const int&); // if the stored String is an integer e.g. “123” it subtracts the
given integer into it and returns the answer. If not then returns “NaN”
bool equal(const String&)const;
bool equal(const char *)const;
bool equal(char *)const;
bool operator!();
void addnew(const String&); //appends a new string into the same string
void removesub(const String&); // removes the substring from the same string
String slice(const String&); // Performs slicing according to explanation below
String slice(const char *); // Performs slicing according to explanation below
int len();// returns length
String lower();
String upper();
String title();
String swapcase();
bool startsWith();
2
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
bool endsWith();
String replace(String&, String&);
String replace(const char *, const char *);
String join(String&);
String join(const char *);
String join(char *);
String* split(String&, int &size);
String* split(const char *, int &size);
String* split(char *, int &size);
~String(); // destructor...
}
Python is a strong language to handle string function, there are multiple functionalities of string function
given below you must implement them in your string class
3
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
In this question, you are required to implement a small pagination problem with this concept.
Instead of making a single long character array to store long sentences. We will divide our sentences into
lines. We will make sure that a line does not exceed our screen. So each line will have a maximum limit of
characters. For example, consider my line can store a maximum of 20 characters. Then No more than 20
characters can occur in a line. But we also need to make sure that a word does not break while changing
lines.
Now Microsoft Word provides the feature of adding separate columns in a Page. Each page contains 2
columns in it. The columns look like this:
Lorem ipsum dolor sit amet, consectetur in voluptate velit esse cillum dolore eu fugiat
adipiscing elit, sed do eiusmod tempor nulla pariatur. Excepteur sint occaecat cupidatat
incididunt ut labore et dolore magna aliqua. Ut non proident, sunt in culpa qui officia deserunt
enim ad minim veniam, quis nostrud exercitation mollit anim id est laborum.
ullamco laboris nisi ut aliquip ex ea commodo Lorem ipsum dolor sit amet, consectetur
consequat. Duis aute irure dolor in reprehenderit adipiscing elit, sed do eiusmod tempor
4
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
incididunt ut labore et dolore magna aliqua. Ut in voluptate velit esse cillum dolore eu fugiat
enim ad minim veniam, quis nostrud exercitation nulla pariatur. Excepteur sint occaecat cupidatat
ullamco laboris nisi ut aliquip ex ea commodo non proident, sunt in culpa qui officia deserunt
consequat. Duis aute irure dolor in reprehenderit mollit anim id est laborum.
in voluptate velit esse cillum dolore eu fugiat Lorem ipsum dolor sit amet, consectetur
nulla pariatur. Excepteur sint occaecat cupidatat adipiscing elit, sed do eiusmod tempor
non proident, sunt in culpa qui officia deserunt incididunt ut labore et dolore magna aliqua. Ut
mollit anim id est laborum. enim ad minim veniam, quis nostrud exercitation
Lorem ipsum dolor sit amet, consectetur ullamco laboris nisi ut aliquip ex ea commodo
adipiscing elit, sed do eiusmod tempor consequat. Duis aute irure dolor in reprehenderit
incididunt ut labore et dolore magna aliqua. Ut in voluptate velit esse cillum dolore eu fugiat
enim ad minim veniam, quis nostrud exercitation nulla pariatur. Excepteur sint occaecat cupidatat
ullamco laboris nisi ut aliquip ex ea commodo non proident, sunt in culpa qui officia deserunt
consequat. Duis aute irure dolor in reprehenderit mollit anim id est laborum.
Here, we first fill the left column. It has a limit of number of line. After that, we fill the right column
which has a limit of number of lines as well.
So our structure goes like this:
You need to implement this Editor class which contains this hierarchy. Your classes should work on the
following code:
int main(){
int n = 10; // number of lines in column
int m = 100; //number of characters in each line
Editor editor(n,m);
cin >> editor;
/* user enters:
“This is my test sentence. You should start doing the assignment as soon as possible to avoid any
problems in the end. The assignment is based on many applications of programming and will be very
helpful if you do it yourself.”;
*/
// This will be added as a new paragraph.
editor += “Plagiarism policy is very strict for this assignment and will be checked thoroughly.
Use of AI like chatgpt and deepseek is the easiest way of getting caught.”;
Page p(n,m);
cin >> p;
/* user enters:
“Now i want to add this in a new page. You can add as much as it fits in two columns. The rest
will be discarded.”;
*/
p+= “add this in new paragraph”;
cout << p[0] << endl;// prints first column of page.
Column c(n,m);
cin >> c;
5
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
/* user enters:
“Now add this as a new column. I can only add this to a page if it has the capacity of a column
left in it.” */
p += c;
editor += p;
cout << c[1] << endl;// prints second line of column.
Column c2(c);
c2 += “Now add more data into this column”;
editor += c2;
editor(“My Own Text Editor”);// gives Title to editor
editor(3);// restricts editor to only 3 pages
cout << editor[0] << endl;//print first page
cout << editor << endl;
}
6
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
7
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
Part B:
In the kingdom of Numerica, King Tony, known for his love of numbers, presented a challenge to his
subjects. The challenge was simple yet intriguing:
Given the number N, the King declared, "I want to know if a sequence of the first N consecutive numbers,
starting from 1, whose sum is exactly N, exists.
For example, if N is 6, the sum of the first 3 numbers (1 + 2 + 3) equals 6, so the answer is YES.
But what about other numbers? Can you determine whether such a sequence exists for any N?"
The task was clear: find out if there is a sequence of consecutive numbers starting from 1 that sums up to
N. If such a sequence exists, the answer is YES;
if not, it's NO. The King awaited the solution, knowing that those who succeeded would gain his favor and
admiration.
Will you take on the King's puzzle and find the answer?
Input Format
The first line contains the number of test cases - t (1 ≤ t ≤ 100) .
Each test case contains a number n (1 ≤ n ≤ 10^18).
Constraints
t (1 ≤ t ≤ 100) .
n (1 ≤ n ≤ 10^18).
Output Format
output "yes" if exists else "no" case sensitive without quotes
Test 2:
1+2+3+4 > 7
8
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
Part C:
In the heart of the Mughal Empire, Emperor Akbar's court was abuzz with rumors of a secret treasure buried
centuries ago by his ancestor, Babur. The treasure's location was encrypted in a mystical Cipher Grid, a gift
from Persian mathematicians. Only the most intelligent minds in the empire could decipher it.
The responsibility to unlock the secret fell to Mehrunissa, a brilliant scholar, and her mentor, Hakim Ahsan.
The grid, etched on an ancient silk scroll, was filled with numbers arranged in a mysterious pattern.
Mehrunissa decided to start at the top-left corner and traverse the grid diagonally to extract the encoded
sequence.
As Mehrunissa progressed through the grid, the numbers revealed themselves in a precise order, forming a
code. With Hakim Ahsan’s guidance, she then combined the numbers into a single value. To unlock the
treasure chest, they needed to calculate the modulus of this number with a specific divisor qq, chosen by
the Emperor himself.
“Mehrunissa, you’ve solved the puzzle of the emperors!” Hakim Ahsan exclaimed. The result was the key
that would guide them to Babur's long-lost treasure.
Task Details:
To simulate Mehrunissa’s challenge, you will:
1. Traverse the grid diagonally to extract numbers.
2. Combine the numbers into a single value.
3. Calculate the modulus of this value with q, as specified by Emperor Akbar.
Input Explanation:
1. The first line contains n, representing the size of the n×n Cipher Grid.
2. The second line contains the divisor q, chosen by the Emperor.
3. The next n lines represent the grid values.
4. If the grid is not n×n, print Impossible.
Example Input and Output:
Input:
3
396
123
456
789
Output:
124753689
225
Input:
4
177777
3792
1648
5203
8974
Output:
3715692428908374
156093
9
NATIONAL UNIVERSITY OF COMPUTER & EMERGING
SCIENCES ISLAMABAD CAMPUS
Instructions:
Traverse the Cipher Grid diagonally, combine the numbers into a sequence, and calculate the modulus
using q. This final number is the key to unlocking Babur’s legendary treasure!
Happy Coding 😊
10