You are on page 1of 4

Data Structure and Analysis (BSCS – V4, V5, V8) Fall 2022

Assignment # 02 Part-ii (70 Marks)


Algebraic Expression Conversion and Evaluation, and OS Task Scheduling

Deadline: Dec 18 (11:30 pm)

Instructions
Your assignment should represent your own effort. However, you are not expected to work alone. It is
fine to discuss the exercises and try to find solutions together, but each student shall write down and
submit his/her solutions separately. It is a good academic standard to acknowledge collaborators, so if
you worked together with other students, please list their names.
For a programming task, your solution must contain (i) an explanation of your solution to the problem,
(ii) the C++ code, in a form that we can run it, (iii) instructions on how to run it.
For all programming tasks, it is not allowed to use any external libraries if not stated otherwise.
Please, include your name, student ID, and email address in your submission.

Plagiarism will lead to -100% marks (any type of code sharing is not allowed).

Submissions after Dec 18 (11:30 pm) will get zero. Any discussion/excuses regarding deadlines
will lead to -50% marks.

Stacks (30 Marks)


Write a C++ code using the stacks to convert the infix expression to a postfix expression.
Infix: A * B + C * D
Postfix: A B * C D * +
Moreover, your program should be able to evaluate the postfix expressions.
Example:
Infix: A * B + C * D
Postfix: A B * C D * +
Get values for A, B, C, and D (from the user) and used these values for the evaluation.
Example Values: A = 2, B = 3, C = 5, and D = 4
Postfix expression for evaluation: 2 3 * 5 4 * +
Output: 26
Your program must be able to detect whether the input given by the user is correct or not. If the user
inserted the correct infix form, your program will convert that infix expression into a postfix
expression and then the expression will be evaluated. You can ask the user to input values for the
variable used in the expression.
When the execution is completed, the program should be able to print the stack table used for
evaluation. (Stack table examples are also given in lecture slides)

Queue: Implement the following functionalities (40 Marks)


Q no 1
Suppose that you are working as an operating system engineer at Microsoft. Microsoft decided to
launch a new version of an operating system for machines that have only a single-core SMT-disabled
processor. The machine supports multitasking (multiple jobs running at the same time (using some
scheduling)). The company decided to implement Round Robin scheduling for multitasking.
Round Robin: Each task/job will be executed for only n-seconds (where n=2, you can change the
time). Job Details: ID and Execution-Time

Your program will take all the jobs as input and store them in a Queue (insertion at the end/tail).
The program will get the first job (extract from the start/head), and send it to the CPU. CPU executes it
for 2 seconds and then returns the job to the controller function (main function). The execution time of
the job is reduced by 2. For example, if the job execution time is 7 before execution, then the job
execution time will be updated to 5 after execution (as the CPU executes the job for 2 seconds).
When the job completed its execution time (2s), the controller function checks the updated execution
time. If the execution time is greater than 0, it will be inserted at the end of the queue. Otherwise, no
need to insert it into the queue (it is complete now).

Example:

Operations Queue Comments

Time before start 3 (A) 8 (B) 1 (C) The queue is filled in the start with job ids
(A, B, and C) and execution time (3, 8, and
1)

Job-A sent to the 8 (B) 1 (C) Job A is in execution


CPU

Job-A returns from 8 (B) 1 (C) 1 (A) The job-A execution time is updated to 1.
the CPU The controller function checks the time. It is
greater than 0, so the controller function
added it again into the queue.

Job-B sent to the 1 (C) 1 (A) Job B is in execution


CPU

Job-B returns from 1 (C) 1 (A) 6 (B) The job-A execution time is updated to 6.
the CPU The controller function checks the time. It is
greater than 0, so the controller function
added it again into the queue.

Job-C sent to the 1 (A) 6 (B) Job C is in execution


CPU

Job-C returns from 1 (A) 6 (B) The job-C execution time is updated to 0 (or
the CPU - 1). The controller function checks the time.
It is not greater than 0, so the controller
function will not add it to the queue. You
can make an extra queue that stores the
completed jobs for tracking purposes.

Job-A sent to the 6 (B) Job A is in execution


CPU

Job-A returns from 6 (B) The job-A execution time is updated to 0 (or
the CPU - 1). The controller function checks the time.
It is not greater than 0, so the controller
function will not add it to the queue. You
can make an extra queue that stores the
completed jobs for tracking purposes.

Job-B sent to the It will be executed till completion as no job


CPU is in the queue.

Q no 2
Now change your implementation for Priority Queue. The queue contains the processes/jobs/tasks that
have ID and priority.
Job Details: ID and Priority
Your program will take all the jobs as input and store them in a Queue.
The implementation of the priority queue will be changed as you are not inserting the tasks at the end.
The tasks will be inserted according to their priorities. For example, we have a queue that has three
tasks with priorities equal to 1,1,5. The new task has been entered with priority 3. The queue will be
1,1,3,5 now. 3 will be inserted between 1 and 5.
All the tasks that have the same priorities will be scheduled according to the First-Come-First-Serve
base technique. So, for this, you can add one more parameter “arrival time” as job details.
When any task is sent to the CPU, it will be executed completely. It will never go back to the queue. It
means that your program is just doing the scheduling while adding the tasks to a queue. If the order of
the insertion of the tasks is correct, you will be able to execute the tasks in the correct way.
Q no 3
Think about the problem with Q no 2. If any task with high priority required too much time, the
priority queue allows it for running till its completion. The other tasks have to wait for a long time.
Any solution?
Think before reading the solution below.
The solution to avoid long waiting:
Combine the above two strategies (round-robin, priority).
Job Details: ID, Priority, Execution-Time, Arrival-Time
Hybrid Strategy:
The tasks will be inserted according to their priorities (priority case).
The execution will be changed now. Every task will be executed on time according to its priority. The
highest priority task will get CPU for more time as compared to the task that has low priority. For
example, if 3 tasks are in the queue. The execution times of these tasks are 15, 7, and 4 and the
priorities are 4, 3, 1. The task with priority 4 will get more time as compared to the task that has
priority equal to 3. You can use some formula to assign the CPU on the basis of priority.
AIM: Make sure that the highest-priority tasks have more CPU time than low-priority tasks. The
wait for low-priority tasks should be as minimum as possible.
The tasks that have high priorities will be partly/completely executed first and then the low priorities
tasks will be executed.
Note: The system you developed must be able to print the queue status with details (ID, Time) after
every time the controller gets the control from the CPU (after each execution, the print function will
be called that prints the updated execution time, and other details).

Submission Instruction:
Your submission should contain separate files (.cpp) for all parts. All the functionalities of stacks
should be added to their corresponding file (applies same on Queue as well). Do not make separate
files for different functionalities of any part. The folder should be in the following format (Roll-No,
First-Name, for example, 201111_XXXX, do not insert special characters with roll number or name).
The file name of part-a should be 201111_XXXX_a.cpp.
Combine the solutions of all parts (part-i, and part-ii) into a single folder named Assignment_02.
Assignment_02 folder will contain 2 sub-folders, one for each part. The first folder will contain the
solutions of part-i (stacks implementation, 3 files), while the second folder will contain the solutions of
algebraic expression (1 file) and Queue parts (3 files). Naming conventions should be followed for
each folder.
Please follow the instruction strictly to avoid any inconvenience
Best of Luck and Happy Coding 😊

You might also like