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

This is some C++ code i need help completeing. The explanations of what i have t

ID: 3889653 • Letter: T

Question

This is some C++ code i need help completeing. The explanations of what i have to do will be first with 1, 2, and 3. After that the code I have so far will be placed in between each one.

1.

Design and implement an algorithm that, when given a collection of integers in an unsorted array, determines the third smallest number (or third minimum). For example, if the array consists of the values 21, 3, 25, 1, 12, and 6 the algorithm should report the value 6, because it is the third smallest number in the array. Do not sort the array.

To implement your algorithm, write a function thirdSmallest that receives an array as a parameter and returns the third-smallest number. To test your function, write a program that populates an array with random numbers and then calls your function.

/********************************

* Week 4 lesson: *

* finding the smallest number *

*********************************/

#include <iostream>

using namespace std;

/*

* Returns the smallest element in the range [0, n-1] of array a

*/

int minimum(int a[], int n)

{

int min = a[0];

for (int i = 1; i < n; i++)

if (min > a[i]) min = a[i];

return min;

}

int main()

{

int a[10];

for (int i = 0; i < 10; i++)

{

a[i] = rand()%100;

cout << a[i] << " ";

}

cout << endl << "Min = " << minimum(a, 10) << endl;

return 0;

}

***************************************************************************

2.

Implement a recursive function for computing the n-th Harmonic number:

Hn=ni=11i/
.

Here you have some examples of harmonic numbers.

H1 = 1
H2 = 1 + 1/2 = 1.5
H3 = 1 + 1/2 + 1/3 = 1.8333
H 4 = 1 + 1/2 + 1/3 + 1/4 = 2.0833

/************************************************

* Week 4 lesson: *

* implementing a recursive factorial function *

*************************************************/

#include <iostream>

using namespace std;

/*

* Returns the factorial of n

*/

long factorial(int n)

{

if (n == 1)

return 1;

else

return n * factorial(n - 1);

}

int main()

{

int n;

cout << "Enter a number: ";

cin >> n;

if (n > 0)

cout << n << "!= " << factorial(n) << endl;

else

cout << "Input Error!" << endl;

return 0;

}

***************************************************************************************

3.

In Sorting Algorithms (Links to an external site.)Links to an external site. you can find the class ArrayList, where these sorting algorithms are implemented. Write a program that times both of them for various list lengths, filling the array lists with random numbers. Use at least 10 different list lengths, and be sure to include both small values and large values for the list lengths (it might be convenient to add a parameterized constructor to the class ArrayList so the size of the list can be set at the moment an ArrayList object is declared).

Create a table to record the times as follows.

Regarding the efficiency of both sorting methods, what are your conclusions? In addition to the source code and a screenshot of the execution window, please submit a separate document with the table and your conclusions about the experiment.

Note: To time a section of your source code, you can do this.

MAIN.CPP

/********************************************

* Week 4 lesson: *

* ArrayList class with sorting algorithms *

*********************************************/

#include <iostream>

#include "ArrayList.h"

#include <time.h>

using namespace std;

/*

* Program to test the ArrayList class.

*/

int main()

{

srand((unsigned)time(0));

//creating a list of integers

ArrayList numbersCopy1, numbersCopy2;

//filling the list with random integers

for (int i = 0; i<10; i++)

{

int number = rand()%100;

numbersCopy1.add(number);

numbersCopy2.add(number);

}

//printing the list

cout << "Original list of numbers:" << endl <<" ";

numbersCopy1.display();

//testing bubblesort

cout << endl << "Bubble-sorted list of numbers:" << endl <<" ";

numbersCopy1.bubbleSort();

numbersCopy1.display();

//testing quicksort

cout << endl << "Quick-sorted list of numbers:" << endl <<" ";

numbersCopy2.quicksort();

numbersCopy2.display();

return 0;

}

ArrayList.cpp

/********************************************

* Week 4 lesson: *

* ArrayList class with sorting algorithms *

*********************************************/

#include <iostream>

#include "ArrayList.h"

using namespace std;

/*

* Default constructor. Sets length to 0, initializing the list as an empty

* list. Default size of array is 20.

*/

ArrayList::ArrayList()

{

SIZE = 20;

list = new int[SIZE];

length = 0;

}

/*

* Destructor. Deallocates the dynamic array list.

*/

ArrayList::~ArrayList()

{

delete [] list;

list = NULL;

}

/*

* Determines whether the list is empty.

*

* Returns true if the list is empty, false otherwise.

*/

bool ArrayList::isEmpty()

{

return length == 0;

}

/*

* Prints the list elements.

*/

void ArrayList::display()

{

for (int i=0; i < length; i++)

cout << list[i] << " ";

cout << endl;

}

/*

* Adds the element x to the end of the list. List length is increased by 1.

*

* x: element to be added to the list

*/

void ArrayList::add(int x)

{

if (length == SIZE)

{

cout << "Insertion Error: list is full" << endl;

}

else

{

list[length] = x;

length++;

}

}

/*

* Removes the element at the given location from the list. List length is

* decreased by 1.

*

* pos: location of the item to be removed

*/

void ArrayList::removeAt(int pos)

{

if (pos < 0 || pos >= length)

{

cout << "Removal Error: invalid position" << endl;

}

else

{

for ( int i = pos; i < length - 1; i++ )

list[i] = list[i+1];

length--;

}

}

/*

* Bubble-sorts this ArrayList

*/

void ArrayList::bubbleSort()

{

for (int i = 0; i < length - 1; i++)

for (int j = 0; j < length - i - 1; j++)

if (list[j] > list[j + 1])

{

//swap list[j] and list[j+1]

int temp = list[j];

list[j] = list[j + 1];

list[j + 1] = temp;

}

}

/*

* Quick-sorts this ArrayList.

*/

void ArrayList::quicksort()

{

quicksort(0, length - 1);

}

/*

* Recursive quicksort algorithm.

*

* begin: initial index of sublist to be quick-sorted.

* end: last index of sublist to be quick-sorted.

*/

void ArrayList::quicksort(int begin, int end)

{

int temp;

int pivot = findPivotLocation(begin, end);

// swap list[pivot] and list[end]

temp = list[pivot];

list[pivot] = list[end];

list[end] = temp;

pivot = end;

int i = begin,

j = end - 1;

bool iterationCompleted = false;

while (!iterationCompleted)

{

while (list[i] < list[pivot])

i++;

while ((j >= 0) && (list[pivot] < list[j]))

j--;

if (i < j)

{

//swap list[i] and list[j]

temp = list[i];

list[i] = list[j];

list[j] = temp;

i++;

j--;

} else

iterationCompleted = true;

}

//swap list[i] and list[pivot]

temp = list[i];

list[i] = list[pivot];

list[pivot] = temp;

if (begin < i - 1)

quicksort(begin, i - 1);

if (i + 1 < end)

quicksort(i + 1, end);

}

/*

* Computes the pivot location.

*/

int ArrayList::findPivotLocation(int b, int e)

{

return (b + e) / 2;

}

ArrayList.h

/********************************************

* Week 4 lesson: *

* ArrayList class with sorting algorithms *

*********************************************/

/*

* Class implementing an array based list. Bubblesort and quicksort algorithms

* are implemented also.

*/

class ArrayList

{

public:

ArrayList ();

~ArrayList();

bool isEmpty();

void display();

void add(int);

void removeAt(int);

void bubbleSort();

void quicksort();

private:

void quicksort(int, int);

int findPivotLocation(int, int);

int SIZE; //size of the array that stores the list items

int *list; //array to store the list items

int length; //amount of elements in the list

};

List Length Bubblesort Time
(seconds) Quicksort Time
(seconds)

Explanation / Answer

#include <iostream>
using namespace std;

int thirdLargest(int arr[], int arr_size) //find 3rd largest number
{
    if (arr_size < 3)   //check if array size <3
    {
        printf(" Invalid Input ");
        return -1;
    }
    int first = arr[0];
    for (int i = 0; i < arr_size ; i++) //find largest element in array
        if (arr[i] < first)
            first = arr[i];
    int second = 100000;    //assign a very high value to compare
    for (int i = 0; i < arr_size ; i++)   //find 2ndlargest element in array
        if (arr[i] < second && arr[i] > first)
            second = arr[i];
    int third = 100000;     //assign a very high value to compare
    for (int i = 0; i < arr_size ; i++)   //find 3rd largest element in array
        if (arr[i] < third && arr[i] > second)
            third = arr[i];

    return third;   //return third largest number back to main
}

int main() {
   // your code goes here
   int n;   //input size of array
   cin>>n;
   int ar[n];
   for(int i=0;i<n;i++) //input array
   cin>>ar[i];
   cout<<thirdLargest(ar,n); //display third largest
   return 0;
}

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