Last active
September 24, 2018 17:02
-
-
Save OtacilioN/20d9b13bbd20a46f8864eb5c480c5fad to your computer and use it in GitHub Desktop.
This file contains 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> | |
#include <time.h> | |
#include <stdlib.h> | |
#define FALSE 0 | |
#define TRUE 1 | |
char _isSorted(int *inputArray, int numberOfInputs); | |
void _changeElements(int inputArray[], int numberOfInputs); | |
int main(void) | |
{ | |
int numberOfInputs, actualNumber; | |
printf("How many numbers do you want to receive?: "); | |
scanf("%d", &numberOfInputs); | |
int inputArray[numberOfInputs]; | |
srand(time(NULL)); | |
for (actualNumber = 0; actualNumber < numberOfInputs; actualNumber++) | |
{ | |
scanf("%d", &inputArray[actualNumber]); | |
} | |
printf("Received %d array:\n", numberOfInputs); | |
for (actualNumber = 0; actualNumber < numberOfInputs; actualNumber++) | |
{ | |
printf("%d ", inputArray[actualNumber]); // PQ DA DANADOS ESSE PRINT NAO SAI QUANDO EU COLOCO 20? | |
} | |
while (!_isSorted(inputArray, numberOfInputs)) | |
{ | |
_changeElements(inputArray, numberOfInputs); | |
} | |
printf("Your array is sorted:\n"); | |
for (actualNumber = 0; actualNumber < numberOfInputs; actualNumber++) | |
printf("%d ", inputArray[actualNumber]); | |
return 0; | |
} | |
char _isSorted(int *inputArray, int numberOfInputs) | |
{ | |
int actualNumber; | |
for (actualNumber = 0; actualNumber < numberOfInputs - 1; actualNumber++) | |
{ | |
if (inputArray[actualNumber] > inputArray[actualNumber + 1]) | |
return FALSE; | |
} | |
return TRUE; | |
} | |
void _changeElements(int *inputArray, int numberOfInputs) | |
{ | |
int tmpElement, rand1, rand2; | |
rand1 = rand() % numberOfInputs; | |
rand2 = rand() % numberOfInputs; | |
tmpElement = inputArray[rand1]; | |
inputArray[rand1] = inputArray[rand2]; | |
inputArray[rand2] = tmpElement; | |
} |
This file contains 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
How many numbers do you want to receive?: 20 | |
1 8 7 6 8 6 5 7 8 7 6 7 8 7 6 6 7 7 5 2 |
This file contains 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> | |
#include <time.h> | |
#include <stdlib.h> | |
#define FALSE 0 | |
#define TRUE 1 | |
char _isSorted(int *inputArray, int numberOfInputs); | |
void _changeElements(int inputArray[], int numberOfInputs); | |
void printArr(int len, int v[]); | |
int main(void) | |
{ | |
int numberOfInputs, actualNumber; | |
printf("How many numbers do you want to receive?: "); | |
scanf("%d", &numberOfInputs); | |
int inputArray[numberOfInputs]; | |
srand(time(NULL)); | |
for (actualNumber = 0; actualNumber < numberOfInputs; actualNumber++) | |
{ | |
scanf("%d", &inputArray[actualNumber]); | |
} | |
printf("Received %d array:\n", numberOfInputs); | |
printArr(numberOfInputs, inputArray); | |
while (!_isSorted(inputArray, numberOfInputs)) | |
{ | |
_changeElements(inputArray, numberOfInputs); | |
} | |
printf("Your array is sorted:\n"); | |
for (actualNumber = 0; actualNumber < numberOfInputs; actualNumber++) | |
printf("%d ", inputArray[actualNumber]); | |
return 0; | |
} | |
char _isSorted(int *inputArray, int numberOfInputs) | |
{ | |
int actualNumber; | |
for (actualNumber = 0; actualNumber < numberOfInputs - 1; actualNumber++) | |
{ | |
if (inputArray[actualNumber] > inputArray[actualNumber + 1]) | |
return FALSE; | |
} | |
return TRUE; | |
} | |
void printArr(int len, int v[]) | |
{ | |
for (int i = 0; i < len; i++) | |
printf("%d ", v[i]); | |
printf("\n"); | |
} | |
void _changeElements(int *inputArray, int numberOfInputs) | |
{ | |
int tmpElement, rand1, rand2; | |
rand1 = rand() % numberOfInputs; | |
rand2 = rand() % numberOfInputs; | |
tmpElement = inputArray[rand1]; | |
inputArray[rand1] = inputArray[rand2]; | |
inputArray[rand2] = tmpElement; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment