0

I need to transfer elements from the total costs (int totalCosts) into a new array to be called costs[]. I tried doing this at the end of the code but when I tried to access the first element in the array, it shows ALL the total costs from the output. I only needed to access the first one.

// ******** CREATE "example.txt" IN THE FOLDER THAT HAS THE PROJECT'S .CPP FILE ******** #include <iostream> #include <fstream> #include <algorithm> #include <cmath> #include <vector> #include <list> #include <iterator> using namespace std; class vehicle_capacities { public: int lw, vw; }; double nearest_ten(double n) { return round(n / 10.0 + 0.4) * 10.0; } bool cmp_fn(int a, int b) { return a > b; } int main() { int cw; vehicle_capacities cap; cap.lw = 30; cap.vw = 10; ifstream myfile("example.txt"); if (myfile.is_open()) { myfile >> cw; myfile.close(); } else { cout << "Unable to open file" << endl; } cout << "Amount of cargo to be transported: " << cw; cw = nearest_ten(cw); cout << "(" << cw << ")" << endl; int maxl = cw / cap.lw; // maximum no. of lorries that can be there vector<pair<int, int>> solutions; //vector<int> costs; vector<int>::iterator it; // for the inclusive range of 0 to maxl, find the corresponding no. of vans for each variant of no of lorries for (int l = 0; l <= maxl; ++l) { bool is_integer = (cw - l * cap.lw) % cap.vw == 0; // only if this is true, then there is an integer which satisfies for given l if (is_integer) { int v = (cw - l * cap.lw) / cap.vw; // no of vans solutions.push_back(make_pair(l, v)); } } cout << "Number of mini-lorries: "; for (auto& solution : solutions) { cout << solution.first << " "; } cout << "\n"; cout << "Number of vans: "; for (auto& solution : solutions) { cout << solution.second << " "; } cout << "\n"; cout << "Total cost: "; // LORRY COST = $200, VAN COST = $45 for (auto& solution : solutions) { int totalCosts = (solution.first * 200) + (solution.second * 45); cout << totalCosts << " "; } /*for (auto& solution : solutions) { int totalcosts = (solution.first * 200) + (solution.second * 45); costs.push_back(totalcosts); for (it = costs.begin(); it < costs.end(); it++) { cout << *it << " "; } }*/ cout << endl; // Comparison between both vehicles, highest amount = trips needed cout << "Trips Needed: "; for (auto& solution : solutions) { int a = solution.first; int b = solution.second; if (a > b) { cout << a << " "; } else if (b > a) { cout << b << " "; } else if (a == b) { cout << a << " "; } } cout << endl; cout << "Lowest #1: "; for (auto& solution : solutions) { int totalCosts[] = { (solution.first * 200) + (solution.second * 45) }; int elements = sizeof(totalCosts) / sizeof(totalCosts[0]); sort(totalCosts, totalCosts + elements, cmp_fn); for (int i = 0; i < elements; ++i) // print the results cout << totalCosts[i] << " "; cout << totalCosts[0] << " "; } // *** FOR SORTING ELEMENTS IN ARRAY LOW TO HIGH *** /*int array[] = { 1,10,21,55,1000,556 }; int elements = sizeof(array) / sizeof(array[0]); // Get number of elements in array sort(array, array + elements); for (int i = 0; i < elements; ++i) // print the results cout << array[i] << ' ';*/ return 0; } 

Do update if you have any solutions, thank you.

(Note that you have to create "example.txt" in project file.

6
  • You should learn how to step line by line through your code with a debugger. Commented Feb 10, 2020 at 7:49
  • @ThomasSablik theres no error with the program. i just need to know how to transfer the total costs into a new array Commented Feb 10, 2020 at 7:56
  • I tried doing this at the end of the code but when I tried to access the first element in the array, it shows ALL the total costs from the output. I only needed to access the first one. This looks like a debugger would help to understand the code. Commented Feb 10, 2020 at 7:57
  • @ThomasSablik What do you mean by using debugger, local windows debugger that I have on Visual Studio only tells me if there is any error, it does not tell me how to change codes. Commented Feb 10, 2020 at 8:01
  • 1
    @cosmicsedan Set a breakpoint and step through your code line by line, while inspecting how the variables change. Here is an intro to Visual Studio debugging: learn.microsoft.com/en-us/visualstudio/debugger/… Commented Feb 10, 2020 at 8:03

1 Answer 1

1

You need to dynamically allocate your array.

int elements = solution.size(); int *totalCosts = new int[elements]; int j= 0; // transfer data for (auto& solution : solutions) { totalCosts[j++] = (solution.first * 200) + (solution.second * 45); } sort(totalCosts, totalCosts + elements, cmp_fn); for (int i = 0; i < elements; ++i) // print the results cout << totalCosts[i] << " "; cout << totalCosts[0] << " "; delete[] totalCosts; 
Sign up to request clarification or add additional context in comments.

1 Comment

Acutally you can use smart pointers to avoid managing dynamic memory yourself. But this is simpler for beginner programmers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.