C++ program please help!!! This assignment involves implementing a pseudocode so
ID: 3838052 • Letter: C
Question
C++ program please help!!!
This assignment involves implementing a pseudocode sorting algorithm in C++ and implementing 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. You are not allowed to use the [] (array subscript) notation in your code. All array access must be done using pointers. Having your code properly sort the array is not sufficient. Your code must implement the sort routine given in the pseudocode for this assignment. In other words, you can't copy a sort routine from somewhere else.
Program requirements
You must use prototypes for all of the functions other than main. This means that you have to add three function prototypes to the supplied code.
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. If you use [], then that code will score a 0. Note: The [] notation is used in two locations, where the memory for the array is allocated, and where that memory block is set free.
When swapping elements of the array, you must use the swap function that is supplied. That is why it has been supplied.
Pseudocode
The following is your pseudocode for the three functions you must implement.
Starting code
The following code is also available at: SortStart.cpp
Sample Run
Rubric
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
bool isSorted(int* ar, int len);
void initArray(int* ar, int len);
void displayArray(int* ar, int len);
void swap(int* ar, int pos1, int pos2);
int main(int argc, char** argv) {
srand(time(NULL));
const int NUM_TESTS = 1703;
int errorCount = 0;
int arraySize = 0;
int* nums = NULL;
int sum = 0;
for (int test = 1; test <= NUM_TESTS; test++) {
if (test % 100 == 0 || test == NUM_TESTS) {
cout << " Test number " << test;
cout.flush();
arraySize = rand() % 1000 + 1;
nums = new int[arraySize];
if (nums == NULL) {
cout << "Error allocating space for array ";
exit(EXIT_FAILURE);
}
initArray(nums, arraySize);
sum = sumArray(nums, arraySize);
sort(nums, arraySize);
if (sum != sumArray(nums, arraySize)) {
cout << "Error: Array corrupted. ";
errorCount++;
} else if (!isSorted(nums, arraySize)) {
cout << "Error: Array not sorted ";
errorCount++;
}
delete[] nums;
}
cout << " Number of tests: " << NUM_TESTS << ' ';
cout << "Number of errors: " << errorCount << ' ';
return 0;
bool isSorted(int* ar, int len) {
bool sorted = true;
for (int i=0; i<len-1; i++) {
if (*(ar+i) > *(ar+i+1)) {
sorted = false;
break;
}
}
return sorted;
}
void initArray(int* ar, int len) {
for (int i=0; i<len; i++) {
*(ar+i) = rand() % 1000 + 1;
}
}
void displayArray(int* ar, int len) {
for (int i=0; i<len; i++) {
cout << setw(5) << *(ar+i);
if (i == len-1 || i % 10 == 9) cout << ' ';
}
}
void swap(int* ar, int pos1, int pos2) {
if (pos1 != pos2) {
int temp = *(ar+pos1);
*(ar+pos1) = *(ar+pos2);
*(ar+pos2) = temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.