简体   繁体   中英

Sorting my 2d array in c++

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.

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM