i need helping constructing an array of structs, this is my part of my header file which contains the following struct

struct PCB
{
  el_t id; // contains the priority of the process                                                                      
  string state; // current state of the process (e.g running)
  PCB *next;// link to the next process  
};

i need help creating an array of 50 structs PCB and initialize all state to READY, and initialize all el_t id from 1 to 50 for example
id[0] = 1;
id[1]=2;
.
.
id[49]=50;

i never created an array of structs, so any help is appreciated.

Recommended Answers

All 9 Replies

First, consider writing a constructor, like

struct PCB{
	PCB(el_t id_,string state_){
		id=id_;
		state=state_;
	}
};

Second, consider making the state an enum - that would be much better IMO, but it depends on you.

Finally, an array of structs is nothing special, just use operator.() , like:

int main(){
	PCB arr[50];
	for(int i=0;i<49;i++)
		arr[i].id=1;
}

Finally, are you sure you can assign an int to type el_t ?

First, consider writing a constructor, like

struct PCB{
	PCB(el_t id_,string state_){
		id=id_;
		state=state_;
	}
};

Second, consider making the state an enum - that would be much better IMO, but it depends on you.

Finally, an array of structs is nothing special, just use operator.() , like:

int main(){
	PCB arr[50];
	for(int i=0;i<49;i++)
		arr[i].id=1;
}

Finally, are you sure you can assign an int to type el_t ?

thanks, so the constructor would still go in the header file
like this

typedef int el_t;

struct PCB{
	PCB(el_t id_,string state_){
		id=id_;
		state=state_;
	}
};

class LL
{
 protected:
  //Data members                                                                
  int count; // how many nodes do we currently have                             
  PCB *front;//pointer named front                                              
  PCB *rear;// pointer named rear                                               

 public:

  //contructor                                                                  
  LL();
  //destructor                                                                  
  ~LL();                        
  void insertProc(el_t NewNum);                                                           
  void removeHighestProc(el_t& OldNum);                                                              
  bool isEmpty();                                                             
  void displayQueue();
  void sort();                                                                
  void destroy();
  void size();

};


#endif

Constructor is just a normal method, just with no return type, so declare and define it like you would do with any other method (usually declaration in header file and definition in cpp file).

I just wrote it like that to make the code put in the forum as short as possible.
Of course don't forget to declare members of PCB ;)

Constructor is just a normal method, just with no return type, so declare and define it like you would do with any other method (usually declaration in header file and definition in cpp file).

I just wrote it like that to make the code put in the forum as short as possible.
Of course don't forget to declare members of PCB ;)

ok thanks for all the help, just one more question. after im done initializing all elements of the array
how would i use it on my main file

for example i have the following

int main()
{
  /**************TEST1********************************/
  PCB* arr = new PCB[50];
  LL myLL;
  el_t OldNum;

for (int i = 0; i < 49;i++)
    {
      arr[i].id = 1;
    }

 

  myLL.isEmpty();
  myLL.insertProc(); // <- what should i put in here? let say i want to call the first index in the array
  myLL.insertProc(); // <- and here the second index
  myLL.displayQueue(); // so this should display 1 2

}

ok thanks for all the help, just one more question. after im done initializing all elements of the array
how would i use it on my main file

for example i have the following

int main()
{
  /**************TEST1********************************/
  PCB* arr = new PCB[50];
  LL myLL;
  el_t OldNum;

for (int i = 0; i < 49;i++)
    {
      arr[i].id = 1;
    }

 

  myLL.isEmpty();
  myLL.insertProc(); // <- what should i put in here? let say i want to call the first index in the array
  myLL.insertProc(); // <- and here the second index
  myLL.displayQueue(); // so this should display 1 2

}

this is my insertProf function, if that helps

void LL::insertProc(el_t NewNum)
{

  if(rear == NULL)//CASE:1(if rear is null)                          
    {
      front = rear = new PCB;
      rear->id = NewNum;
    }
  else//CASE:2(if the list is not empty)                             
    {
      rear->next = new PCB;
      rear = rear->next;
      rear->id = NewNum;
      rear->next = NULL;

    }
  count++

Seems legit, just one thing: make count static, so that the value is shared across all instances of LL.
Further reading on static members:
http://cplusplus.com/doc/tutorial/classes2/

yeah i will but referring to my other previous post how could i call an index of my PCB array using my insertProc()
function

would it be something like this? im not really familiar with this thats why im asking

insertProc.arr[0];

The thing is, insertProc() accepts argument of type el_t , not PCB.
The way this is now, you would do something like

myLL.insertProc(arr[0].id)

The thing is, insertProc() accepts argument of type el_t , not PCB.
The way this is now, you would do something like

myLL.insertProc(arr[0].id)

thanks man i appreciate all your help. it worked , the thing is is that it initialized all indexes of the array to 1 i think, when it should initizialize them 1 through 50
i tested this
myLL.insertProc(arr[0].id)
myLL.insertProc(arr[1].id)

and it displayed
1 1
and it should of displayed
1 2

, but i can probably figure it out on my own. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.