Question: Please translate these c++ codes into C language! and run it successfully. /* * CutStock.cpp * ------------ * This program accepts a list of lengths
Please translate these c++ codes into C language! and run it successfully.
/* * CutStock.cpp * ------------ * This program accepts a list of lengths of pipes needed by the * user and the length of the stock pipe available. It returns the minimum * number of stock pipes required to satisfy the list of requested pipes */
#include "simpio.h" #include "console.h" #include "vector.h" #include
void initializeRequests(Vector
int main() { Vector
/* Accepts the requested pipe lengths and stores them in the vector requests */ void initializeRequests(Vector
/* This is a wrappper function for the actual recursive function that returns * the minimum number of stockpipes required to satisfy the list of requests */ int cutStock(Vector
// call the actual recursive function and return its value return (recCutStock(reqCopy, stockLength)); }
/* Prototypes for helper functions */ void removeRequestedPipes(Vector
/* Recursively calculates the minimum number of stock pipes (of stockLength) required * to satisfy the list of requests * The basic strategy is- * 1. If there are requests remaining to be serviced then, * find a subset of the pipe lengths such that the wastage is minimized when those * pipes are cut from a pipe of stockLength. * 2. Remove that subset from the original requested pipe lengths. * 3. Recursively service the remaining requested pipe lengths */ int recCutStock(Vector
Vector
// return 1 stock pipe for the combination removed by the function above and then recursively // calculate the number of stock pipes needed to service the rest of the requested pipes return 1 + recCutStock(reqCopy, stockLength); }
/* Starting with the first requested pipe length (in reqCopy), removes a combination of requested pipes * from all the given requests (in reqCopy) in such a way that the stock pipe is used with the minimum * amount of wastage */ void removeRequestedPipes(Vector
int sum = sumOf(subsetPipes), targetIndex = 0; // requested pipe being considered in this call int currentPipe = reqCopy[index];
// if the current pipe length can be cut from the stockLength pipe then add it into the // subsetPipes vector and then remove it from the original requests if (sum + currentPipe <= stockLength) { subsetPipes.add(currentPipe); reqCopy.remove(index); removeRequestedPipes(reqCopy, stockLength, subsetPipes, index); }
// if the pipe length from original requests is longer any of the pipes in the subsetPipes // and it can be cut from the stockLength then replace it in the subset else if (isBetterPipe(reqCopy[index], subsetPipes, stockLength, sum, targetIndex)) { swap(targetIndex, index, subsetPipes, reqCopy); removeRequestedPipes(reqCopy, stockLength, subsetPipes, index); }
else { removeRequestedPipes(reqCopy, stockLength, subsetPipes, index + 1); } }
/* Returns the sum of all the elements in the vector */ int sumOf(Vector
/* Checks if the pipe length given in "checkThisPipe" is longer than any of the pipes in subsetPipes * while still being smaller or equal to the stockLength given. * if yes then update the targetIndex to the element that is smaller and return true. * else return false */ bool isBetterPipe(int checkThisPipe, Vector
/* Swaps the element at minIndex of subsetPipes with the element at index of reqCopy */ void swap(int minIndex, int index, Vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
