Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5: Array-based expression calculator This problem considers specifying an expression using five arrays. Consider the example arrays of capacity 12 and size 11 in

image text in transcribed

Problem 5: Array-based expression calculator This problem considers specifying an expression using five arrays. Consider the example arrays of capacity 12 and size 11 in Figure 1. Entries in array names store strings with a textual represen tation of the expression at the entry. Entries in array types specify whether the entry is a variable, a unary operation, or a binary operation. Entries in array operandi specify the index of the first operand expression in case the entry was an operation. Entries in array operand specify the index of the second operand expression in case the entry was a binary operation. Array values holds the value of the expression once it is evaluated to be discussed later). . Define an enumerated ser defined type to represent the different types of expressions. . Declare the needed arrays with an adequate capacity. Write a function printExpression that takes all the arrays, the mamber of total expressions, and an expression index i. The function prints the expression at index i. For example, the expression at index 0 is 1, the expression at index 4 is (-1), the expression at index 8is (9/=) and the expression at index 10 is!(-((x + :)/(--)))> (/)). Write a function set Var Value that takes all the arrays, the number of total expressions, an index of a variable, and an integer value cal. The function checks if the index is indeed a value. If so, it sets the corresponding entry in array values to val. Otherwise, it prints an error message and returns. Write a function computeValue that takes all the arrays, the number of total expressions, and an index i of an expression. If the index i is that of a variable, the function returns the corresponding value in array values. If i is an index of a unary operation, the function recursively calls itself on the corresponding expression index in array operandl. If i is an index of a binary operation, the function recursively calls itself on the corresponding expression indices in arrays operandl and operand2. The function takes the return values from its recursive calls and applies its own operation on them. It saves the result of the computation in array values and returns the result. Test your code in sain by initializing the arrays to match Figure 1 (see code below). 4 int main() { string nanes (capacity] - {"x", "y","z", "-","","+","7""-","/", ">,"!"); int types (capacity]={VAR, VAR, VAR, UN,BIN,BIN, BIN, UN, BIN,BINUN): int operandi (capacity]={-1,-1,-1,0,1,0, 5,6.1,7,9): int operand2 [capacity]-{-1,-1,-1,-1, 3.2.4,-1,2,8.-1): int values (capacity]: print( nanes, types, operandi, operand2, values, 11, 10); setVarValue( nanes, types, operandi operand2, values, 11,0, 5): setVarValue( nanes, types, operandi.operand2, values, 11, 1, 5): setVarValue( nanes, types, operandi.operand2, values, 11,2, 5): int val-computeValue( nanes, types, operand1, operand2, values,11,10): cout (/)). Write a function set Var Value that takes all the arrays, the number of total expressions, an index of a variable, and an integer value cal. The function checks if the index is indeed a value. If so, it sets the corresponding entry in array values to val. Otherwise, it prints an error message and returns. Write a function computeValue that takes all the arrays, the number of total expressions, and an index i of an expression. If the index i is that of a variable, the function returns the corresponding value in array values. If i is an index of a unary operation, the function recursively calls itself on the corresponding expression index in array operandl. If i is an index of a binary operation, the function recursively calls itself on the corresponding expression indices in arrays operandl and operand2. The function takes the return values from its recursive calls and applies its own operation on them. It saves the result of the computation in array values and returns the result. Test your code in sain by initializing the arrays to match Figure 1 (see code below). 4 int main() { string nanes (capacity] - {"x", "y","z", "-","","+","7""-","/", ">,"!"); int types (capacity]={VAR, VAR, VAR, UN,BIN,BIN, BIN, UN, BIN,BINUN): int operandi (capacity]={-1,-1,-1,0,1,0, 5,6.1,7,9): int operand2 [capacity]-{-1,-1,-1,-1, 3.2.4,-1,2,8.-1): int values (capacity]: print( nanes, types, operandi, operand2, values, 11, 10); setVarValue( nanes, types, operandi operand2, values, 11,0, 5): setVarValue( nanes, types, operandi.operand2, values, 11, 1, 5): setVarValue( nanes, types, operandi.operand2, values, 11,2, 5): int val-computeValue( nanes, types, operand1, operand2, values,11,10): cout

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

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago