#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 100 int
ID: 3715646 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int bubbleArray(int numbers[], int array_size);
int main()
{
char c;
int i, j;
int array1[SIZE];
int retcode = 0;
printf("Sorting Program, LAB 0 ");
//------------------------------------------------
//generate 100 random numbers and place them in array1, nubmers should range from 0 - 999
for (i = 0; i < SIZE; i++) {
array1[i] = rand() % 1000;
}
//print unsorted data 20 items per line, each number in a 4 digit space
printf(" Unsorted Data ");
j = 0;
printf("%4d", array1[SIZE]);
//------------------------------------------------------------------------
//sort the numbers in the array and print results
// array1 has unsorted data, after return it has sorted data
// print 20 numbers in each line after sorting, 4 spaces for each digit
//------------------------------------------------------------------------
retcode = bubbleArray(array1, sizeof(array1) / 4);
//print sorted array
printf(" Number of Swaps: %d ", retcode);
printf(" Sorted Data Using Arrays: ");
//----------------------------------------------------
//Please keep the following line for seeing the output
//----------------------------------------------------
printf(" End of Learning C, Bye now! ");
scanf_s("%c", &c);
scanf_s("%c", &c);
};
//-------------------------------------------
//buble sort using an array and its size
//sorted elements are stored in the same array
// returns number of swaps
//-------------------------------------------
int bubbleArray(int* numbers, int array_size)
{
//declare some more variables and modify the code to make it work
int i, j;
//return swaps;
return 0;
};
Explanation / Answer
Hi Dear,
Please find my code for bubble sort.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int bubbleArray(int numbers[], int array_size);
int main()
{
char c;
int i, j;
int array1[SIZE];
int retcode = 0;
printf("Sorting Program, LAB 0 ");
//------------------------------------------------
//generate 100 random numbers and place them in array1, nubmers should range from 0 - 999
for (i = 0; i < SIZE; i++) {
array1[i] = rand() % 1000;
}
//print unsorted data 20 items per line, each number in a 4 digit space
printf(" Unsorted Data ");
j = 0;
printf("%4d", array1[SIZE]);
//------------------------------------------------------------------------
//sort the numbers in the array and print results
// array1 has unsorted data, after return it has sorted data
// print 20 numbers in each line after sorting, 4 spaces for each digit
//------------------------------------------------------------------------
retcode = bubbleArray(array1, sizeof(array1) / 4);
//print sorted array
printf(" Number of Swaps: %d ", retcode);
printf(" Sorted Data Using Arrays: ");
//----------------------------------------------------
//Please keep the following line for seeing the output
//----------------------------------------------------
printf(" End of Learning C, Bye now! ");
scanf_s("%c", &c);
scanf_s("%c", &c);
};
//-------------------------------------------
//buble sort using an array and its size
//sorted elements are stored in the same array
// returns number of swaps
//-------------------------------------------
int bubbleArray(int* numbers, int array_size)
{
//declare some more variables and modify the code to make it work
int i, j, sCount = 0, t;
for (i = 0; i < array_size-1; i++)
// Last i elements are already in place
for (j = 0; j < array_size-i-1; j++)
if (numbers[j] > numbers[j+1]){
t = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = t;
sCount++;
}
//return swaps;
return sCount;
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.