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
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
Step by step
Solved in 3 steps