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

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++)...
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; }...
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....
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Copy the content of the source code into Visual Studio. Fill in the __BLANKS__ with proper...
Copy the content of the source code into Visual Studio. Fill in the __BLANKS__ with proper code and make it work! (HINT: you will be creating two files with this source code; one .cpp file and one .h file]. Please alter the code so that: YOUR first and last name appears as the fourth customer [Make up your balance and phone number.] Your Instructors first and last name appears as the fifth customer [Make up the balance and phone number.]...
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
VISUAL STUDIO CODE Use a for loop for to create "password". Request the password at least...
VISUAL STUDIO CODE Use a for loop for to create "password". Request the password at least 3 times. If the password is 12345, the procedure ends, if the correct password entered, close excel saving changes. THANKS
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using...
PLEASE SOLVE THIS QUESTION WITHOUT CHANGING THE SOURCE CODE POSTED BELOW. (ONLY ADDING) #include <iostream> using namespace std; const int arrSize = 3; // QUESTION ii. COMPLETE A displayMatrix(...) FUNCTION HERE // QUESTION iii. COMPLETE A transposedMatrix(...) FUNCTION HERE // QUESTION iv. COMPLETE A calculateTotal(...) FUNCTION HERE int main(){ // variable declaration int mat[arrSize][arrSize], transMat[arrSize][arrSize] = {0}, total = 0; // QUESTION i. Prompt user to enter 9 integer numbers and fill in into the matrix cout << "\nThe original...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT