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

Show full handtrace of the following code, and describe what it accomplishes. #i

ID: 3848903 • Letter: S

Question

Show full handtrace of the following code, and describe what it accomplishes.

#include <iostream>
#include <cmath>
using namespace std;
const int THIS_WILL_NOT_CHANGE=5;
// function prototypes
void fun1(double[],int&);
void fun2(const double[],int);
void fun3(double[],int);
void fun4(double&,double&);
int fun5(const double[],int);
int main()
{
double numList[THIS_WILL_NOT_CHANGE];
int integer1=THIS_WILL_NOT_CHANGE;
fun1(numList,integer1);
fun2(numList,integer1);
fun3(numList,integer1);
fun2(numList,integer1);
return 0;
}
void fun1(double list[],int& num)
{
list[0]=1.625;
list[1]=9;
list[2]=12.375;
list[3]=-5.5;
list[4]=-0.125;
num = THIS_WILL_NOT_CHANGE;
}
void fun2(const double list[],int mun)
{
for (int i=0;i<mun;i++)
cout << i << ": " << list[i] << endl;
cout << endl;
}
void fun3(double list[],int umn)
{
for (int i=umn;i>0;i--)
{
fun4(list[fun5(list,i)],list[i-1]);
}
}
void fun4(double& n,double& m)
{
double t=n;
n=m;
m=t;
}
int fun5(const double list[],int nmu)
{
int pos=0;
for(int i=1;i<nmu;i++)
if (list[i]>list[pos])
pos = i;
return pos;
}

Explanation / Answer

Here the objective of the program is to assign 5 values to an array elements and then sort them.

The array elements are printed before the sorting and after the sorting.

The detailed explaination is as below with hand-tracing.

1. The program begins with # directives, which indicates to the pre-processor which libraries are to be included.

iostream is included for input/output functions like cout.

cmath is included for mathematical funcitons.

2. using namespace std, allows to write the code without referring to std:: in the code. the compiler knows to refer to std::

3.There is a integer constant defined with a value of 5.

4. // indicates this is a comment only for readability of the program.

5. There are declarations of 5 functions before main fuction. As these are declared here they will be defined after main function.

6. main fuction

The execution of the program starts here. It has created an array of 5 elements and also defined an integer that has been assigned value 5.

Next there are statements to call the functions with these 2 paramenters.

It calles fun1 first.

As seen in the fun1 the array elements are assigned floating point values.

Then it calls fun2, which prints out these values as below.

0: 1.625

1: 9

2: 12.375

3: -5.5

4: -0.125

Here the array index starts with 0 and endl provides a new line for the next value to be printed.

Next main function calls fun3 which calls fun4 for each element to get it in the right order of the sorted list starting with the biggest number to go to the end.

fun5 is called to identify the position where the biggest number is for each position in the array starting with index 4 or 5th position(Index goes from 0 to 4), so that the swapping can be done with that number.

2nd and 4th element get swapped as below:

0: 1.625
1: 9
2: -0.125
3: -5.5
4: 12.375

In the next call to fun5 after the swapping the result is as below, as 9 is bigger and moves down to the 4th placeand swaps place with -5.5:

0: 1.625
1: -5.5
2: -0.125
3: 9
4: 12.375

In the next call to fun5 from fun4, the swapping happens for the 3rd place and the list now looks as below:

0: -5.5
1: -0.125
2: 1.625
3: 9
4: 12.375

The final output is displayed as below when fun3 ends and the control is back to the main program:

0: -5.5
1: -0.125
2: 1.625
3: 9
4: 12.375

The program ends with return value 0.