Create binary sort158: main.o merge.o mergesort.o wrt.o         gcc -o sort158 main.o merge.o mergesort.o wrt.o -lm #Create object file for main.c main.o: main.c mergesort.h         gcc -c main.c #Create object file for merge.c merge.o: merge.c mergesort.h         gcc -c merge.c #Create object file for mergesort.c mergesort.o: mergesort.c mergesort.h         gcc -c mergesort.c #Create object file for wrt.c wrt.o: wrt.c         gcc -c wrt.c #Delete the all of the object files clean:         rm *.o sort158

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I Need help with this C language program! I can't use the 'make' command to combine the files together to create a "sort158a" program to execute. There might be an error to the make file, please check carefully, which I have provided. 

Here is the updated code:

sort158a.c:

#include <stdio.h>
#include <stdlib.h>
#include <mergesort.h>

int main(void)
{
    int sz, key[] = { 4, 3, 1, 67, 55, 8, 0, 4,
                      -5, 37, 7, 4, 2, 9, 1, -1 };
    int n  = sizeof(key) / sizeof(int); /* the size of key [] */
    int i;

    /*remove first 4 elements from key [] */
    for(i = 4; i < n; i++) {
        key[ i - 4] = key[i];

    }

    sz = n - 4; /* Update the size of key [] */
    wrt(key, sz); /* Print the contents of the orginal array */

    mergesort(key, sz); /* Sort the array using mergesort */
    printf("After mergesort:\n"); /*prints "After mergesort:"*/
    wrt(key, sz); // Print the contents of the sorted array

    /* Restore first 3 elements in key[] */
    key[0] = 4;
    key[1] = 3;
    key[2] = 1;
    key[3] = 67;
    sz = n;

    return 0;
}

Merge.c

void merge(int a[], int n, int b[], int m, int sorted[]) {
    int i = 0, j = 0, k = 0;

    while (i < n && j < m) {
        if (a[i] <= b[j]) {
            sorted[k] = a[i];
            i++;
        } else {
            sorted[k] = b[j];
            j++;
        }
        k++;
    }

    while (i < n) {
        sorted[k] = a[i];
        i++;
        k++;
    }

    while (j < m) {
        sorted[k] = b[j];
        j++;
        k++;
    }
}

Mergesort.c

#include <stdlib.h>
#include <assert.h>

void merge(int a[], int n, int b[], int m);

void mergesort(int key[], int n)
{
    int j, k, m, *w;

    for (m = 1; m < n; m <<= 1);

    if (n != m) {
        for (m = 1; m < n; m <<= 1);
    }

    w = calloc(n, sizeof(int));
    assert(w != NULL);

merge.h 

#ifndef MERGESORT_H
#define MERGESORT_H

#include <stdlib.h>
#include <assert.h>

void merge(int a[], int b[], int c[], int m, int n);
void mergesort(int key[], int n);
void wrt(int key [], int sz);

#endif


Wrt.c

#include <stdio.h>
#include "mergesort.h"

/* Print out the elements of an array of integers.
   key: the array to be printed.
   sz: the number of elements in the array. */

void wrt(int key[], int sz)
{
  int   i;
printf("The array is:");
 for (i=0; i < sz; ++i)
   printf("%d", key[i]);
printf("\n");
}

    for (k = 1; k < n; k <<= 1) {
        for (j = 0; j < n - k; j += 2 * k) {
            merge(key + j, k, key + j + k, k, w + j);
        }
        for (j = 0; j < n; j++) {
            key[j] = w[j];
        }
    }

    free(w);
}

Makefile

#Create binary
sort158: main.o merge.o mergesort.o wrt.o
        gcc -o sort158 main.o merge.o mergesort.o wrt.o -lm

#Create object file for main.c
main.o: main.c mergesort.h
        gcc -c main.c


#Create object file for merge.c
merge.o: merge.c mergesort.h
        gcc -c merge.c

#Create object file for mergesort.c
mergesort.o: mergesort.c mergesort.h
        gcc -c mergesort.c

#Create object file for wrt.c
wrt.o: wrt.c
        gcc -c wrt.c

#Delete the all of the object files
clean:
        rm *.o sort158

Program Name
sort158a
Description
The program will sort any number of integers for lowest to highest.
The program will use the format and words displayed in the Example of Output.
The program will use the following files:
sort158a.c
merge.c
mergesort.h
mergesort.c
To begin do the following:
Requirements
All the module are files from the class textbook "A Book on C", 6.9 An Example: of
Merge and Merge Sort, Pages 263-269. You should have already entered these modules on
CISWEB as part of assignment A07 and will be in directory a07. If you have not done
assignment A07, do it NOW! Source code file main.c will become sort158a.c.
wrt.c
1. Create the p07 directory as required and go to it.
2.
Copy all the .c and .h files as well as the makefile from a07
3. Rename main.c to sort158a.c
4. Go into sort158a.c and change the title. Make sure to indicate that this is a modification
of main.c from "A Book on C"
5. Update the makefile to reflect the new filenames.
6. Compile the program using the make command and verify that no errors or warnings
occur. If they do, make the appropriate corrections.
7. Validate that sort158a works properly. If it does not make corrections as needed.
As written in the text and A07, the program will sort integers. However, the number of
integers must be a power of 2. In main.c, key[ ] has 16 integers. But as you should already
know, the program will not sort if one integers is removed or added.
What you are going to do is modify the mergesort.c module so that a non-power of 2
number of integers can be sorted. When you modify a module, indicate by way of
comment, what was changed, the date the change was made. You will also need to add or
remove an integer to/from key[] in module sort158a.c.
You are NOT allowed to change merge.c, mergesort.h or wrt.c files
The mergesort function in file mergesort.c MUST call the merge function which it currently does.
The main function in sort158a.c you can only change the number of integers in the key[] array.
Transcribed Image Text:Program Name sort158a Description The program will sort any number of integers for lowest to highest. The program will use the format and words displayed in the Example of Output. The program will use the following files: sort158a.c merge.c mergesort.h mergesort.c To begin do the following: Requirements All the module are files from the class textbook "A Book on C", 6.9 An Example: of Merge and Merge Sort, Pages 263-269. You should have already entered these modules on CISWEB as part of assignment A07 and will be in directory a07. If you have not done assignment A07, do it NOW! Source code file main.c will become sort158a.c. wrt.c 1. Create the p07 directory as required and go to it. 2. Copy all the .c and .h files as well as the makefile from a07 3. Rename main.c to sort158a.c 4. Go into sort158a.c and change the title. Make sure to indicate that this is a modification of main.c from "A Book on C" 5. Update the makefile to reflect the new filenames. 6. Compile the program using the make command and verify that no errors or warnings occur. If they do, make the appropriate corrections. 7. Validate that sort158a works properly. If it does not make corrections as needed. As written in the text and A07, the program will sort integers. However, the number of integers must be a power of 2. In main.c, key[ ] has 16 integers. But as you should already know, the program will not sort if one integers is removed or added. What you are going to do is modify the mergesort.c module so that a non-power of 2 number of integers can be sorted. When you modify a module, indicate by way of comment, what was changed, the date the change was made. You will also need to add or remove an integer to/from key[] in module sort158a.c. You are NOT allowed to change merge.c, mergesort.h or wrt.c files The mergesort function in file mergesort.c MUST call the merge function which it currently does. The main function in sort158a.c you can only change the number of integers in the key[] array.
Below are 3 examples of output. The first with an array of 15 integers. The second with an array
of 16 (power of 2) integers and the third with 17 integers.
Example of Output with 15 integers
Before mergesort:
4
3 1 67 55 4
mergesort:
After
-5 -1 1 1 2 3 4 4
-5 37
Example of Output with 16 integers
Before mergesort:
4 3 1 67 55 8 0
After mergesort:
-5 -1 0 1 1 2
3
4
Example of Output with 17 integers
Before mergesort:
4 3 1 67 55 80
After mergesort:
-5 -1 0 1 1 2 3
4
7 4 2 9 1 -1 6
4
4 6 7 9 37 55 67
-5 37
4 4
4 -5 37
7 4 2 9 1 -1
7 8 9 37 55 67
7 4 2 9 1 -1 6
4 4 6 7 8 9 37 55 67
Transcribed Image Text:Below are 3 examples of output. The first with an array of 15 integers. The second with an array of 16 (power of 2) integers and the third with 17 integers. Example of Output with 15 integers Before mergesort: 4 3 1 67 55 4 mergesort: After -5 -1 1 1 2 3 4 4 -5 37 Example of Output with 16 integers Before mergesort: 4 3 1 67 55 8 0 After mergesort: -5 -1 0 1 1 2 3 4 Example of Output with 17 integers Before mergesort: 4 3 1 67 55 80 After mergesort: -5 -1 0 1 1 2 3 4 7 4 2 9 1 -1 6 4 4 6 7 9 37 55 67 -5 37 4 4 4 -5 37 7 4 2 9 1 -1 7 8 9 37 55 67 7 4 2 9 1 -1 6 4 4 6 7 8 9 37 55 67
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Dictionary
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education