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 using namespace std;

void initializeRequests(Vector & requests); int cutStock(Vector & requests, int & stockLength); int recCutStock(Vector & reqCopy, int stockLength);

int main() { Vector requests; initializeRequests(requests); int stockLength = getInteger("Enter the stock length: "); cout << "Minimum number of stock pipes required to meet the requests = " << cutStock(requests, stockLength) << endl; return 0; }

/* Accepts the requested pipe lengths and stores them in the vector requests */ void initializeRequests(Vector & requests) { while (true) { int votes = getInteger("Enter required length (-1 to quit): "); if (votes == -1) break; requests.add(votes); } }

/* 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 & requests, int & stockLength) { // make a deep copy of the given vector of requests so that it can be // manipulated on Vector reqCopy = requests;

// call the actual recursive function and return its value return (recCutStock(reqCopy, stockLength)); }

/* Prototypes for helper functions */ void removeRequestedPipes(Vector & reqCopy, int stockLength, Vector & subsetPipes, int index); int sumOf(Vector & subsetPipes); bool isBetterPipe(int checkThisPipe, Vector & subsetPipes, int stockLength, int sum, int & targetIndex); void swap(int maxIndex, int index, Vector & subsetPipes, Vector & reqCopy);

/* 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 & reqCopy, int stockLength) { // if all requests have been met if (reqCopy.isEmpty()) return 0; // Base Case

Vector subsetPipes; removeRequestedPipes(reqCopy, stockLength, subsetPipes, 0);

// 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 & reqCopy, int stockLength, Vector & subsetPipes, int index) { if (index >= reqCopy.size()) return; // Base Case

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 & subsetPipes) { int sum = 0; for (int i = 0; i < subsetPipes.size(); i++) sum += subsetPipes[i]; return sum; }

/* 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 & subsetPipes, int stockLength, int sum, int & targetIndex) { for (int i = 0; i < subsetPipes.size(); i++) { if ((checkThisPipe > subsetPipes[i]) && ((sum - subsetPipes[i] + checkThisPipe) <= stockLength)) { targetIndex = i; return true; } } return false; }

/* Swaps the element at minIndex of subsetPipes with the element at index of reqCopy */ void swap(int minIndex, int index, Vector & subsetPipes, Vector & reqCopy) { int temp = reqCopy[index]; reqCopy[index] = subsetPipes[minIndex]; subsetPipes[minIndex] = temp; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!