Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Two sample inputs are provided below. Please copy and paste them into individual *.txt files and place them in the same directory as your *.cpp

Two sample inputs are provided below. Please copy and paste them into individual *.txt files and place them in the same directory as your *.cpp file.

When an integer is read in, it is inserted into the integer array as before (you can use either base-conversion with base 10 or stoi() function to convert a string of digits to an integer). When an operator is read in, it retrieves thetwomost recent integers in the integer array and performs the operation (i.e., postfix mathematical operation). It then inserts the result into the integer array. The insertion over-writes the most recent 2 elements in the array (replaces the most recent 2 elements with a single new element)! You can assume that the input is always legal (that the array is never empty before you perform an operation, and that you will not divide by 0). You may assume that there will never be more than 100 integers, so your array size can be set to be 100.

Your program should contain a function called 'operate' that performs the operator function. The function is

int operate(int array[], int op, int index)

where the argument array is the integer array, the argument op is the operator type (0: add, 1: sub, 2: mul, 3: div), and the argument index is the first index of the 2 integers that the operator should work on. This function should also insert the result into the array. It returns the number of valid elements in the array.

Example input:5 2 sub

This performs 5 - 2. The output produced should look like the following:

Token #0: 5 (integer)

Current array: 5

Token #1: 2 (integer)

Current array: 5 2

Token #2: sub (operator)

Current array: 3

In the above example, when token #2 is received, we would call operate(array, 1, 0), which would perform the subtraction operation on elements stored in indices 0 and 1 (namely array[0]=5 and array[1]=2), and store the result, 3, into the array. Note that elements 0 and 1 of the array will be over-written by future insertions into the array. The function would return 1, since only one element (array[0] = 3) remains in the array.

Here is another example input:3 4 5 mul add 7 sub

And the corresponding output is:

Token #0: 3 (integer)

Current array: 3

Token #1: 4 (integer)

Current array: 3 4

Token #2: 5 (integer)

Current array: 3 4 5

Token #3: mul (operator)

Current array: 3 20

Token #4: add (operator)

Current array: 23

Token #5: 7 (integer)

Current array: 23 7

Token #6: sub (operator)

Current array: 16

In this above example, when performing the first operation, it would call operate(array, 2, 1) since it is operating multiplication on elements 1 and 2 (array[1] = 4, array[2] = 5), and return 2, since the final size of the array after this mul operation would be 2 (array of 3 20).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

How else might employees be motivated to improve their performance?

Answered: 1 week ago