Should be in C programming. Need it quick please like under 5 hours if possible.
ID: 3737782 • Letter: S
Question
Should be in C programming. Need it quick please like under 5 hours if possible. Thank you experts.
Please assign random transparency at the beginning of the program. And let me have brief explanation of the program or algorithm. I need to explain the code to the teacher in verbal so please add comments to the lines of code
You are the lead developer of a software consulting firm that has recently landed an important software contract. As the lead developer you are responsible for the
technical delivery of all software contracts. The customer (Glass Corp) manufactures small glass cubes. The cubes are manufactured in batches of 27. The 27 cubes are then stuck together to make larger cubes. The trouble is that during the manufacturing process the small glass cubes are produced with randomly varying levels of transparency due to impurities within the glass. What GlassCorp require is a software tool that will work out the best possible arrangement of the small 27 cubes to produce larger cubes with the best overall transparency.
Your boss has given you the following requirements specification:
--------------------
Given 27 cubes of glass with differing transparency, arrange them into a cube with the best overall transparency.
Write a program which performs the arrangement as fast as possible then prints out the result and the execution time.
Explanation / Answer
Hi Student,
This is basically a sorting problem. So please find the C code below with explanation :
#include<stdio.h>
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int transparency[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(transparency)/sizeof(transparency[0]);
quickSort(transparency, 0, n-1);
printf("Sorted array: n");
printArray(transparency, n);
return 0;
}
Happy Learning :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.