It is compiling, but it still is not working properly. If I enter a word, weird output comes up, and it does not stop once you press enter.
Here is what I get:
Enter a word: boo
Enter a word: Enter a word: Enter a word: Enter a word:
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int int_compare(const void* p, const void* q);
int main()
{
int n, i;
int *a;
printf("Enter the length of the array: ");
scanf("%d", &n);
a = malloc(n*sizeof(int));
for(i = 0; i < n; i++)
{
printf("Enter a number: ");
scanf("%d", &a[i]);
}
qsort(a, n, sizeof(int), int_compare);
printf("In sorted order:\n");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
printf("\n");
return 0;
}
int int_compare(const void* p, const void* q){
int n1 = *(int *) p;
n1 = *(int *) p;
int n2 = *(int *) q;
if (n1 < n2)
return -1;
if (n1 == n2)
return 0;
return 1;
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n') {
if (i < n)
str[i++] = ch;
}
str[i] = '\0';
return i;
}