Ok i have the fallowing problem with this code. Exemple: If i have frame size 4, the first 4 numbers from string are stored correctly into the frame, but when the numbers in frame need to be replaced it replaces only the number which is in last spot of frame, so the algoritm isnt working correctly because of this.

here i read the string from a file and store it into array
Code:
 
                     .
                     . 
                     .
                     . 
            int num_frames= atoi(argv[1]);
            int frames[num_frames];
            int error=0;
            int j, avail,k;
            int n,b,v;
            int index_frame=-1;
            int idx_p=-1;
            int idx_pbest=-1;
            int idx_f=-1;
            

            for(i=0;i<num_frames;i++)
            {
                frames[i]= -1;
            }

            
            for(i=0;i<count;i++)
            {
                avail=0;
               for(k=0;k<num_frames;k++)
                {
                    if(frame[k]==ref_string[i])
                    {
                        printf("found %d: ",ref_string[i]);
                        for(k=0;k<num_frames;k++)
                     {                                      
                            printf(" %s %d","|",frame[k]);
                        }
                         avail=1;
                    }
                }
        
              if (avail==0)
              {
                    for(k=0;k<num_frames; k++)
                    {
                        if(frames[k]==-1)
                        {
                            index_frame=k;
                            break;
                        }
                    }        
                    
                    for(b=0; b<num_frames;b++)    
                    {
                        for(v=count-1; v>= i ;v--)
                        {
                            if(frame[b]==ref_string[v])
                            {
                                idx_p=v;
                            }
                        }

                        if(idx_pbest<idx_p)
                        {
                            idx_pbest=idx_p;
                            idx_f=b;
                        }        
                    }

                    if(index_frame !=-1)
                    {
                        
                        frame[index_frame]=ref_string[i];

                    }
                    else
                    {    
        
                            frames[idx_f]=ref_string[i];
            
                    }

                    
                    printf("insert %d: ",ref_string[i]);
                 error++;
            
                 for(k=0;k<num_frames;k++)
                 {                                      
                    printf(" %s %d","|",frames[k]);
                    }
                }
               printf("\n");
    
            }
           printf("Errors: %d%s",error,"\n");

        }

   return 0;
The algorithm is opt algorithm for page replacement

Ty for you help!