Thêm lệnh lặp thì bị lỗi không chạy được chương trình

#include <iostream>
#include<windows.h>
#include<bits/stdc++.h>
using namespace std;
void gotoxy(int x,int y){
    static HANDLE h= NULL;
    if(!h) h=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c={x,y};
    SetConsoleCursorPosition(h,c);
}
struct process{
    int id,timexh,timeth;
};
struct sodo{
    int *ten;
    int *moc;
    int sl;
 };
process* input(int &n){
    int i,j;
    process *a,tg;
    a=new process[n];
    cout<<"Nhap so tien trinh:";cin>>n;
    gotoxy(8,1);cout<<"Ten TT";
    gotoxy(18,1);cout<<"Time XH";
    gotoxy(28,1); cout<<"Time TH";
    for(i=0;i<n;i++){
        gotoxy(10,i+2);cout<<"P"<<i+1; a[i].id=i+1;
        gotoxy(20,i+2);cin>>a[i].timexh;
        gotoxy(30,i+2);cin>>a[i].timeth;
     }
    for(i=0;i<n;i++)
    for(j=0;j<n-1;j++)
      if( a[j].timexh>a[j+1].timexh){
          tg=a[j];
          a[j]=a[j+1];
          a[j+1]=tg;
       }
     return a;
}
sodo RR(process *x,int &n)
{   sodo G;
    process *a;
    int i,j,q;
    a=new process[n];
    for(i=0;i<n;i++)  a[i]=x[i];
    int *xh,sopt=0,slxh=0,*vt,*cl;
    xh=new int[n];
    vt=new int;
    cl=new int;
    G.ten=new int;
    G.moc=new int;
    G.sl=0; G.moc[0]=0;
    for(i=0;i<n;i++) xh[i]=0;
    cout<<"Luong tu thoi gian q= "; cin>>q;
    cout<<"Ket qua theo thuat toan RR\n\n";
    for(i=0;i<n;i++)
    if(a[i].timexh==G.moc[0] && xh[i]==0){
        vt=(int*)realloc(vt,(sopt+1)*sizeof(int));
        cl=(int*)realloc(cl,(sopt+1)*sizeof(int));
        vt[sopt]=a[i].id;
        cl[sopt]=a[i].timeth;
        xh[i]=1; sopt++; slxh++;
    }
   while(sopt>0 || slxh<n ){
        G.ten=(int*)realloc(G.ten,(G.sl+1)*sizeof(int));
        G.moc=(int*)realloc(G.moc,(G.sl+2)*sizeof(int));
   if(sopt>0){
       if(cl[sopt-1]<=q){
           G.moc[G.sl+1]=G.moc[G.sl]+cl[sopt-1];
           G.ten[G.sl]=vt[sopt-1];
           sopt--;
       for(i=0;i<n;i++)
       if(a[i].id==G.ten[G.sl]){
           j=i;
           break;
       }
       xh[j]=1;
       }
       else{
           G.moc[G.sl+1]=G.moc[G.sl]+q;
           G.ten[G.sl]=vt[sopt-1];
           for(i=sopt-1;i>0;i--){
                vt[i]=vt[i-1];
                cl[i]=cl[i-1];
                xh[i]=xh[i-1];
            }
           vt[0]=G.ten[G.sl];
           for(i=0;i<n;i++)
           if(a[i].id==G.ten[G.sl]){
                j=i; break;
           }
           a[j].timeth-=q;
           cl[0]=a[j].timeth;
           xh[j]=1;
       }
       G.sl++;
     }
     else{
            for(i=0;i<n;i++)
               if(xh[i]==0){
                   j=i; break;
                }
    vt=(int*)realloc(vt,(sopt+1)*sizeof(int));
    cl=(int*)realloc(cl,(sopt+1)*sizeof(int));
    vt[sopt]=a[j].id;
    cl[sopt]=a[j].timeth;
    G.moc[G.sl+1]=a[j].timexh;
    G.ten[G.sl]=0;
    slxh++; sopt++;
    G.sl++;
    continue;
     }
    for(i=0;i<n;i++)
    if(a[i].timexh<=G.moc[G.sl] && xh[i]==0){
            vt=(int*)realloc(vt,(sopt+1)*sizeof(int));
            cl=(int*)realloc(cl,(sopt+1)*sizeof(int));
            vt[sopt]=a[i].id;
            cl[sopt]=a[i].timeth;
            sopt++; slxh++; xh[i]=1;
    }
    }
    return G;
}
int main(){
    int n;
    process *x;
    x=input(n);
    sodo s;
    s=RR(x,n);
    int i=1;
    while(i<s.sl){
       cout<<"P"<<s.ten[i]<<" ";
       ++i;
    }
    cout<<"\n";
///    int j=1;
  ///  while(j<s.sl){
  ///      cout<<"__|__";
  ///      ++j;
  ///  }
  ///  for(int j=1;i<k;++j)
 ///   cout<<s.sl<<" ";
    int k=1;
    while(k<s.sl){
      cout<<s.moc[k]<<" ";
      ++k;
    }
    return 0;
}

Không hiểu sao em thêm hàm lặp ở cuối thì chương trình lại bị vậy, còn không thêm thì chạy được nhưng lại không đúng được bài toán,anh chị giúp e với


Xem lại mấy đoạn realloc() các giá trị đã lưu chính xác chưa. Dùng try - catch bao các hàm realloc() để bắt ngoại lệ.
Chạy chế độ Gỡ lỗi (Debug).

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?