google bubble sort, and how to remove duplicate numbers in an array C programming
Code:
#include <stdio.h>
#include <stdlib.h>
int set_random_range(float min, float max);
int main(void)
{
int i, temp,size = 100, j,k;
int array[size];
// fill array with random 1 .. 20
for(i=0; i< size;i++)
{
array[i] = set_random_range(1.0,20.0);
}
for(i=0;i< size;i++)
printf("i= %d : element = %d\n", i, array[i]);
printf("\n removing dups : ");
/* cycles through the array elements */
for (i = 0; i <= size; i++) {
/* cycles through array elements + 1
* kepping it one step ahead **/
for (j = i + 1; j <= size;) {
/* checks for dups */
if (array[j] == array[i]) {
/* picks up where it is at, then reassign value into the element */
for (k = j; k <= size; k++) {
array[k] = array[k + 1];
}
// keeps track of total size of number set not element in array
// that stays at 100, moves it back or deincurments it by one thus moving
// the array length back one on each match. even though the
// array itself stays at 100 lenght.
size--;
} else
// if no match it moves to next element
j++;
}
}
for (i = 0; i <= size; i++) {
printf("%d \n", array[i]);
}
// Bubble sorting begins
for (i = 0; i <= size ; i++)
{
for (j = 0; j < (size - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < size; i++)
{
printf("%d\n", array[i]);
}
return 0;
}
int set_random_range(float min, float max)
{
return ((int)(min + max * rand() / RAND_MAX));
}
do not forget to seed your numbers (for whatever reason, if he is asking you to do that too in this one , maybe it is a windows thing, I run Linux and have no issues with rand() without seeding first, it generates a pool at boot up)