@SylvianBryan/

# Array Final Refined

## No description

Files
• main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>

// Reference: https://stackoverflow.com/questions/1787996/c-library-function-to-do-sort

// This function is used in the quick sort function qsort
int compare_function(const void *a, const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}

int main(void) {

int start; // used as a counter in for loops
int numArray; // The number of elements in the array

printf("\n\n\nEnter the number of elements to be in array: ");
scanf("%d", &numArray);
// Declare the array to hold the integers to be sorted
int genArray [numArray];

// Ask the user for a number (integer) and add it to the
// array to be sorted
for (start = 0; start < numArray; start++) {
printf("\nEnter a number: ");
scanf("%d", &genArray[start]);
}

// Use the qsort function to sort the array in place
// See reference link at the top of this program
// to see how to use this function
qsort(genArray, sizeof(genArray)/sizeof(*genArray), sizeof(*genArray), compare_function);

printf("\nThe smallest number is: %d\n\n", genArray[0]);

printf("The largest and second largest number is: %d %d\n\n", genArray[numArray - 2], genArray [numArray - 1]);

printf("Sorted array is: ");
for (start = 0; start < numArray; start++){
printf(" %d, ",  genArray[start]);
}
return 0;
}
Fetching token