How Can Bubble Sort Optimize PCB Drill Bed Movements?

AI Thread Summary
The discussion revolves around creating a C program using bubble sort to optimize drill data for a PCB manufacturing company. The program must allow users to sort X coordinate values in ascending or descending order through a menu system. Key requirements include user-friendly design, structured coding practices without global data, and proper function usage. The provided code snippet demonstrates the implementation of bubble sort for both sorting orders, but it contains some limitations. Suggestions for enhancements include allowing data input from a file for easier debugging and considering the use of structures to handle both X and Y coordinate values more efficiently. The user seeks feedback on the correctness of their code and potential improvements.
Apple4ever
Messages
8
Reaction score
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;
}
}
}
}
}
 
Technology news on Phys.org


Hello,

Here is a link to one of the older C books from Numerical Recipes. Chapter 8 contains sorting algorithms. Check it out. If it is to complicated I can help you with some other code for a bubble sort.

http://www.nrbook.com/a/bookcpdf.php

Thanks
 


Thanks 4 da reply. i am new to this programming and i am not a Computer Student.

Can someone let me know the indications of limitations/enhancements of the above program...?

atleast 1 is enough thanks !
 


You might want to input the data from a file instead of user input. It will be easier to debug the program this way. Also, it's likely that the array will contain both x and y values. You could make an array of structures (since these can be assigned directly instead of having to use memcpy to move a pair of numbers).
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top