C++ Pointers, Sorting, Functions Objectives: use pointer notation instead of sta
ID: 3577553 • 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
Hello I added Methods as per given problems (using Array Pointers and Method Prototypes)
Please check and Make a Comment if Some thing is missed
Source Code :-
----------------------------
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::boolalpha;
using std::endl;
using namespace std;
//First Sorting Method
void basicSort(int * a , int size);
//Partition Method used for Partition Sorting
int partition(int *nums, int left,int right);
//Second Sorting Method , Partition method
void partirionSort(int *nums, int left,int right);
//isSorted Method
bool isSorted(int series[], int len);
/*
*
* Main Method
*/
int main(int argc, char* argv[]) {
const int SIZE = 100;
srand(time(NULL));
//Check First Sorting Method
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...";
basicSort(series, SIZE);
cout << " The array is sorted: " << boolalpha << isSorted(series, SIZE) << endl;
//Check Second Sorting Method
for (int i=0; i<SIZE; i++) {
*(series+i) = rand() % (SIZE * 10);
}
cout << "The array is sorted: " << boolalpha << isSorted(series, SIZE) << endl;
cout << "Sorting...";
basicSort(series, SIZE);
cout << " The array is sorted: " << boolalpha << isSorted(series, SIZE) << endl;
if (series != NULL) delete[] series;
return 0;
}
//First Sorting Method
void basicSort(int * a , int size){
//Temp Variable for Sorting
int temp;
for(int i=0;i<size;i++) {
for(int j=0;j<size-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1]; /*Swap have been done here*/
a[j+1]=temp;
}
}
}
}
//Partition Method used for Partition Sorting
int partition(int *a, int left,int right){
int x;
int i;
x=a[right];
i=left-1;
for(int j=left;j<=right-1;j++)
{
if(a[j]<=x)
{
i=i+1;
swap(a[i],a[j]);
}
}
swap(a[i+left],a[right]);
return i+1;
}
//Second Sorting Method , Partition method
void partirionSort(int *a, int left,int right){
int q;
if(left<right)
{
q=partition(a,left,right);
partirionSort(a,left,q-1);
partirionSort(a,q+1,right);
}
}
//Is sorted Method
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.