Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> // The below function Merges two subarrays of arr[]. // First subarray...

#include <stdio.h>
#include <stdlib.h>
// The below function Merges two subarrays of arr[].
// First subarray is arr[low..mid]--left to middle
// Second subarray is arr[mid+1..high]--middle+1 element to right most element
void merge(int arr[], int low, int mid, int high)
{
int i, j, k;
int len1 = mid - low + 1;
int len2 = high - mid;

//create two temporary arrays
int A[len1], B[len2];

/* Copy data to temporary arrays A[] and B[] */
for (i = 0; i < len1; i++)
A[i] = arr[low + i]; //Copies first half of array
for (j = 0; j < len2; j++)
B[j] = arr[mid + 1 + j]; //Copies second half of array

/* Merge the temporary arrays again into arr[low..high]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = low; // Initial index of merged subarray
while (i < len1 && j < len2) {
if (A[i] <= B[j]) {
arr[k] = A[i];
i++;
}
else {
arr[k] = B[j];
j++;
}
k++;
}

/* The remaining elements of A[] should be copied, if there
are any */
while (i < len1) {
arr[k] = A[i];
i++;
k++;
}

/* The remaining elements of B[] should be copied, if there
are any */
while (j < len2) {
arr[k] = B[j];
j++;
k++;
}
}

/* low is for left most index and high is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int low, int high)
{
if (low < high) {
// checks if the left index is less than right index and avoids overflow for
int mid = low + (high - low) / 2;

// Sort first half from low to mid
mergeSort(arr, low, mid);
//Sort second half from mid+1 to high
mergeSort(arr, mid + 1, high);

merge(arr, low, mid, high); //Call merge function
}
}


/* Function to print the array */
void print(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* main program to call the mergesort functions and read inputs*/
int main()
{
int n; printf("enter number of elements");
scanf_s("%d", &n);
int arr[n];
if (n <= 50 && n >= 0)// condition to check if input less than 50
{
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Given array is \n");
print(arr, n);
mergeSort(arr, 0, n - 1);

printf("\nSorted array is \n");
print(arr, n);
return 0;
}
else
printf("invalid count entered");//since count should be between 0 and 50
}

why it has an error

I wrote c program in visual studio. but it has error   int A[len1], B[len2];

and

Solutions

Expert Solution

Solution:

Nothing is wrong with your code.

The code is compiling fine in dev c++ .

There is nothing wrong with the code.

It is running correctly.

Code:

#include <stdio.h>
#include <stdlib.h>
// The below function Merges two subarrays of arr[].
// First subarray is arr[low..mid]--left to middle
// Second subarray is arr[mid+1..high]--middle+1 element to right most element
void merge(int arr[], int low, int mid, int high)
{
        int i, j, k;
        int len1 = mid - low + 1;
        int len2 = high - mid;
    //create two temporary arrays       
        int *A = (int*) malloc (len1 * sizeof(int));
        int *B = (int*) malloc (len2 * sizeof(int));

        /* Copy data to temporary arrays A[] and B[] */
        for (i = 0; i < len1; i++)
                A[i] = arr[low + i]; //Copies first half of array
        for (j = 0; j < len2; j++)
                B[j] = arr[mid + 1 + j]; //Copies second half of array
        
        /* Merge the temporary arrays again into arr[low..high]*/
        i = 0; // Initial index of first subarray
        j = 0; // Initial index of second subarray
        k = low; // Initial index of merged subarray
        while (i < len1 && j < len2) {
                if (A[i] <= B[j]) {
                        arr[k] = A[i];
                        i++;
                }
                else {
                        arr[k] = B[j];
                        j++;
                }
                k++;
        }
        
        /* The remaining elements of A[] should be copied, if there
        are any */
        while (i < len1) {
                arr[k] = A[i];
                i++;
                k++;
        }
        
        /* The remaining elements of B[] should be copied, if there
        are any */
        while (j < len2) {
                arr[k] = B[j];
                j++;
                k++;
        }
    free(A);
    free(B);
}

/* low is for left most index and high is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int low, int high)
{
        if (low < high) {
                // checks if the left index is less than right index and avoids overflow for
                int mid = low + (high - low) / 2;
                
                // Sort first half from low to mid
                mergeSort(arr, low, mid);
                //Sort second half from mid+1 to high
                mergeSort(arr, mid + 1, high);
                
                merge(arr, low, mid, high); //Call merge function
        }
}


/* Function to print the array */
void print(int arr[], int n)
{
        int i;
        for (i = 0; i < n; i++)
                printf("%d ", arr[i]);
        printf("\n");
}

/* main program to call the mergesort functions and read inputs*/
int main()
{
        int n; printf("enter number of elements");
        scanf_s("%d", &n);
        int *arr = (int*) malloc (n * sizeof(int));
        if (n <= 50 && n >= 0)// condition to check if input less than 50
        {
                for (int i = 0; i < n; i++)
                        scanf("%d", &arr[i]);
                printf("Given array is \n");
                print(arr, n);
                mergeSort(arr, 0, n - 1);
                
                printf("\nSorted array is \n");
                print(arr, n);
        free(arr);
                return 0;
        }
        else
                printf("invalid count entered");//since count should be between 0 and 50
}

Output:


Related Solutions

Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */...
Please Work on the commented parts in the code #include <stdio.h> #include <stdlib.h> /* * */ void printArray(int *arr, int size){ int i; for( i = 0; i < size; i++) { // Print each element out printf("%d ", *(arr+i)); //Print addresses of each element printf("%p", (arr+i)); //Printing a new line printf("\n"); } } int main() { // Allows user to specify the original array size, stored in variable n1. printf("Enter original array size: "); int n1 = 0; scanf("%d",...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class: Introduction to C Programming 1057 Spring 2019 Section 004 * Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm." * Date: << 02-19-2019 >> * Version: 1 * Description: Program will prompt for some data, read in the values, perform the calculation using library math functions, and display the result. * File: lab5.c */ # define M_PI 3.14159265358979323846 /* pi */...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
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...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ IUSING SEMAPHORES 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