Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays

Status
Not open for further replies.

robert693

Programmer
Jun 20, 2001
40
US
I am creating a C project that simulates CPU Scheduler algorithms. The algorithims that are simulated are Round Robin, Shortest job first, Priority, and first come first serve. I have a simulation of 55 jobs that come in. I have randomly created numbers for the burst, quantum time, and priority of each job. I have created arrays that store the numbers and display each next to each job. I need to create arrays that will take in each job and its corresponding burst, priority, and quantum time numbers together. Right now each are stored in different arrays. I need the numbers to be in an array together so they will read out in a way that I can perform the necessary algorithms for each job. this is what I hav so far:#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip.h>




int main()
{
int burst[55], priority[55], quantum[55];
int i, j, k, l;
int job[55] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55};

cout<<&quot;Job # Burst Priority Quantum&quot;<<endl;

//Generate random numbers.

srand ( (unsigned int)time ( 0 ) ); //Sets random function seed to the value of the internal clock.

for ( i=0; i< 55; i++ )
burst = rand()%21;

for ( j=0; j< 55; j++ )
priority[j] = rand()%16;

for ( k=0; k< 55; k++ )
quantum[k] = rand()%16;

for ( l=0; l< 55; l++ )
cout<<job[l]<<setw(10)<<burst[l] <<setw(10)<<priority[l]<<setw(10)<<quantum[l]<<endl;



return 0;
}

Any help would be greatly appreciated!!
 
if the indexs correspond across all the arrays, do you really need to combine them? or can you just use the same index to access each item out of the the various arrays?

if you really have to have them all together, and since they are all integers, you can have a three dimensional array. use the index to specify the job number instead of maintaining another array w/ the exact sequential information as the index. the only thing you might want to do is bump up your index by one so that 0 is not a job number. either maintain the index as index+1 or specify an array of 56 and leave 0 blank.

[ponytails2]
 
This sort of set-up lends itself to a double-scripted array. If you use one, you can have 3 entries for each of the 55 jobs in a single array.

Here's how it works. If you have a 3 x 2 array initialized to 0s, it would look like this:

const int rows = 3, columns = 2;
int array [rows][columns] = { 0 };

If you want to print the values, you can have for loops traverse the elements of an array like this:

for( int currentRow = 0; currentRow < rows; currentRow++ )
{
for ( int currentColumn = 0; currentColumn < columns; currentColumn++ )
cout << array[currentRow][currentColumn] << ' ';
cout << '\n';
}


Similarly, you can have for loops traverse the array elements to get numbers for calculations ( and to assign values so you don't have to write them all out seperately as in your job array).

Tell me if I can explain further.
 
Would it be easier in a case such as this to declare a class to hold the information and then store an array of those objects?

[tt]class CScheduler
{
public:
CScheduler();
virtual CScheduler() {};

void PrintJob(void); // implement for output

void Sort(int method); // a 'sorting' function
// which defines hot to sort
// by method chosen, ie.
// sort by priority or whatever
// (I didn't think a basic
// comparison operator would
// be appropriate for this job!)

// ... other members here to do something...


int burst;
int priority;
int quantum;
};

// constructor
CScheduler::CScheduler()
{
// initilize member ints with random number here
}
[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top