My code does not continue after the for loop, the cursor just remains there blinking. I tried it on multiple compilers still same problem.
Input:
3 50
60 20
100 50
120 30
when I give this input it take 3 values, than does not print the statement after the for loop. It just pauses. :(
This is the problem I face. not working after taking input (image)
Here is my code.
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
double get_optimal_value (int capacity, vector<int> weights,
vector<int> values, int n){
std::cout<<"we are in the function";
double value = 0.0;
int current_weight = 0;
vector<double> v_by_w(n);
for (int i = 0; i < n; ++i)
v_by_w[i] = values[i] / weights[i];
std::cout<<"printing the v/w elements";
for (int i = 0; i < n; ++i)
std::cout<< v_by_w[i] << " ";
while( current_weight < capacity ) {
int maxi = std::max_element(v_by_w.begin(),v_by_w.end()) -
v_by_w.begin();
if((capacity - current_weight) > weights[maxi]){
current_weight += weights[maxi];
value = values[maxi];
} else
value += v_by_w[maxi]*(capacity - current_weight);
v_by_w[maxi] = -1;
}
return value;
}
int main() {
int n;
int capacity;
char ch;
std::cin >> n >> capacity;
vector<int> values(n);
vector<int> weights(n);
for (int i = 0; i < n; i++) {
std::cout<<"hello "<<i ;
std::cin >> values[i] >> weights[i];
}
std::cout<<"we took the values"; //why won't this print?
double optimal_value = get_optimal_value(capacity, weights, values, n);
std::cout.precision(10);
std::cout << optimal_value << std::endl;
return 0;
}
My goal is to print that we took the input after taking the input.
please let me know why does this happen. What can I do to prevent it.
This will really help me :)
I tried running your code below:
int main()
{
int n;
int capacity;
char ch;
std::cin >> n >> capacity;
vector<int> values(n);
vector<int> weights(n);
for (int i = 0; i < n; i++) {
std::cout<<"hello "<<i ;
std::cin >> values[i] >> weights[i];
}
std::cout<<"we took the values"; //this is getting printed after taking 2n input from keyboard
}
Below is a explanation of sample run of your program:
On running the code, I get empty console window. At this point, the line below is expecting two input from the keyboard: std::cin >> n >> capacity; So I give the below input(2 space 3 enter) 2 3 // please note with this input 2 is assigned to variable n and 3 is assigned to variable capacity
Now the program execution control enters your for-loop and print the line below code: std::cout<<"hello "<
output hello 0 Now enter your two input:output hello 011 12 (11 space 12 enter) // after hitting enter 11 is assigned to values[0] and 12 is assigned to weights[0]
this triggers next iteration of loop, console window looks as shown below:
hello 011 12 hello 1 now enter your two input:hello 113 14(13 space 14 enter) // after hitting enter 13 is assigned to values[1] and 14 is assigned to weights[1]
Now loop is terminated and your text after loop is print on the screen.
Catch here is each iteration of for-loop requires two int input from console (std::cin >> values[i] >> weights[i];)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.