Lab 7 Item 1
Write a program that:
(a) creates an integer array of size , where is a user input;
(b) takes integer inputs from the user and stores them in that array; and
(c) displays the integer elements of the array sorted in descending order without
repetition.
// CAMASO, KYLLE REENNIELLE D. BSABE 1B
#include <stdio.h>
#include <stdlib.h>
// Comparison function for descending sort
int compareDescending(const void *a, const void *b) {
return (*(int*)b - *(int*)a);
int main() {
int size, i;
printf("How many inputs? ");
scanf("%d", &size);
if (size <= 0) {
printf("Invalid input.\n");
return 1;
}
// Dynamic memory allocation
int *arr = (int*)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
// Input loop with "Enter number:" prompt
for (i = 0; i < size; i++) {
printf("Enter number: ");
scanf("%d", &arr[i]);
// Sort in descending order
qsort(arr, size, sizeof(int), compareDescending);
// Display unique values only (no label, just numbers)
for (i = 0; i < size; i++) {
if (i == 0 || arr[i] != arr[i - 1]) {
printf("%d ", arr[i]);
printf("\n");
free(arr);
return 0;
}