decrementing array

1
2
3
4
5
6
struct Process
{
int id;
STATE myState;
Process *next;
};


i have the following function which should decrement all the current values of int by 1

for example if the array has the following {2 3 5}
after calling the function below it should change the array to {1 2 4}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void LL::decrement(int NewNum)
{
int id;
int temp;
int temp2;
Process *x = front;
if(x->id != 1)
{
while(x!= NULL)
    {
      x->id = temp; // puts the current id to temp
      temp2 = temp-1; //temp2 gets decremented by 1
      temp2 = NewNum; 
      x = x->next;// gets next value                                           
    }

}

}

but after i call it i just get {0 0 0 }

any help on how to fix this?
Last edited on
The logic of this code seems wrong for what you are trying to do...
1
2
3
4
5
6
7
8

      x->id = temp;   // puts the current id to temp --- temp is a unknown value when it hits here. 
                              // it isn't passed in and it isn't set to some value in the function.
      
      temp2 = temp-1; //temp2 gets decremented by 1
                                  // temp2 will always be temp -1, which I expect temp to be 0 or some random value
      temp2 = NewNum;  // after the above value is set for temp2 you reset it to New Num which is passed in.
      x = x->next;// gets next value                                            


I don't know if that is going to point you in a direction or not. I don't know if id, temp, and temp2 are suppose to be members of the LL class or not or are suppose to be local to this function.

I guess I would expect this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct Process
{
     int id; // id for the class.. this wouldn't change
     STATE myState;
     double temp; // the stored temp.
     Process *next;
};

// in the loop.
void LL:: decrement(int NewNum)
{
     Process *x = front;

     while(x!=NULL)
     {
           if(x->id != 1) // assuming this is a special state of some form.
           {
                 x->temp-=1;
           }
      }
}


temp and temp2 are just local to this function
this is part of my main file
1
2
3
4
5
q1.insert(arr[4].id) // when i call this function it inserts 5 to the id in the array Process, 
q1.insert(arr[5].id)// when i call this function it outputs 6 to the id in the array Process
q1.display() // this displays the queue
q1.decrement() // this should decrement all values of id by 1
q1.display() // this should display the queue after the values of id have decreased by one 


//This should be my display
5 6

4 5
Last edited on
Given what you have have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void LL:: decrement(int NewNum)
{
     Process *x = front;

     while(x!=NULL)
     {
           x->id-=1; // this would decrement the id by one.
           // if I needed to preform a bounds check I would do it here.
           // like if the id fell to 0;
           x = x->next;
      }
      // this still leaves NewNum unused here.
      // and I still haven't used temp and temp2, which have no purpose in what you are asking for.
}
thanks it worked, i just been moving stuff around a lot
Topic archived. No new replies allowed.