Question

In: Computer Science

int[]array1={67, 78, 92, 45, 67, 86, 24, 96, 39, 82, 86}; String[] array2 ={“Namath”, “Dockery”, “Atkinson”,...

int[]array1={67, 78, 92, 45, 67, 86, 24, 96, 39, 82, 86};

String[] array2 ={“Namath”, “Dockery”, “Atkinson”, “Sauer”, “Brady”, “Maynard”, “Boozer”};

  1. Write code using selection sort to sort array1 in descending order
  1. Write code using selection sort to alphabetize array2.
  1. Write a method called cubicNumbers that will sum the 3rd power of each of first n positive integers cublic powers . Example cubicNumbers(4)-> 43 + 33+23 +13 =64+27+8+1=100. Write this code in an iterative fashion
  1. Write a method called cubicNumbers that will sum the 3rd power of each of first n positive integers cublic powers . Example cubicNumbers(-> 43 + 33+23 +13 =64+27+8+1=100. Write this code in an recursive fashion

Solutions

Expert Solution

C++ code for the required specifications is provided below: FileName- main.cpp

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

void swappingINT(int &a, int &b) {         //swap the content of a and b
   int temp;
   temp = a;
   a = b;
   b = temp;
}
void displayINT(int *array, int size) {
   for(int i = 0; i<size; i++)
      cout << array[i] << " ";
   cout << endl;
}
void selectionSortINT(int *array, int size) {
   int i, j, imin;
   for(i = 0; i<size-1; i++) {
      imin = i;   //get index of minimum data
      for(j = i+1; j<size; j++)
         if(array[j] > array[imin])
            imin = j;
         //placing in correct position
         swap(array[i], array[imin]);
   }
}

void swapping(string &a, string &b) {         //swap the content of a and b
   string temp;
   temp = a;
   a = b;
   b = temp;
}
void display(string *array, int size) {
   for(int i = 0; i<size; i++)
      cout << array[i] << " ";
   cout << endl;
}
void selectionSort(string *array, int size) {
   int i, j, imin;
   for(i = 0; i<size-1; i++) {
      imin = i;   //get index of minimum data
      for(j = i+1; j<size; j++)
         if(array[j] < array[imin])
            imin = j;
         //placing in correct position
         swap(array[i], array[imin]);
   }
}

int cubicNumbersIterative(int n) {
    int sum = 0;
    for(int i = n; i>0; i--) {
        //cout << sum << " ";
        sum += pow(i, 3);
    }
    return sum;
}

int cubicNumbersRecursive(int n) {
    if(n == 0)
        return 0;
    return(pow(n, 3) + cubicNumbersRecursive(n-1));
}

int main() {
   int n;
   int arr1[] = {67, 78, 92, 45, 67, 86, 24, 96, 39, 82, 86}; 
   n = 11;
   string arr2[] = { "Namath", "Dockery", "Atkinson", "Sauer", "Brady", "Maynard", "Boozer"};
    int n2 = 7; 
    cout << "Array1 before Sorting: ";
   displayINT(arr1, n);
   selectionSortINT(arr1, n);
   cout << "Array1 after Sorting: ";
   displayINT(arr1, n);
   cout << "Array2 before Sorting: ";
   display(arr2, n2);
   selectionSort(arr2, n2);
   cout << "Array2 after Sorting: ";
   display(arr2, n2);
   int number = 3;
   int sumit = cubicNumbersIterative(number);
   int sumrec = cubicNumbersRecursive(number);
   cout << "\nCubic Sum Iterative: " << sumit << endl;
   cout << "\nCubic Sum Recursive: " << sumrec <<  endl;

    return 0;
}

Related Solutions

DATA SET: 105, 82, 94.5, 72.5, 92, 91, 52, 86, 100, 96, 98, 109, 96, 103,...
DATA SET: 105, 82, 94.5, 72.5, 92, 91, 52, 86, 100, 96, 98, 109, 96, 103, 68 Q1. What is the 15% trimmed mean of these 15 data points? Q2. What is the sample mean of these 15 data points? Q3. What is the probability that a randomly chosen number among these data points is between 95 and 100, exclusive of the ends? Q4. What is the probability that a randomly chosen number among these date pints is not a...
DATA SET: 105, 82, 94.5, 72.5, 92, 91, 52, 86, 100, 96, 98, 109, 96, 103,...
DATA SET: 105, 82, 94.5, 72.5, 92, 91, 52, 86, 100, 96, 98, 109, 96, 103, 68 Data Table: A 10 B 2 C 1 D 1 F 1 Q1. Considering grade C or above as a passing grade, what is the probability for a student to receive a passing grade? Q2. What is the probability of a student not receiving a passing grade? Q3. What is the probability that the student received grade A or grade B? Q4. What...
exper score salary 4 78 24 7 100 43 1 86 23.7 5 82 34.3 8...
exper score salary 4 78 24 7 100 43 1 86 23.7 5 82 34.3 8 86 35.8 10 84 38 0 75 22.2 1 80 23.1 6 83 30 6 91 33 9 88 38 2 73 26.6 10 75 36.2 1-R2= 2-F test statistic= 3.b2= 4-P-value for F test=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT