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

C++ Pointers, Sorting, Functions Objectives: use pointer notation instead of sta

ID: 3574316 • Letter: C

Question

C++ Pointers, Sorting, Functions

Objectives: use pointer notation instead of standard array notation, create function prototypes, create and use functions, implement pseudocode algorithms in C++, pass arrays to functions.

Overview: Implementing a pseudocode sorting algorithm in C++ and several functions that use pointers. You are given the main function, which will create the array, call one of your functions to do the sort, and then display the results.

Requirement:You must use prototypes for all of the functions other than main.You may not have any global variables.You must use pointers for all of the code that you write. You are not allowed to use [] to access elements of the array.

Pseudocode:The following is your pseudocode for the three functions you must implement.

Starting code:

Sample Run:

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::boolalpha;
using std::endl;

bool isSorted(int series[], int len);
void swap(int* ar, int pos1, int pos2);
void sort(int * series , int SIZE);
int main(int argc, char* argv[]) {
   const int SIZE = 100;
   srand(time(NULL));
   int* series = new int[SIZE];
   for (int i=0; i<SIZE; i++) {
       *(series+i) = rand() % (SIZE * 10);
   }

   cout << "The array is sorted: " << boolalpha << isSorted(series, SIZE) << endl;
   cout << "Sorting...";
   sort(series, SIZE);
   cout << " The array is sorted: " << boolalpha << isSorted(series, SIZE) << endl;
   if (series != NULL) delete[] series;
   return 0;
}
void sort(int * series , int SIZE)
{
   int i = 0 , j = 0 , temp=0;
   for(i=0;i<SIZE;i++)
   {
       for(j=0;j<SIZE;j++)
       {
           if(*(series+i)<*(series+j))
           {
               temp=*(series+i);
               *(series+i)=*(series+j);
               *(series+j)=temp;
           }
       }
   }

}

bool isSorted(int* series, int len) {
   bool sorted = true;
   for (int i=1; i<len; i++) {
       if (*(series+i-1) > *(series+i)) {
           sorted = false;
           break;
       }
   }
   return sorted;
}

void swap(int* ar, int pos1, int pos2) {
   int temp = *(ar+pos1);
   *(ar+pos1) = *(ar+pos2);
   *(ar+pos2) = temp;
}

/*output:

The array is sorted: false
Sorting...
The array is sorted: true
*/

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