Question

In: Computer Science

fix the code with constant expression error exrpession below in visual studio #include <iostream> #include <cstdlib>...

fix the code with constant expression error exrpession below in visual studio

#include <iostream>
#include <cstdlib>
#include <ctime>

void insertion_sort(int array[], int size, int start);
void heap_sort(int B[], int n);
void build_max_heap(int B[], int n);
void max_heapify(int B[], int i, int n);
void quick_sort(int B[], int p, int r);
int partition(int B[], int p, int r);


int main() {
   int m = 10, Nf = 20000, Ns = 1000, delta = 1000, A[m][Nf];
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < Nf; j++) {
           A[i][j] = rand() % 99 + 1;
           // std::cout << A[i][j] << " ";
       }
   } std::cout << std::endl;

   //----------------------------------------------------------------------------------------------

   int ns = 1000, nf = 20000, mnf = m * nf, randArray[mnf], counter1 = 0;
   //create array with random numbers
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < Nf; j++) {
           randArray[counter1] = rand() % 20000 + 1;
           //std::cout << randArray[counter1] << " ";
           counter1++;
       }
   }std::cout << std::endl;

   //more variables
   int copyArray[mnf], multiplier = 1;
   clock_t t5, t6;


   //----------------------------------------------------------------------------------------------
   //calculations for ALG1
   for (int n = ns; n <= nf; n = n + delta) {
       //local variables
       int timeAccu = 0, counter2 = 0, timeAvg;
       for (int i = 0; i < m; i++) {
           //copy randArray for first n values of row i
           for (int j = 0; j < n; j++) {
               copyArray[counter2] = randArray[counter2];
               counter2++;
           }
           counter2 = counter2 + Nf - (delta * multiplier);
           t5 = clock();
           insertion_sort(copyArray, n, counter2 - Nf);
           t6 = clock() - t5;
           timeAccu = timeAccu + (int)t6;
       }
       multiplier++;
       counter2 = 0;
       timeAvg = timeAccu / m;
       std::cout << "The average runtime of ALG1 for " << n << " expressions is " << (float)timeAvg / 1000 << " ms" << std::endl;
       std::cout << std::endl;

   }
   std::cout << "-------------------------------------------------------------------------------------------------------" << std::endl;

   //int multiplier = 1;
   clock_t t1, t2;
   for (int n = Ns; n <= Nf; n = n + delta) {
       int timeAccu = 0/*, counter = 0*/;
       for (int i = 0; i < m; i++) {
           int *B = new int[n];
           for (int k = 0; k < n; k++) {
               B[k] = A[i][k];
           }
           //counter = counter + Nf - (delta * multiplier);
           t1 = clock();
           heap_sort(B, n - 1);
           t2 = clock() - t1;
           timeAccu = timeAccu + (int)t2;
           if (i == 9 && n == 20000){
               for (int poo = 0; poo < n; poo++) {
                   // std::cout << B[poo] << " ";
               }
           }
       }
       std::cout << "The average time for ALG2 for " << n << " expressions is " << (float)timeAccu / 10000 << " ms" << std::endl;

       std::cout << std::endl;

   }
   std::cout << "-------------------------------------------------------------------------------------------------------" << std::endl;

   //----------------------------------------------------------------------------------------------


   for (int i = 0; i < m; i++) {
       for (int j = 0; j < Nf; j++) {
           A[i][j] = rand() % 99 + 1;
           // std::cout << A[i][j] << " ";
       }
   } std::cout << std::endl;

   //int multiplier = 1;
   clock_t t3, t4;
   for (int n = Ns; n <= Nf; n = n + delta) {
       int timeAccu = 0/*, counter = 0*/;
       for (int i = 0; i < m; i++) {
           int *B = new int[n];
           for (int k = 0; k < n; k++) {
               B[k] = A[i][k];
           }
           //counter = counter + Nf - (delta * multiplier);
           t3 = clock();
           quick_sort(B, 0, n - 1);
           t4 = clock() - t3;
           timeAccu = timeAccu + (int)t4;
           if (i == 9 && n == 20000){
               for (int poo = 0; poo < n; poo++) {
                   // std::cout << B[poo] << " ";
               }
           }
       }
       std::cout << "The average time for ALG3 for " << n << " expressions is " << (float)timeAccu / 10000 << " ms" << std::endl;
       std::cout << std::endl;

   }


}

void insertion_sort(int array[], int size, int start) {
   int i, key;
   for (int j = start + 1; j < start + size; j++) {
       key = array[j];
       i = j - 1;
       while (i >= 0 && array[i] > key) {
           array[i + 1] = array[i];
           i = i - 1;
       }
       array[i + 1] = key;
   }
}

void heap_sort(int B[], int n) {
   int temp;
   build_max_heap(B, n);
   for (int i = n; i >= 1; i--) {
       temp = B[0];
       B[0] = B[n];
       B[n] = temp;
       n = n - 1;
       max_heapify(B, 0, n);
   }
}

void build_max_heap(int B[], int n) {
   for (int i = n / 2 - 1; i >= 0; i--) {
       max_heapify(B, i, n);
   }
}

void max_heapify(int B[], int i, int n) {
   int left = i * 2 + 1;
   int right = i * 2 + 2;
   int largest, temp;
   if (left <= n && B[left] > B[i]) {
       largest = left;
   }
   else largest = i;
   if (right <= n && B[right] > B[largest]) {
       largest = right;
   }
   if (largest != i) {
       temp = B[i];
       B[i] = B[largest];
       B[largest] = temp;
       max_heapify(B, largest, n);
   }
}
void quick_sort(int B[], int p, int r) {
   int q;
   if (p < r) {
       q = partition(B, p, r);
       quick_sort(B, p, q - 1);
       quick_sort(B, q + 1, r);
   }
}

int partition(int B[], int p, int r) {
   int x = B[r];
   int i = p - 1;
   int temp1, temp2;
   for (int j = p; j <= r - 1; j++) {
       if (B[j] <= x) {
           i = i + 1;
           temp1 = B[i];
           B[i] = B[j];
           B[j] = temp1;
       }
   }
   temp2 = B[i + 1];
   B[i + 1] = B[r];
   B[r] = temp2;
   return i + 1;
}

Solutions

Expert Solution

It worked for me

#include <iostream>
#include <cstdlib>
#include <ctime>
void insertion_sort(int array[], int size, int start);
void heap_sort(int B[], int n);
void build_max_heap(int B[], int n);
void max_heapify(int B[], int i, int n);
void quick_sort(int B[], int p, int r);
int partition(int B[], int p, int r);

int main() {
int m = 10, Nf = 20000, Ns = 1000, delta = 1000, A[m][Nf];
for (int i = 0; i < m; i++) {
for (int j = 0; j < Nf; j++) {
A[i][j] = rand() % 99 + 1;
// std::cout << A[i][j] << " ";
}
} std::cout << std::endl;
//----------------------------------------------------------------------------------------------
int ns = 1000, nf = 20000, mnf = m * nf, randArray[mnf], counter1 = 0;
//create array with random numbers
for (int i = 0; i < m; i++) {
for (int j = 0; j < Nf; j++) {
randArray[counter1] = rand() % 20000 + 1;
//std::cout << randArray[counter1] << " ";
counter1++;
}
}std::cout << std::endl;
//more variables
int copyArray[mnf], multiplier = 1;
clock_t t5, t6;

//----------------------------------------------------------------------------------------------
//calculations for ALG1
for (int n = ns; n <= nf; n = n + delta) {
//local variables
int timeAccu = 0, counter2 = 0, timeAvg;
for (int i = 0; i < m; i++) {
//copy randArray for first n values of row i
for (int j = 0; j < n; j++) {
copyArray[counter2] = randArray[counter2];
counter2++;
}
counter2 = counter2 + Nf - (delta * multiplier);
t5 = clock();
insertion_sort(copyArray, n, counter2 - Nf);
t6 = clock() - t5;
timeAccu = timeAccu + (int)t6;
}
multiplier++;
counter2 = 0;
timeAvg = timeAccu / m;
std::cout << "The average runtime of ALG1 for " << n << " expressions is " << (float)timeAvg / 1000 << " ms" << std::endl;
std::cout << std::endl;
}
std::cout << "-------------------------------------------------------------------------------------------------------" << std::endl;

//int multiplier = 1;
clock_t t1, t2;
for (int n = Ns; n <= Nf; n = n + delta) {
int timeAccu = 0/*, counter = 0*/;
for (int i = 0; i < m; i++) {
int *B = new int[n];
for (int k = 0; k < n; k++) {
B[k] = A[i][k];
}
//counter = counter + Nf - (delta * multiplier);
t1 = clock();
heap_sort(B, n - 1);
t2 = clock() - t1;
timeAccu = timeAccu + (int)t2;
if (i == 9 && n == 20000){
for (int poo = 0; poo < n; poo++) {
// std::cout << B[poo] << " ";
}
}
}
std::cout << "The average time for ALG2 for " << n << " expressions is " << (float)timeAccu / 10000 << " ms" << std::endl;
std::cout << std::endl;
}
std::cout << "-------------------------------------------------------------------------------------------------------" << std::endl;
//----------------------------------------------------------------------------------------------

for (int i = 0; i < m; i++) {
for (int j = 0; j < Nf; j++) {
A[i][j] = rand() % 99 + 1;
// std::cout << A[i][j] << " ";
}
} std::cout << std::endl;
//int multiplier = 1;
clock_t t3, t4;
for (int n = Ns; n <= Nf; n = n + delta) {
int timeAccu = 0/*, counter = 0*/;
for (int i = 0; i < m; i++) {
int *B = new int[n];
for (int k = 0; k < n; k++) {
B[k] = A[i][k];
}
//counter = counter + Nf - (delta * multiplier);
t3 = clock();
quick_sort(B, 0, n - 1);
t4 = clock() - t3;
timeAccu = timeAccu + (int)t4;
if (i == 9 && n == 20000){
for (int poo = 0; poo < n; poo++) {
// std::cout << B[poo] << " ";
}
}
}
std::cout << "The average time for ALG3 for " << n << " expressions is " << (float)timeAccu / 10000 << " ms" << std::endl;
std::cout << std::endl;
}

}
void insertion_sort(int array[], int size, int start) {
int i, key;
for (int j = start + 1; j < start + size; j++) {
key = array[j];
i = j - 1;
while (i >= 0 && array[i] > key) {
array[i + 1] = array[i];
i = i - 1;
}
array[i + 1] = key;
}
}
void heap_sort(int B[], int n) {
int temp;
build_max_heap(B, n);
for (int i = n; i >= 1; i--) {
temp = B[0];
B[0] = B[n];
B[n] = temp;
n = n - 1;
max_heapify(B, 0, n);
}
}
void build_max_heap(int B[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
max_heapify(B, i, n);
}
}
void max_heapify(int B[], int i, int n) {
int left = i * 2 + 1;
int right = i * 2 + 2;
int largest, temp;
if (left <= n && B[left] > B[i]) {
largest = left;
}
else largest = i;
if (right <= n && B[right] > B[largest]) {
largest = right;
}
if (largest != i) {
temp = B[i];
B[i] = B[largest];
B[largest] = temp;
max_heapify(B, largest, n);
}
}
void quick_sort(int B[], int p, int r) {
int q;
if (p < r) {
q = partition(B, p, r);
quick_sort(B, p, q - 1);
quick_sort(B, q + 1, r);
}
}
int partition(int B[], int p, int r) {
int x = B[r];
int i = p - 1;
int temp1, temp2;
for (int j = p; j <= r - 1; j++) {
if (B[j] <= x) {
i = i + 1;
temp1 = B[i];
B[i] = B[j];
B[j] = temp1;
}
}
temp2 = B[i + 1];
B[i + 1] = B[r];
B[r] = temp2;
return i + 1;
}
  



See The Output


If it doesnt work try to run it online C++ compiler and it works


Related Solutions

C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For...
C++ code won't run. Fix? //========================================================== #include <conio.h> // For function getch() #include <cstdlib> // For several general-purpose functions #include <fstream> // For file handling #include <iomanip> // For formatted output #include <iostream> // For cin, cout, and system #include <string> // For string data type using namespace std; // So "std::cout" may be abbreviated to "cout" //Converting hexadecimal to binary int main() {    char binarynum[65], hexa[17];    //Using long int because it has greater capacity    long int...
How to compile this code on Visual Studio? Because it keeps getting me an error when...
How to compile this code on Visual Studio? Because it keeps getting me an error when i compile it. #include<iostream.h> #include<cio.h> class Array { public: Array(int=0)//initalise the array with 0 value Array(const Array &); ~Array(); private: int size; int *arr; bool setvalue(int index,int value); bool getvalue(int index,int &value); Array & increment(); int getsize(); void print(); Array &Add(const Array arr); bool Equal(const Array *arr)const; Array &removeAt(int index); Array &insertAt(int index,int value); }; //End of Array class void Array::setvalue(int index,int value) //set...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
Language:C++ NEEDS TO WORK IN VISUAL BASIC error on line 41 expression must have a constant...
Language:C++ NEEDS TO WORK IN VISUAL BASIC error on line 41 expression must have a constant value #include using namespace std; //function declaration void EnterRents(int*, int); void displayRents(int*, int); void selectionSort(int*, int); int sumRents(int* temp, int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += *(temp + i); } return sum; } void Displaymemory(int* temp, int size) { /*int memory; memory=sizeof(temp)*size; cout< for (int i = 0; i < size; i++)...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Static methods used for testing //============================================================================ /** * Display the bid information...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
Take the code below, add the parameter validation as prompted by the highlighted comments. #include <iostream>...
Take the code below, add the parameter validation as prompted by the highlighted comments. #include <iostream> using namespace std; // Prototype (Wait, what's this? Hmmm... how would you find out? ) void calculateBill(double, int); int main() { // local variables int numMonths = 10; double rate = 25.99; // Perform a test for $25.99 membership. cout << "Calling the calculateBill function with arguments " ??? << rate << " and " << numMonths << "\n"; total = calculateBill(rate, numMonths); cout...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT