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);
}
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
}
Write a Pseudocode for the below implementation of Greedy Algorithm to find the number of the...
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 <<...
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...
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) { ...
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) {...
#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...
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 , 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...
My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() { int n; int flag= 1; cout << "Prime/Not Prime" << endl; cout << "Enter the Number to check Prime:" << endl; cin >> n; int m = n / 2; int i; for (i = 2; i <= m; i++) if (n % i == 0) ...
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; 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) {...