- #1
Apple4ever
- 8
- 0
Urgent help need ! Bubble sort !
hi all, I was asked to built a C programme using bubble sort. here is the Question.
TASK
A PCB manufacturing company requires a software program to convert a series of a table of drill data to minimise the movement of the drill bed to speed up production i.e.to place all the holes near each other row by row. This requires sorting out a series of X coordinates points in ascending/descending order.
You are required to write an ANSI C visual studio program to provide a numerical bubble sort, to a set of data. The user should be able to select from a menu system the type of bubble sort (ascending or descending), data input and final data display i.e. four options. The program should be user friendly and coded in best programming style: no global data, good use of functions and structured commenting. You are required to submit a word document report comprising of the final source code, sample test screen dumps and indications of limitations/enhancements of your program. Below is shown an example of basic pseudo code to help with the bubble sort approach to be used.
Pseudo code
Main
Define storage array input and output
Define menu
Option 1: Call array fill for input
Option 2: Call descending bubble sort on input for X data only
Option 3: Call ascending bubble sort on input for X data only
Option 4: Call display array output
Array fill
Input: Array size, Input arrays
Output: Array full of real numbers i.e. floats
Function: Single dimension loop using stdio to input data
Sort
Input: Array size, Input array, Output array, type of sort a or d
Output: Output array with sorted data
Function: Principle used bubble sort
Compare current value in array to array+1 element
Sort A: If value is greater than value+1 then swap over elements else carry on to next element
Sort D: If value is less than value+1 then swap over elements else carry on to next element
Repeat till you have compared all elements and no swaps have occurred and then return back the final output array
Can someone please check if the undermentioned answer is correct for the above question and can you simplify more if it is possible; any new answers are wellcome !
Work Done So far;
#include<stdio.h>
#define NUM 4
void array_direction(int ar[],char direction);
int main (void)
{
int n;
int x[NUM];
char order;
int y;
printf("This is the assignment 2 of Softwares for engineers \n");
printf("Welcome to the PCB Company :\n");
printf("Please enter the values of x coordinate to be sorted:\n");
for(n=0;n< NUM;n++)
{
printf(" please enter X Value %d:",n+1);
scanf("%3d",&x[n]);
}
printf(" How do you want your values of X coordinate sorted\n"); fflush(stdin);
printf("Pease enter A for ascending order or D for descending order\n:");
scanf("%c",&order);
array_direction(x, order);
printf(" The order is:\n");
for(y=0;y< NUM;y++)
{
printf("%3d, ",x[y]);
}
fflush(stdin);
getchar();
return 0;
}
void array_direction(int ar[],char direction)
{
int outer;
int inner;
int hold;
/*by using bubble Sort*/
/*For Ascending Order*/
if( direction == 'a' || direction == 'A')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if (ar[inner] > ar[inner+1])
{
//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}
/* For Descending Order */
if(direction == 'd' || direction == 'D')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if(ar[inner] < ar[inner+1])
{
//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}
}
hi all, I was asked to built a C programme using bubble sort. here is the Question.
TASK
A PCB manufacturing company requires a software program to convert a series of a table of drill data to minimise the movement of the drill bed to speed up production i.e.to place all the holes near each other row by row. This requires sorting out a series of X coordinates points in ascending/descending order.
You are required to write an ANSI C visual studio program to provide a numerical bubble sort, to a set of data. The user should be able to select from a menu system the type of bubble sort (ascending or descending), data input and final data display i.e. four options. The program should be user friendly and coded in best programming style: no global data, good use of functions and structured commenting. You are required to submit a word document report comprising of the final source code, sample test screen dumps and indications of limitations/enhancements of your program. Below is shown an example of basic pseudo code to help with the bubble sort approach to be used.
Pseudo code
Main
Define storage array input and output
Define menu
Option 1: Call array fill for input
Option 2: Call descending bubble sort on input for X data only
Option 3: Call ascending bubble sort on input for X data only
Option 4: Call display array output
Array fill
Input: Array size, Input arrays
Output: Array full of real numbers i.e. floats
Function: Single dimension loop using stdio to input data
Sort
Input: Array size, Input array, Output array, type of sort a or d
Output: Output array with sorted data
Function: Principle used bubble sort
Compare current value in array to array+1 element
Sort A: If value is greater than value+1 then swap over elements else carry on to next element
Sort D: If value is less than value+1 then swap over elements else carry on to next element
Repeat till you have compared all elements and no swaps have occurred and then return back the final output array
Can someone please check if the undermentioned answer is correct for the above question and can you simplify more if it is possible; any new answers are wellcome !
Work Done So far;
#include<stdio.h>
#define NUM 4
void array_direction(int ar[],char direction);
int main (void)
{
int n;
int x[NUM];
char order;
int y;
printf("This is the assignment 2 of Softwares for engineers \n");
printf("Welcome to the PCB Company :\n");
printf("Please enter the values of x coordinate to be sorted:\n");
for(n=0;n< NUM;n++)
{
printf(" please enter X Value %d:",n+1);
scanf("%3d",&x[n]);
}
printf(" How do you want your values of X coordinate sorted\n"); fflush(stdin);
printf("Pease enter A for ascending order or D for descending order\n:");
scanf("%c",&order);
array_direction(x, order);
printf(" The order is:\n");
for(y=0;y< NUM;y++)
{
printf("%3d, ",x[y]);
}
fflush(stdin);
getchar();
return 0;
}
void array_direction(int ar[],char direction)
{
int outer;
int inner;
int hold;
/*by using bubble Sort*/
/*For Ascending Order*/
if( direction == 'a' || direction == 'A')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if (ar[inner] > ar[inner+1])
{
//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}
/* For Descending Order */
if(direction == 'd' || direction == 'D')
{
for (outer = 0;outer < NUM; outer++)
{
for(inner = 0;inner < NUM;inner++)
{
if(ar[inner] < ar[inner+1])
{
//swap
hold = ar[inner];
ar[inner] = ar[inner+1];
ar[inner+1] = hold;
}
}
}
}
}