0% found this document useful (0 votes)
8 views3 pages

Sort Unique Integers Descending

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Sort Unique Integers Descending

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
}

You might also like