Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ i am having trouble with my input.txt file, i don't have any errors with my main driver program. Just need help adding my

In C++ i am having trouble with my input.txt file, i don't have any errors with my main driver program.

Just need help adding my input.txt file to the main program.

Here is my code along with the input.txt file to be added. results still printed to screen. thank you

input.txt

3

10

6

5 10 2 5 4 4

10

20

4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6

10

4

3 8 2 7

C++ code below:

// C++ program to find number of bins required using // Best fit algorithm. #include using namespace std; // Returns number of bins required using best fit // online algorithm int bestFit(int weight[], int n, int c) { // Initialize result (Count of bins) int res = 0; // Create an array to store remaining space in bins // there can be at most n bins int bin_rem[n]; // Place items one by one for (int i=0; i= weight[i] && bin_rem[j] - weight[i] < min) { bi = j; min = bin_rem[j] - weight[i]; } } // If no bin could accommodate weight[i], // create a new bin if (min==c+1) { bin_rem[res] = c - weight[i]; res++; } else // Assign the item to best bin bin_rem[bi] -= weight[i]; } return res; }

// C++ program to find number of bins required using // First Fit Decreasing algorithm. #include using namespace std; /* Copy firstFit() from above */ int firstFit(int weight[], int n, int c) { // Initialize result (Count of bins) int res = 0; // Create an array to store remaining space in bins // there can be at most n bins int bin_rem[n]; // Place items one by one for (int i=0; i= weight[i]) { bin_rem[j] = bin_rem[j] - weight[i]; break; } } // If no bin could accommodate weight[i] if (j==res) { bin_rem[res] = c - weight[i]; res++; } } return res; }

// Returns number of bins required using first fit // decreasing offline algorithm int firstFitDec(int weight[], int n, int c) { // First sort all weights in decreasing order sort(weight, weight+n, std::greater()); // Now call first fit for sorted items return firstFit(weight, n, c); }

// Driver program int main() { int weight[] = {3, 8, 2, 7 }; int c = 10; int n = sizeof(weight) / sizeof(weight[0]); cout << "first fit: " << firstFit (weight, n, c); cout << "first fit dec: " << firstFitDec(weight, n, c); cout << "Number of bins required in Best Fit: " << bestFit(weight, n, c); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

explain what is meant by the terms unitarism and pluralism

Answered: 1 week ago