Question

In: Computer Science

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;
}
}

void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1, n2 = r - m;
int al[n1], ar[n2];
for (int i = l;i < m + 1;i++)
{
al[i - l] = arr[i];
}
for (int i = m + 1;i < r + 1;i++)
{
ar[i - (m + 1)] = arr[i];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
c++;
if (al[i] - 1 < ar[j])
{
arr[k] = al[i];
i++;
k++;
}
else
{
arr[k] = ar[j];
j++;
k++;
}
}
while (i < n1)
{
arr[k] = al[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = ar[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main() {
int n = 1;
for (int i = 0;i < 13;i++)
{
n *= 2;
int arr[n];
c = 0;
for (int i = 0;i < n;i++)
{
arr[i] = n - i;
}
cout << "INSERTION SORT" << endl;
auto start = high_resolution_clock::now();
insertionSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast(stop - start);
cout << "Size: " << n << "\t\tComparisons: " << c << "\t\tRunning time(ms): " << duration.count() << endl;
c = 0;
for (int i = 0;i < n;i++)
{
arr[i] = n - i;
}
cout << "Merge SORT" << endl;
start = high_resolution_clock::now();
mergeSort(arr, 0, n - 1);
stop = high_resolution_clock::now();
duration = duration_cast(stop - start);
cout << "Size: " << n << "\t\tComparisons: " << c << "\t\tRunning time(ms): " << duration.count() << endl;
return 0;
}
}

Solutions

Expert Solution

//I have fixed your cpp program you had made the syntax error while using the duration_cast

//Link to code => https://repl.it/@FAYAZPASHA/TrustworthyVirtuousClasslibrary#main.cpp

#include<bits/stdc++.h>
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;
           }
}

void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1, n2 = r - m;
int al[n1], ar[n2];
for (int i = l;i < m + 1;i++)
{
al[i - l] = arr[i];
}
for (int i = m + 1;i < r + 1;i++)
{
ar[i - (m + 1)] = arr[i];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
c++;
if (al[i] - 1 < ar[j])
{
arr[k] = al[i];
i++;
k++;
}
else
{
arr[k] = ar[j];
j++;
k++;
}
}
while (i < n1)
{
arr[k] = al[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = ar[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main() {
int n = 1;
for (int i = 0;i < 13;i++)
{
n *= 2;
int arr[n];
c = 0;
for (int j = 0; j < n;j++)
{
arr[j] = n - j;
}
cout << "INSERTION SORT" << endl;
auto start = high_resolution_clock::now();
insertionSort(arr, n);
auto stop = high_resolution_clock::now();

auto duration = duration_cast<milliseconds>(stop - start);

cout << "Size: " << n << "\t\tComparisons: " << c << "\t\tRunning time(ms): " << duration.count() << endl;
c = 0;
for (int k = 0;k < n;k++)
{
arr[k] = n - k;
}
cout << "Merge SORT" << endl;
start = high_resolution_clock::now();
mergeSort(arr, 0, n - 1);
stop = high_resolution_clock::now();

duration = duration_cast<milliseconds>(stop - start);

cout << "Size: " << n << "\t\tComparisons: " << c << "\t\tRunning time(ms): " << duration.count() << endl;
return 0;
}
}


Related Solutions

HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
R code Need assistance in getting this R code to run variables (Process) char ( Age)...
R code Need assistance in getting this R code to run variables (Process) char ( Age) char tmp <- expand.grid(Process = unique(words$Process),Age =unique(words$Age)) X <- model.matrix(~ factor(Process):factor(Age), data = tmp) glht(mod, linfct = X)
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 need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface...
I keep getting this error "LetterDemo.cs(21,14): error CS1519: Unexpected symbol `string' in class, struct, or interface member declaration" Can someone please help me. Here is my code: using static System.Console; class LetterDemo {    static void Main()    {      Letter letter1 = new Letter();      CertifiedLetter letter2 = new CertifiedLetter();      letter1.Name = "Electric Company";      letter1.Date = "02/14/18";      letter2.Name = "Howe and Morris, LLC";      letter2.Date = "04/01/2019";      letter2.TrackingNumber = "i2YD45";      WriteLine(letter1.ToString());      WriteLine(letter2.ToString() +       " Tracking number: " + letter2.TrackingNumber);    } } class Letter {...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT