Created
March 3, 2016 01:16
-
-
Save lucasmarqs/4dd6904afe0ecc80181a to your computer and use it in GitHub Desktop.
Enter a int vector and order it by desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define MAX 100 | |
int defineMaxIndex(); | |
void receiveNumbers(int nums[], int maxIndex); | |
void descSort(int nums[], int maxIndex); | |
void printVector(int nums[], int maxIndex); | |
int main() | |
{ | |
int nums[MAX]; | |
int maxIndex; | |
maxIndex = defineMaxIndex(); | |
receiveNumbers(nums, maxIndex); | |
descSort(nums, maxIndex); | |
printVector(nums, maxIndex); | |
} | |
int defineMaxIndex() { | |
int num = -1; | |
while ((num < 0)||(num > 100)) { | |
printf("\n Quantity: "); | |
scanf("%d", &num); | |
} | |
return num; | |
} | |
void receiveNumbers(int nums[], int maxIndex) { | |
int i; | |
for (i = 0; i < maxIndex; i++) { | |
printf("\n Number %d: ", i + 1); | |
scanf("%d", &nums[i]); | |
} | |
} | |
void descSort(int nums[], int maxIndex) { | |
int i, j, aux; | |
for (i = 0; i < maxIndex; i++) { | |
for (j = i; j < maxIndex; j++) { | |
if (nums[i] < nums[j]) { | |
aux = nums[i]; | |
nums[i] = nums[j]; | |
nums[j] = aux; | |
} | |
} | |
} | |
} | |
void printVector(int nums[], int maxIndex) { | |
int i; | |
printf("\n [ "); | |
for (i = 0; i < maxIndex; i++) { | |
if (i != (maxIndex - 1)) | |
printf("%d, ", nums[i]); | |
else | |
printf("%d ]\n", nums[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment