Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help in simplify my C program code. I feel there is possiblity to merge rep

ID: 3838884 • Letter: N

Question

Need help in simplify my C program code. I feel there is possiblity to merge repetitive functions such as:

void row_rearrange(char tempcolumn[], int temprow[], int total);

double vertical_distance(buyer_t customer[], int target, double path);

void row_rearrange_picker(char clump_b[], int clump_a[], int total);

double vertical_distance_picker(picker_t picker[], int target, double path);

This is the link to access my c program code, compiled executable .exe and two txt file for input.

https://drive.google.com/open?id=0B7zZNbIufI7NV3BVT1Nmbmp3M0k

Please run the compiled .exe file first in shared folder by input this code in terminal : codecode 8 8 < data1.txt OR codecode 8 8 < data2.txt

to ensure that after simplify my c program code, it will able to be compiled and make same outcome as initial .exe file output.

After done, please upload to cloud drive and share link for me to download as refer. Thanks.

* UPDATE : The share folder now contains ass.pdf file that describe the problem of assignment I had given into. Thanks.

Explanation / Answer

From the four mentioned function - I made row_rearrange() as a single function and commented row_arrange_picker(), whereas the same cannot be done for vertical_distance() function since one uses the structure of buyer and the other uses the structure of picker. Also C program does not follow inheritance. Had this been C++, we could have used classes and these two functions could have been merged into one. Please find the updated code below.

PROGRAM CODE:

/* Assignment 2, Gabriel Hii Jia Enn (Student No:851507) */

/* COMP20005 Engineering Computation */

#include <stdio.h>

#include <stdlib.h>

#define INITIAL 0

#define AHEAD 1

#define FALSE 0

#define TRUE 1

#define MAXORDER 10

#define SECOND 2

#define DOUBLE 2

#define TRIPLE 3

#define CONVERSION 1.00

#define MAXBUYERS 100

#define ALPHABETS 26

#define HORIZONTAL 6.4

#define VERTICAL 3.8

#define MAXROWS 99

/* Store scanned item and its related bin locations */

typedef struct{

   int           item_no;

   int           row;

   char       column;

}itemsinfo_t;

/* Store sorted item and its related bin locations */

typedef struct{

   int           row;

   char       column;

}sortinfo_t;

/* Store order information required distance to pick up per order */

typedef struct{

   int        buyer_id;

   int        no_of_items;

   itemsinfo_t   order[MAXORDER];

   sortinfo_t   sort[MAXORDER];

   double       distance;

}buyer_t;

/* Store required pickers and distance required for each picker to travel */

typedef struct{

   sortinfo_t   newsort[MAXORDER];

   double       distance;

}picker_t;

void full_stage1(buyer_t customer[], int pax, int totalitems);

void full_stage2(buyer_t customer[], int pax);

void stage2_sorting(buyer_t customer[], int target, int total);

void row_rearrange(char tempcolumn[], int temprow[], int total);

void swap_row(int *A, int *B);

void swap_column(char *A, char *B);

void full_stage3(buyer_t customer[], int pax, char *argv[]);

double vertical_distance(buyer_t customer[], int target, double path);

double average_distance(double average, int target);

void full_stage4(buyer_t customer[], int pax, char *argv[]);

int stage4_sorting(int total, int clump_a[], char clump_b[], picker_t picker[]);

//void row_rearrange_picker(char clump_b[], int clump_a[], int total);

void stage4_distance_picker(picker_t picker[], int unit, char *argv[]);

double vertical_distance_picker(picker_t picker[], int target, double path);

int

main(int argc, char *argv[]) {

   int id, unit, item_no, row, pax=INITIAL, item, totalitems=INITIAL;

   char column;

   buyer_t customer[MAXBUYERS];

   /* Scan customer no and no of items */

while(scanf("%d %d    ",&id, &unit)==DOUBLE){

       customer[pax].buyer_id = id;

       customer[pax].no_of_items = unit;

       /* Based on no of items, record each of their information */

       for(item=INITIAL;item<unit;item++){

           scanf("%d %d%c",&item_no, &row, &column);

           customer[pax].order[item].item_no = item_no;

           customer[pax].order[item].row = row;

           customer[pax].order[item].column = column;

           totalitems++;

           /* Proceed to next scan if there are items pending to be recorded */

           if(item<unit){

               scanf(" ");

           }

           /* Proceed to next customer if all items have been scanned */

           if(item==unit-AHEAD){

               scanf(" ");

           }

       }

       /* Reset variable to prepare for next customer record */

       pax++;

       item=INITIAL;

       /* Proceed to execute program if reach maximum amount of customers */

       if(pax==MAXBUYERS){

           //break;

       }

   }

   full_stage1(customer, pax, totalitems);

   full_stage2(customer, pax);

   full_stage3(customer, pax, argv);

   full_stage4(customer, pax, argv);

   return 0;

}

/* Prints bin locatons for first and last customer orders */

void

full_stage1(buyer_t customer[], int pax, int totalitems){

   int first=customer[INITIAL].buyer_id, last=customer[pax-AHEAD].buyer_id;

   int a=customer[INITIAL].no_of_items, b=customer[pax-AHEAD].no_of_items;

   int loop1, loop2;

   printf("Stage 1 ------- ");

   printf(" orders:%4d ",pax);

   printf(" items :%4d ",totalitems);

   printf(" customer %7d,%3d items, bins:", first, a);

   /* Prints bins locations according to no of items for first customer */

   for(loop1=INITIAL;loop1<a;loop1++){

       printf("%3d%1c",customer[INITIAL].order[loop1].row,

           customer[INITIAL].order[loop1].column);

       /* Enter next line after printing out last bin location */

       if(loop1==a-AHEAD){

           printf(" ");

       }

   }

   printf(" customer %7d,%3d items, bins:", last, b);

   /* Prints bins locations according to no of items for last customer */

   for(loop2=INITIAL;loop2<b;loop2++){

       printf("%3d%1c",customer[pax-AHEAD].order[loop2].row,

           customer[pax-AHEAD].order[loop2].column);

       /* Proceed to stage 2 after printing out last bin location */

       if(loop2==b-AHEAD){

           printf(" ");

       }

   }

}

/* Prints sorted bin locatons for first and last customer orders */

void

full_stage2(buyer_t customer[], int pax) {

   int loop1, loop2, total;

   int first=customer[INITIAL].buyer_id, last=customer[pax-AHEAD].buyer_id;

   int a=customer[INITIAL].no_of_items, b=customer[pax-AHEAD].no_of_items;

   /* Sort bin locations for each customer orders */

   for(loop1=INITIAL;loop1<pax;loop1++){

           total=customer[loop1].no_of_items;

           stage2_sorting(customer, loop1, total);

   }

   printf("Stage 2 ------- ");

   printf(" customer %7d,%3d items, bins:", first, a);

   /* Prints sorted locations according to no of items for first customer */

   for(loop1=INITIAL;loop1<a;loop1++){

       printf("%3d%1c",customer[INITIAL].sort[loop1].row,

           customer[INITIAL].sort[loop1].column);

       /* Enter next line after printing out last bin location */

       if(loop1==a-AHEAD){

           printf(" ");

       }

   }

   printf(" customer %7d,%3d items, bins:", last, b);

   /* Prints sorted locations according to no of items for last customer */

   for(loop2=INITIAL;loop2<b;loop2++){

       printf("%3d%1c",customer[pax-AHEAD].sort[loop2].row,

           customer[pax-AHEAD].sort[loop2].column);

       /* Proceed to stage 3 after printing out last bin location */

       if(loop2==b-AHEAD){

           printf(" ");

       }

   }

}

/* Sort bin locations for each customer */

void

stage2_sorting(buyer_t customer[], int target, int total){

   int loop, loop1, loop2, loop3, loop4, temprow[total];

   char tempcolumn[total];

   /* Store every rows and columns respectively into two temporary arrays */

   for(loop=INITIAL;loop<total;loop++){

       temprow[loop]=customer[target].order[loop].row;

       tempcolumn[loop]=customer[target].order[loop].column;

   }

   /* Insertion method to arrange column in ascending order */

   for(loop1=AHEAD;loop1<total;loop1++){

       for(loop2=loop1-AHEAD;loop2>=INITIAL;loop2--){

           if(tempcolumn[loop2+AHEAD]<tempcolumn[loop2]){

               swap_column(&tempcolumn[loop2],&tempcolumn[loop2+AHEAD]);

               /* Swap rows if their respective columns have been swaped */

               swap_row(&temprow[loop2],&temprow[loop2+AHEAD]);

           }

       }

   }

   /* Arrange rows by brute force based on what columns they fall into */

   for(loop3=INITIAL;loop3<MAXROWS;loop3++){

       row_rearrange(tempcolumn, temprow, total);

   }

   /* Store sorted bin locations back to main struct */

   for(loop4=INITIAL;loop4<total;loop4++){

       customer[target].sort[loop4].row=temprow[loop4];

       customer[target].sort[loop4].column=tempcolumn[loop4];

   }

}

/* Arrange rows by brute force based on what columns they fall into */

void

row_rearrange(char tempcolumn[], int temprow[], int total){

   int loop;

   /* Run for all available locations based on no of items */

   for(loop=INITIAL;loop<total;loop++){

       /* If current column same as next column */

       if(tempcolumn[loop]==tempcolumn[loop+AHEAD]){

           /* If current column is a,c,e,g,i,k,m,o,q,s,u,w,y */

           if(tempcolumn[loop]%DOUBLE==TRUE){

               /* If value of current row bigger than value of next row */

               if(temprow[loop+AHEAD]<temprow[loop]){

                   swap_row(&temprow[loop],&temprow[loop+AHEAD]);

               }

           }

           /* If current column is b,d,f,h,j,l,n,p,r,t,v,x,z */

           if(tempcolumn[loop]%DOUBLE==FALSE){

               /* If value of current row smaller than value of next row */

               if(temprow[loop+AHEAD]>temprow[loop]){

                   swap_row(&temprow[loop],&temprow[loop+AHEAD]);

               }

           }

       }

   }

}

/* Exchange current row and next row */

void

swap_row(int *A, int *B){

   int temp;

   temp = *A;

   *A = *B;

   *B = temp;

}

/* Exchange current column and next column */

void

swap_column(char *A, char *B){

   char temp;

   temp = *A;

   *A = *B;

   *B = temp;

}

/* Exceute required stage three procedures and proceed to printout */

void

full_stage3(buyer_t customer[], int pax, char *argv[]) {

   int loop1,loop2,print,row=atoi(argv[AHEAD]),column=atoi(argv[SECOND]);

   double horizontal,vertical,path,total,average=INITIAL;

   printf("Stage 3 ------- ");

   printf(" warehouse has %2d rows and %2d columns ",row, column);

   /* Calculate total required horizontal distance based on defined columns */

   horizontal=(column-AHEAD)*HORIZONTAL;

   /* Calculate vertical distance for each columns based on defined rows*/

   path=(row+AHEAD)*VERTICAL;

   /* Calculate distance required for each customer order */

   for(loop1=INITIAL;loop1<pax;loop1++){

       /* Total vertical distance of current customer order */

       vertical=vertical_distance(customer, loop1, path);

       /* Total = horizontal dist + vertical dist + distance for entry/exit */

       total=horizontal+vertical+VERTICAL;

       /* Return total distance per customer order back to main struct */

       customer[loop1].distance=total;

   }

   /* Printout required distance for first and last customer order */

   for(print=INITIAL;print<=pax-AHEAD;print+=AHEAD){

       printf(" customer %d,",customer[print].buyer_id);

       printf("%3d items,",customer[print].no_of_items);

       printf(" pick distance:%6.1lf metres ",customer[print].distance);

   }

   /* Summing up distances for all customer orders */

   for(loop2=INITIAL;loop2<pax;loop2++){

       average+=customer[loop2].distance;

   }

   /* Printout average distance based on number of customer orders */

   printf(" average distance per order over %2d orders:",pax);

   printf("%6.1lf metres ", average_distance(average,pax));

}

/* Total vertical distance of current customer order is calculated here*/

double

vertical_distance(buyer_t customer[], int target, double path){

   /* Initialise variable to be column of first bin location */

   char compare=customer[target].sort[INITIAL].column;

   int loop;

   double total=INITIAL;

   /* If column of first bin location is a,c,e,g,i,k,m,o,q,s,u,w,y */

   if(compare%DOUBLE==TRUE){

       /* Only required to walk single column path */

       total+=path;

   }

   /* If column of first bin location is b,d,f,h,j,l,n,p,r,t,v,x,z */

   if(compare%DOUBLE==FALSE){

       /* Need to walk two column path to reach current column*/

       total+=path+path;

   }

   /* Proceed to compare with column of next bin locations */

   for(loop=AHEAD;loop<MAXORDER;loop++){

       /* Ignore if column of next bin location is b,d,f,h,j,l,n,p,r,t,v,x,z */

       if(customer[target].sort[loop].column==FALSE){

           break;

       }

       /* If next column is different than current column */

       if(compare!=customer[target].sort[loop].column){

           /* If next column is different path direction than current column */

           if((compare%DOUBLE)!=(customer[target].sort[loop].column%DOUBLE)){

               /* Only required to walk single column path */

               total+=path;

           }

           /* If next column is same path direction as current column */

           if((compare%DOUBLE)==(customer[target].sort[loop].column%DOUBLE)){

               /* Need to walk two column path to reach next column*/

               total+=path+path;

           }

       }

       /* Set current column as next column */

       compare=customer[target].sort[loop].column;

   }

   /* If column of last bin location is a,c,e,g,i,k,m,o,q,s,u,w,y */

   if(compare%DOUBLE==TRUE){

       /* Need walk additional column path before able to reach exit*/

       total+=path;

   }

   /* Return total vertical distance of current customer order */

   return total;

}

/* Calculate average distance and return back to designated function */

double

average_distance(double average, int target){

   average=average/target*CONVERSION;

   return average;

}

/* Exceute required stage four procedures and proceed to printout */

void

full_stage4(buyer_t customer[], int pax, char *argv[]){

   picker_t picker[MAXBUYERS];

   int max,unit,loop1,loop2=INITIAL,loop3,loop4,clump_a[MAXBUYERS*MAXORDER];

   char clump_b[MAXBUYERS*MAXORDER];

   double average=INITIAL;

   /* Accumulate all bins rows and columns into two temporary arrays */

   for(loop1=INITIAL;loop1<pax;loop1++){

       max=customer[loop1].no_of_items;

       for(loop3=INITIAL;loop3<max;loop3++){

               clump_a[loop2]=customer[loop1].order[loop3].row;

               clump_b[loop2]=customer[loop1].order[loop3].column;

               loop2++;

       }

   }

   /* Figure out how many pickers need for efficient picking */

   unit=stage4_sorting(loop2, clump_a, clump_b, picker);

   /* Calculate walk distance for each picker and store into picker struct */

   stage4_distance_picker(picker, unit, argv);

   /* Sum of walk distance for all pickers required */

   for(loop4=INITIAL;loop4<unit;loop4++){

       average+=picker[loop4].distance;

   }

   /* Proceed to printout no of pickers and average distance */

   printf("Stage 4 ------- ");

   printf(" pickers required:%3d ",unit);

   printf(" average distance per order over %2d orders:",pax);

   printf("%6.1lf metres ", average_distance(average, pax));

}

/* Sorting accumulated rows and columns to figure out how many pickers need */

int

stage4_sorting(int total, int clump_a[], char clump_b[], picker_t picker[]){

   int loop1, loop2, loop3, loop4, loop5, unit;

   loop5 = unit = INITIAL;

   /* Insertion method to arrange column in ascending order */

   for(loop1=AHEAD;loop1<total;loop1++){

       for(loop2=loop1-AHEAD;loop2>=INITIAL;loop2--){

           if(clump_b[loop2+AHEAD]<clump_b[loop2]){

               swap_column(&clump_b[loop2],&clump_b[loop2+AHEAD]);

               /* Swap rows if their respective columns have been swaped */

               swap_row(&clump_a[loop2],&clump_a[loop2+AHEAD]);

           }

       }

   }

   /* Arrange rows by brute force based on what columns they fall into */

   for(loop3=INITIAL;loop3<MAXROWS;loop3++){

       row_rearrange(clump_b, clump_a, total);

   }

   /* Assign maximum 10 items per picker for all accumulated items info */

   for(loop4=INITIAL;loop4<total;loop4++){

       picker[unit].newsort[loop5].row=clump_a[loop4];

       picker[unit].newsort[loop5].column=clump_b[loop4];

       /* loop5 record no of items hold by current picker */

       loop5++;

       /* If no of items reach ten items, proceed to next picker */

       if(loop5==MAXORDER){

           unit++;

           loop5=INITIAL;

       }

   }

   /* Recorrect number of pickers since it counts from zero */

   unit++;

   return unit;

}

/* Arrange rows by brute force based on what columns they fall into */

/*void

row_rearrange_picker(char clump_b[], int clump_a[], int total){

   int loop;

   /* Run for all available locations based on no of items */

   /*for(loop=INITIAL;loop<total;loop++){

       /* If current column same as next column */

       /*if(clump_b[loop]==clump_b[loop+AHEAD]){

           /* If current column is a,c,e,g,i,k,m,o,q,s,u,w,y */

           /*if(clump_b[loop]%DOUBLE==TRUE){

               /* If value of current row bigger than value of next row */

           /*   if(clump_a[loop+AHEAD]<clump_a[loop]){

                   swap_row(&clump_a[loop],&clump_a[loop+AHEAD]);

               }

           }

           /* If current column is b,d,f,h,j,l,n,p,r,t,v,x,z */

           /*if(clump_b[loop]%DOUBLE==FALSE){

               /* If value of current row bigger than value of next row */

               /*if(clump_a[loop+AHEAD]>clump_a[loop]){

                   swap_row(&clump_a[loop],&clump_a[loop+AHEAD]);

               }

           }

       }

   }

}*/

/* Calculate walking distance required for each picker */

void

stage4_distance_picker(picker_t picker[], int unit, char *argv[]) {

   int loop, rows=atoi(argv[AHEAD]), columns=atoi(argv[SECOND]);

   double horizontal, vertical, path, total;

   /* Calculate total required horizontal distance based on defined columns */

   horizontal=(columns-AHEAD)*HORIZONTAL;

   /* Calculate vertical distance for each columns based on defined rows*/

   path=(rows+AHEAD)*VERTICAL;

   /* For every picker required to carry out this efficient algorithim */

   for(loop=INITIAL;loop<unit;loop++){

       /* Total vertical distance of current picker */

       vertical=vertical_distance_picker(picker, loop, path);

       /* Total = horizontal dist + vertical dist + distance for entry/exit */

       total=horizontal+vertical+VERTICAL;

       /* Store total distance of current picker back into picker struct */

       picker[loop].distance=total;

   }

}

/* Total vertical distance of current picker is calculated in here */

double

vertical_distance_picker(picker_t picker[], int target, double path){

   /* Initialise variable to be column of first bin location */

   char compare=picker[target].newsort[INITIAL].column;

   int loop;

   double total=INITIAL;

   /* If column of first bin location is a,c,e,g,i,k,m,o,q,s,u,w,y */

   if(compare%DOUBLE==TRUE){

       /* Only required to walk single column path */

       total+=path;

   }

   /* If column of first bin location is b,d,f,h,j,l,n,p,r,t,v,x,z */

   if(compare%DOUBLE==FALSE){

       /* Need to walk two column path to reach current column*/

       total+=path+path;

   }

   /* Proceed to compare with column of next bin locations */

   for(loop=AHEAD;loop<MAXORDER;loop++){

       /* Ignore if column of next bin location is b,d,f,h,j,l,n,p,r,t,v,x,z */

       if(picker[target].newsort[loop].column==FALSE){

           break;

       }

       /* If next column is different than current column */

       if(compare!=picker[target].newsort[loop].column){

           /* If next column is different path direction than current column */

           if((compare%DOUBLE)!=(picker[target].newsort[loop].column%DOUBLE)){

               /* Only required to walk single column path */

               total+=path;

           }

           /* If next column is same path direction as current column */

           if((compare%DOUBLE)==(picker[target].newsort[loop].column%DOUBLE)){

               /* Need to walk two column path to reach next column*/

               total+=path+path;

           }

       }

       /* Set current column as next column */

       compare=picker[target].newsort[loop].column;

   }

   /* If column of last bin location is a,c,e,g,i,k,m,o,q,s,u,w,y */

   if(compare%DOUBLE==TRUE){

       /* Need walk additional column path before able to reach exit*/

       total+=path;

   }

   /* Return total vertical distance of current picker */

   return total;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote