### vector looping problem

I'm writing a program to demonstrate static memory partitioning. The program takes user input for 5 partition sizes and then user input for any number of job sizes (I usually use 6 or 7). The logic is as follows:

1) store job sizes in vector job_size
2) store partition sizes in vector part_size
3)if job size <= partition size, then store partition size and job size in a 2D vector called results and then delete the partition so it can't be used again.
4) If there is no spot for the job (all partitions are full or all partitions are too small, then store job size and "-1" in results vector to indicate the job is waiting.

I will post the entire code, and then the section that's giving me trouble.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````int main() { vector job_size; // vector to hold job sizes vector part_size; // vector to hold partition sizes vector< pair > result; // 2D vector to hold results int size; int counter = 0; int count = 0; bool finished = false; vector::iterator psize; while (count < 5) // enter 5 partition sizes { cout << "Please enter the size for partition " << count + 1 << ": "; cin >> size; part_size.push_back (size); count++; } while (!finished) // get sizes of jobs { cout << "Please enter the size of a job (9999 to stop): "; cin >> size; if (size != 9999) { job_size.push_back (size); } else{finished=true;} } vector::iterator js; vector::iterator ps; //bool found = false; bool done = false; for (js = job_size.begin(); // loop through job sizes js != job_size.end(); js++) { for (ps = part_size.begin(); // loop through partitions for jobs ps != part_size.end(); ) { if (*js <= *ps) // check if job size <= partition size { result.push_back(make_pair(*js,*ps)); // if js <= ps, store in results array cout << "Job of size " << result[counter].first << " in partition of size " << result[counter].second << endl; ps = part_size.erase(ps); counter++; break; } else if (ps != part_size.end() ) // if no partition found, keep looping { // through partitions until there are no more. *ps++; //counter++; } else { result.push_back(make_pair(*js,*ps)); // if no partitions, job waits cout << "Job of size " << result[counter].first << " waits." << endl; counter++; *ps++; *js++; break; } } } } ``````

If I use "!done" in place of "ps != part_size.end();" on line 40 everything works fine except if there are more than 5 jobs. That's where the segfault comes in.

 ``123`` ``````for (ps = part_size.begin(); // loop through partitions for jobs !done; )``````

When I use "ps != part_size.end();" I think the loop is ending before it gets to the last "else" on line 60.

Any help is appreciated!
Well, I figured it out. I added a couple if statements to the beginning of the loop instead of at the end.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677`` ``````int main() { vector job_size; // vector to hold job sizes vector part_size; // vector to hold partition sizes vector< pair > result; // 2D vector to hold results int size; int counter = 0; int count = 0; bool finished = false; vector::iterator psize; while (count < 5) // enter 5 partition sizes { cout << "Please enter the size for partition " << count + 1 << ": "; cin >> size; part_size.push_back (size); count++; } while (!finished) // get sizes of jobs { cout << "Please enter the size of a job (9999 to stop): "; cin >> size; if (size != 9999) { job_size.push_back (size); } else{finished=true;} } vector::iterator js; vector::iterator ps; for (js = job_size.begin(); // loop through job sizes js != job_size.end(); js++) { if (part_size.empty()) { result.push_back(make_pair(*js,*ps)); // if no partitions left, job waits. cout << "Job of size " << result[counter].first << " waits." << endl; counter++; *ps++; } for (ps = part_size.begin(); // loop through partitions for jobs ps != part_size.end(); ) { if (*js > *(max_element(part_size.begin(), part_size.end()))) { result.push_back(make_pair(*js,*ps)); // if job size over max partition size, job waits. cout << "Job of size " << result[counter].first << " waits." << endl; counter++; *ps++; break; } if (*js <= *ps) // check if job size <= partition size { result.push_back(make_pair(*js,*ps)); // if js <= ps, store in results array cout << "Job of size " << result[counter].first << " in partition of size " << result[counter].second << endl; ps = part_size.erase(ps); counter++; break; } else if (ps != part_size.end() ) // if no partition found, keep looping { // through partitions until there are no more. *ps++; } } } }``````
Last edited on
Topic archived. No new replies allowed.