We define \(\texttt{tuple}\) ADT allowing to manipulate, for any value of \(n\), the tuple of floats \((x_1,\cdots,x_n)\), as follows:

Type: \(\texttt{tuple}\)
Use: \(\texttt{float, integer}\)
Functions:
\(\begin{array}{p{1cm} l l l} & \textbf{Create} & \texttt{integer} & \rightarrow \texttt{tuple} \text{ (creates a tuple with null elements)}\\ & \textbf{Assign} & \texttt{tuple} \times \texttt{integer} \times \texttt{float} & \rightarrow \texttt{tuple} \text{ (assigns a value to the } i^{th} \text{ element } x_i \text{ of a tuple)}\\ & \textbf{Nth} & \texttt{tuple} \times \texttt{integer} & \rightarrow \texttt{float} \text{ (returns the } n^{th} \text{ element of a tuple)}\\ & \textbf{Rank} & \texttt{tuple} & \rightarrow \texttt{integer} \text{ (number of a tuple elements)}\\ & \textbf{Sum} & \texttt{tuple} \times \texttt{tuple} & \rightarrow \texttt{tuple}\\ & \textbf{Product} & \texttt{tuple} \times \texttt{float} & \rightarrow \texttt{tuple} \text{ (multiplies all tuple elements by a given float)}\\ \end{array}\)

Complete \(\texttt{tuple}\) ADT by the Constructors, Preconditions and Axioms paragraphs.

 


Difficulty level
This exercise is mostly suitable for students
Let n1 and n2 be 2 n-tuples, r and s 2 reals, i and n 2 integers
Nth(Create(n),i) = 0
Nth(Assign(n1,i,r),i) = r
Rang(Create(n)) = n
Rang(Assign(n1,i,r)) = Rank(n1)
Sum(Create(n),n1) = n1
Sum(Assign(n1,i,r),n2) = Assign(Sum(n1, n2),i,r+ Nth(n2,i))
Product(Create(n),r) = Create(n)
Product(Assign(n1,i,r),s) = Assign (Product(n1, s),i,s* Nth(n1,i))
	

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
UVA 10009 - All Roads Lead Where