Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Parnas table depict meaning of functions and procedures Example: Consider

ID: 3805234 • Letter: U

Question

Using Parnas table depict meaning of functions and procedures

Example:

Consider the following function:

int example (int a[], int n) {
int count=0;
for (int i=0; i < n; i++) {
if (a[i] %2 == 0) count++;
}
return count;
}

The input variables are the array a and n, the size of a.
The output variables is only returnValue the value returned by the function since the array is not modified and no global variables are modified.
Clearly this function returns the number of even numbers in the array a. We can specify this in a Parnas table like:

Condition   returnValue
n = size of the array a   n1
(a[i] is even)
i=0

Question need to be help:

1. A procedure that rearranges things
void rearrange (int &a, int &b, int &c) {
int t = b; b = a; a = c; c = t;
}

2. A procedure that prints something
void print (int a, int b) {
int sum = 0; int i;
for (i=a; i <=b; i++)
sum = sum + i;
cout << sum << endl;
}

3. A procedure that computes something
void compute (int a[], int n, int key, int &i) {
for (i=0; i < n && a[i] != key; i++);
}

4. A function that computes something
int sm1 (int a[], int n) {
int t;
for (t=0, int i=0; i < n; i++) {
t = t+i;
}
return t;
}
5. A function that accumulates from an array
int sm3 (int a[], int n) {
int t;
for (t=0, int i=0; i < n; i++) {
t = t+a[i];
}
return t;
}

6. A function that computes a value from an array
int sm5 (int a[], int n) {
int t;
for (t=a[0], int i=0; i < n; i++) {
if (a[i] > t) t = a[i];
}
return t;
}

7. A function that computes something from an array
int f1 (int a[], int n, int k) {
int i=0;
while (i < n and a[i] != k) i++;
return i;
}

8. A function that computes a boolean from an array
bool f2 (int a[], int n, int k) {
int i=0;
while (i < n and a[i] != k) i++;
return i < n;
}

9. A function that computes something from an array
int f3 (int a[], int n, int k) {
int i=0;
while (i < n ) {
i++;
}
return i < n;
}

10. A function that does weird and unusual things
int f1 (int a[], int n, int k) {
int i=n-1;
while (i>=0 and a[i] != k) i++;
return i;
}

Explanation / Answer

Using Parnas table depict meaning of functions and procedures
Example:
Consider the following function:
int example (int a[], int n) {
int count=0;
for (int i=0; i < n; i++) {
if (a[i] %2 == 0) count++;
}
return count;
}
The input variables are the array a and n, the size of a.
The output variables is only returnValue the value returned by the function since the array is not modified and no global variables are modified.
Clearly this function returns the number of even numbers in the array a. We can specify this in a Parnas table like:
Condition returnValue
n = size of the array a                 n1
(a[i] is even)
i=0

Question need to be help:
1. A procedure that rearranges things
void rearrange (int &a, int &b, int &c) {
int t = b; b = a; a = c; c = t;
}
The input variables are the elements a, b, c integers passed by reference.
The function return type is void, but as the variables are passed by reference,
the values of a, b, and c are modified in the calling function. This function right rotates the
values in the variables. a value will be moved to b, b value to c, and c value to a.

2. A procedure that prints something
void print (int a, int b) {
int sum = 0; int i;
for (i=a; i <=b; i++)
sum = sum + i;
cout << sum << endl;
}
The input variables are the elements a, and b by value.
The function returns a void.
This function adds all the integer values from a to b inclusive and the sum is printed
to the screen. And the number of times the loop executes is: b-a+1.

3. A procedure that computes something
void compute (int a[], int n, int key, int &i) {
for (i=0; i < n && a[i] != key; i++);
}
The input variables are the array a, the array size n, the key element key, and the
position i, which is passed by reference.
The function performs a linear search on a list of elements for the key. It will return
the position(index) at which the element is found if found, else will return n as ouput
to the called function. The efficiency of the procedure is: O(n).

4. A function that computes something
int sm1 (int a[], int n) {
int t;
for (t=0, int i=0; i < n; i++) {
t = t+i;
}
return t;
}
The input variables are the array a, and the size n.
The function returns an integer as output.
Given the array, the function returns the sum of the first n-1 elements as output.
Note that the function is not utilizing the array a, by any means. Its just computing the
sum of values in i and finally the sum t is returned.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote