CST – Homework 4: Stacks and Queues Solution

$30.00 $24.00

Write two programs and submit them on iLearn If you use repl.it, the files will be named main.cpp; you will need to rename them The names are in the assignment; incorrect names will be graded as 0 Part 1. Binary Converter (5 pts) Write a program that prompts the user to enter an integer number,…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)
  • Write two programs and submit them on iLearn

  • If you use repl.it, the files will be named main.cpp; you will need to rename them

  • The names are in the assignment; incorrect names will be graded as 0

Part 1. Binary Converter (5 pts)

Write a program that prompts the user to enter an integer number, and uses a stack to convert that number to binary. If the number given is 0, exit the program. To a convert an integer number to binary:

  1. Stop if the number is zero

  1. Use the remainder operator (%) to divide by two and check the remainder

    1. If the remainder is 1, store a 1

    1. If the number is 0, store a 0

  1. Set the number to itself divided by two

  1. Go to step 1

Here is a sample run:

Enter a number: 26

26 => 11010

Enter a number: 5

5 => 101

Enter a number: 0

Exiting

Final Notes:

  • Read the sample run carefully to understand the requirements of the problem correctly

  • You can find many examples of base conversion by division online!

  • This program should be uploaded to iLearn as hw04-1.cpp

Part 2. More Queue Operations (25 points)

Extend the Queue (Static Array) starting code to add the following features. Test carefully as you add each function!

  1. Change the Queue capacity to 7

  1. Change QueueElement to char

  1. Test thoroughly; come up with scenarios in which the various parts of each function will be used

  • bool isSorted() const;

This function checks if the queue is sorted in ascending or descendingorder. For example, if a queue contains A, C, and B, the function should return false. However, if the queue contains (A, B, B, F)

or (F, B, B, A), it will return true. If the queue is empty or has only one element, the function should return true as well.

  • bool drop(QueueElement value);

This removes the value given in an argument in the queue. For example, if a queue contains (A, B,

C) drop(‘C’)will remove the value C from the queue and return true. For the queue (A, B, C), drop(‘D’)will not remove anything and return false. If a queue has duplication like (A, C, B, C), drop(‘C’)should remove only the first instance and return true. So, the result queue will contain (A, B, C).

  • bool indexUsed(int index) const;

This function returns true if the index is in use, or false otherwise. For example, if this is your current queue state:

myFront 5

myBack 2

myArray

Q

R

O

P

Then a call to validIndex()with the inputs 0, 1, 5, or 6 will return true, while a call to validIndex()with the inputs 2, 3, or 4 will return false. You can assume that the call will always be less than QUEUE_CAPACITY. You will use this function for the function below.

  • void dump() const;

This function should print out myFront, myBack, and myArray. If the contents of the array element are in the queue, print out that char; otherwise, print an underscore. Between each element, print a space. For example, assume the following queue:

myFront 5

myBack 2

myArray

Q

R

O

P

In this case, you would print out:

myFront: 5

myBack: 2

QR___OP

Final Notes:

  • Read the sample run carefully to understand the requirements of the problem correctly.

  • This program should be uploaded to iLearn as hw04-2.cpp

2

CST - Homework 4: Stacks and Queues Solution
$30.00 $24.00