Home > Back-end >  Iterations getting skipped in a for loop
Iterations getting skipped in a for loop

Time:09-23

I'm currently working on a reliability design algorithm, which I found on this video https://www.youtube.com/watch?v=uJOmqBwENB8 here. I wrote the code in c , but I've came across a problem where in one of the loops some iterations get skipped. Here is the whole code:

#include <iostream>
#include <cmath>
#include <vector>
#include <utility>

using namespace std;

int main(){
    int n;
    cin>>n;
    int cost[n],num_available[n],max_cost;
    double rel[n];
    for(int i=0;i<n;i  )
        cin>>cost[i];
    for(int i=0;i<n;i  )
        cin>>rel[i];
    for(int i=0;i<n;i  )
        cin>>num_available[i];
    cin>>max_cost;

    vector<pair<double,int>> resSet;
    resSet.push_back(make_pair(1,0));

    for(int i=0;i<n;i  ){
        vector<pair<double,int>> tempSet;
        for(int j=0;j<num_available[i];j  ){
            for(int pos=0;pos<resSet.size();pos  ){
                int sum_left_costs=0;
                for(int k=i 1;k<n;k  )
                    sum_left_costs =cost[k];
                if(sum_left_costs resSet[pos].second cost[i]*(j 1)>max_cost)
                    break;
                tempSet.push_back
                    (make_pair(resSet[pos].first*(1-pow((1-rel[i]),j 1))
                            ,resSet[pos].second cost[i]*(j 1)));
            }
        }
        for(int i=0;i<tempSet.size();i  ){
            cout<<"Reliability: "<<tempSet[i].first<<
                ", Price: "<<tempSet[i].second<<endl;
        }
        resSet=tempSet;
        cout<<endl;
    }

    double maxRel=0,pos;
    for(int i=0;i<resSet.size();i  ){
        if(maxRel<resSet[i].first){
            pos=i;
            maxRel=resSet[i].first;
        }
    }

    cout<<"Best Reliability: "<<resSet[pos].first<<
        " for price: "<<resSet[pos].second;

    return 0;
}

Anyways, while trying to find the problem, I added some changes to code in the loops, just to see how many iterations does the loop go through:

for(int i=0;i<n;i  ){
        cout<<"i="<<i<<", ";
        vector<pair<double,int>> tempSet;
        for(int j=0;j<num_available[i];j  ){
            cout<<"j="<<j<<", ";
            for(int pos=0;pos<resSet.size();pos  ){
                cout<<"pos="<<pos<<", ";
                int sum_left_costs=0;
                for(int k=i 1;k<n;k  )
                    sum_left_costs =cost[k];
                if(sum_left_costs resSet[pos].second cost[i]*(j 1)>max_cost)
                    break;
                tempSet.push_back
                    (make_pair(resSet[pos].first*(1-pow((1-rel[i]),j 1))
                            ,resSet[pos].second cost[i]*(j 1)));
            }
        }
        resSet=tempSet;
        cout<<endl;
    }

Now, once I executed the program, these were the results:

Input:
3
30 15 20
0.9 0.8 0.5
2 3 3
105

Output:
i=0, j=0, pos=0, j=1, pos=0, 
i=1, j=0, pos=0, pos=1, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
i=2, j=0, pos=0, pos=1, pos=2, pos=3, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
Best Reliability: 0.63 for price: 105

But the expected output should of been:

i=0, j=0, pos=0, j=1, pos=0, 
i=1, j=0, pos=0, pos=1, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
i=2, j=0, pos=0, pos=1, pos=2, pos=3, j=1, pos=0, pos=1,pos=2,pos=3, j=2, pos=0, pos=1, pos=2, pos=3
Best Reliability: 0.648 for price: 100
Any ideas what could cause these problems?
P.S. Yes i know I need to learn how to use a debugger.

CodePudding user response:

If you switch your break to a continue it gets your expected results.

Reliability: 0.9, Price: 30
Reliability: 0.99, Price: 60

Reliability: 0.72, Price: 45
Reliability: 0.792, Price: 75
Reliability: 0.864, Price: 60
Reliability: 0.8928, Price: 75

Reliability: 0.36, Price: 65
Reliability: 0.396, Price: 95
Reliability: 0.432, Price: 80
Reliability: 0.4464, Price: 95
Reliability: 0.54, Price: 85
Reliability: 0.648, Price: 100
Reliability: 0.63, Price: 105

Best Reliability: 0.648 for price: 100

break will exit the innermost loop, continue will go the next iteration

  • Related