1

My homework program has to write random numbers for arrival time and burst time into a file. Then after they are written, it reads the file and sorts the contents.

I figured setting up a 2d array would be the easiest way for me to go about this. But I am unsure on how to implement my sort so that if an arrival time swaps places then burst time of that arrival goes along for the ride.

I feel like I worded that poorly, but a basic example would be:

array[3][10] > array[2][23]

So since second array has an earlier arrival time I need both its arrival 2 and its burst 23 to move before array[3][10], but I need this do that and compare 100 inputs.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

const int max = 100;

using namespace std;

int main()
{
    multimap<int [][]> myMap;

    int randomBurst[max];
    int arrivalTime[max];
    int line[max][2];
    int first = 0;

    for (int i = 0; i < 100; i++)
    {
        if (i < 100)
        {
            ofstream write("Schedule.txt", ios::app);
            randomBurst[i] = rand() % 1000;
            arrivalTime[i] = rand() % 1000;
            write << arrivalTime[i] << " " << randomBurst[i] << endl;
        }
    }
    ifstream read("Schedule.txt");
    for (int i = 0; i <= max; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            read >> line[i][j];

            cout << line[i][j] << " " ;
        }
        cout << endl;
    }

    cout << endl;
    cout << endl;
    for (int i = 0; i <= max; i++)
    {
    for (int j = 0; j < 2; j++)
    {

        myMap.insert(pair<int[][]>(line[i][j]);

    }
    cout << endl;

    }
    system("pause");
    return 0;
}

My code sets up my array correctly after it reads the written file content, but I'm kind of lost what I should implement for a sort.

6
  • 3
    Have you considered our good lord and savior. Map? Commented Nov 29, 2015 at 3:16
  • The initialization is off, that't the first thing that I see, when you have created the multimap, you initialized it with some wrong points, ie 2 arrays. That in itself will cause a bad mixup, it already acts somewhat like a map with an a list within it. You've not brought in the map in the include directives above as well, that would cause it to not recognize that structure too. If using a system dependent call, instill it with a #ifdef #define macro, not really needed if you're starting out, but will be needed soon. If i push this off a bit doucheish, I'm a grader so look at things as such. Commented Nov 29, 2015 at 4:11
  • Running the code in a bit to see if there's anything else to be done for it. Commented Nov 29, 2015 at 4:11
  • You're also running into a bit of an issue with the max value you defined, that is one of the functions that are included in the include directives that you've brought in. having a conflict in G++ because it doesn't know if you are speaking about the function or the variable, which is why the namespace thing comes back and bites you more than you think, it's a cstdlib function though so that would'nt save you. Be carefule with that. Commented Nov 29, 2015 at 4:17
  • Updated my answer hope that helps you along a bit, not too sure what you are tring to accomplish though, if you are implementing a new idea it's best to start from scratch, and not mess yourself up by applying new framework to a bad foundation. Commented Nov 29, 2015 at 4:39

1 Answer 1

1

Well coming forward with this, mainly left that comment to be able to find this question faster on my laptop.

Like I said in the comment, if you want a presorted, by key value 2D "array", the quickest manner in which you could do this is with the map container., and if you really need the internal points to be ordered, and you will be using multiple entries within it, lets say entries 2,30 2,12 ... You could either build a map of vectors, or arrays, or use a Multimap. Not too sure of this data structure, as I have never really had a reason to use it as of yet. Referenced here http://www.cplusplus.com/reference/map/multimap/

The above will provide you with the sorting done for you, and the reason why I recommended a vector is the lack of order within it, and not sure if the 'bursts?' are to be ordered as well.

EDIT: Forgot to mention, that a map will not hold more than one key of any given value, so if you are, again, inputting multiple points a above, then you will. if implementing things as you were before, overwrite things.

EDIT: So this is more or less the fix I think I have, but you are working around this in a very indirect manner, that is hard to follow honestly.

    #include <map>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;

const int MAX = 100;
int main()
{
  multimap<int,int> myMap;

  int randomBurst[100];
  int arrivalTime[100];
  int line[100][2];
  int first = 0;

  for (int i = 0; i < 100; i++)
    {
      if (i < 100)
        {
          ofstream write("Schedule.txt", ios::app);
          randomBurst[i] = rand() % 1000;
          arrivalTime[i] = rand() % 1000;
          write << arrivalTime[i] << " " << randomBurst[i] << endl;
        }
    }
  ifstream read("Schedule.txt");
  for (int i = 0; i <= 100; i++)
    {
      for (int j = 0; j < 2; j++)
        {
          read >> line[i][j];

          cout << line[i][j] << " " ;
        }
      cout << endl;
    }
  // cout << endl;                                                              
  // cout << endl;                                                              
  for (int i = 0; i < 100; i++)
    {
      for (int j = 0; j < 2; j++)
        {
          //Attain the value in the index, and the held value within it.        
          myMap.insert(pair<int, int> (line[i][j], line[i][j]));
        }
      cout << endl;

    }
  //    system("pause");                                                        
  return 0;

This fixes the insertion point, just because you give it an array it does not mean that the program will take that as a pair, as the first index is a point to another array in itself. And so on. I recommend starting off wiht a map object instead, as the multimap makes things a bit annoying, if you are familiar with the vector containers then use that instead within the map to log multiple values.

Sign up to request clarification or add additional context in comments.

5 Comments

No, the bursts just need to stay with their arrival time pair. Looking over the maps now, thank you
I'm unsure what is happening but once you've instantiated the object as a multimap, you do not need the std:: flag before it, you should however rarely, if ever, use the using namespace flag. Without context of the rest of the code, not too sure how to proceed with this, maybe ad a bit more as an edit to your original post.
updated the code above, I feel as though I am still going about mapping wrong. I assume I can loop through and map each value, then go about sorting the first part of the array?
Well the best way would be to eliminate the 2d array you are holding, and index the points seperately, as you have in randombursts, and arrival all of the things that are needed, you can just read those points into the map themselves instead of the thing you currently do.
So you would be doing. myMap.insert(pair<int, int> (randomBurst[i], arrivalTime[i])); Wrapped around a for loop, just so, and negating the second loop, the j loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.