Question

Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the...

Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the least truck can be used to deliver items in C++.

#include <iostream>
using namespace std;

void TruckFunction(int n , int w[], int limit)
{
int trucks = 1;
int weight = 0;

cout<<endl<<"Items carried in truck " <<trucks<<": "<<endl;

for ( int i = 0 ; i < n ; i ++ )
{
if((weight + w[i]) <= limit)
{
weight += w[i];
cout<<"Item " << i+1 << " ("<<w[i]<<" kg) "<<endl;
}
else if (w[i] <= limit)
{
cout<<"Total weight in truck " << trucks<<": "<<weight<<" kg"<<endl;
trucks++;
weight = 0;
if((weight + w[i]) <= limit)
weight = w[i];
cout<<endl<<"Items carried in truck " <<trucks<<": "<<endl;
cout<<"Item " << i+1 << " ("<<w[i]<<" kg) "<<endl;
}
}
cout<<"Total weight in truck " << trucks<<": "<<weight<<" kg"<<endl;
cout<<"\nTotal number of trucks needed: "<<trucks;
cout<<endl;
}

int main()
{
int n, limit;
cout<<"Enter the amount of item going to be shipped : ";
cin>>n;
cout<<endl<<"Enter the item/(s) weight sequentially : ";
int w[n];
for ( int i = 0 ; i < n ; i ++)
{
cin>>w[i];
}
cout<<endl<<"Enter the maximum weight a truck can carry : ";
cin >> limit;
cout<<endl;

for ( int i = 0 ; i < n ; i ++)
{
if (w[i] < 1 || w[i] > limit)
{
cout<<"The value input is wrong (negative or above the limit), please re-run the program.";
cout<<endl;
return 0;
}


}
TruckFunction(n,w,limit);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode for the truckFunction is as:

truckFunction(n, w , limit) ]{

{

CURR_TRUCK = 1;

WEIGHT_LOADED = 0;

print "Items carried in truck" CURR_TRUCK;

for i=0 to n // begin for loop

{

if WEIGHT_LOADED + w[i] <= limit

{

WEIGHT_LOADED = WEIGHT_LOADED + w[i];

print Item i with weight w[i]

}

else if w[i] <= limit

{

print the truck (i) that got loaded with the WEIGHT_LOADED;

increment CURR_TRUCK by 1

Reset WEIGHT_LOADED to 0

if WEIGHT_LOADED + w[i] <= limit

{

   WEIGHT_LOADED = w[i];

}

  print "Items carried in truck" CURR_TRUCK;

  print Item i with weight w[i]

}   

} // end for-loop

print the truck (i) that got loaded with the WEIGHT_LOADED;

print the total number of trucks needed as CURR_TRUCK

}

Add a comment
Know the answer?
Add Answer to:
Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • Use the functions below to write a C++ program to find the smallest Fibonacci number greater...

    Use the functions below to write a C++ program to find the smallest Fibonacci number greater than 1,000,000 and greater than 1,000,000,000. #include <iostream> using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how

    This is what I have as of right now. I am lost on what to do next#include <stdio.h>#include <iostream>using namespace std;int main(){    double scores, numOfStudents, average = 0, highscore, sum = 0;    string names;    cout << "Hello, please enter the number of students" << endl;    cin >> numOfStudents;    do {        cout << "Please enter the student's name:\n";        cin >> names;        cout << "Please enter the student's...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • C++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the ite...

    c++, data structure Write a solution to test 2 problem 3 that uses selection sort to sort the items in the vector of integers. #include <iostream> #include <iterator> #include <string> #include <random> using namespace std; void insertionSort(int a[], int N) {    int sorted = 0;    for (int sorted = 0; sorted < N - 1; ++sorted)    {        int item_position = sorted + 1;        int item = a[item_position];        while (item_position > 0 && a[item_position - 1] > item)        {            a[item_position] =...

  • C++ question I'm trying to write a code that takes in n number of variables between...

    C++ question I'm trying to write a code that takes in n number of variables between 00 and 99. The program should be able to output the 1st and 2nd lowest digits as well as the highest and second highest digits. So if the input is 4 - 50 20 10 70 it should output 10 20 50 70. I think I've gotten the basic code but I can't seem to get the last digit to shift. #include <iostream> #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT