Thread: code.exe has stopped working

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    1

    code.exe has stopped working

    Dear all, I am new here and I am new to work with C/C++. I am posting my code here. it has no error. but showing code.exe has stopped working. I am using code blocks. I will be really grateful if anyone can give any solution. Thanks in advance.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    
    
    using namespace std;
    
    
    const int t=2;//12;
    const int n=6;//195;
    const int l=3;
    const int k=4;//n-l+1//need to change
    int i=1;
    //int j;
    
    
    int *nextvertex(int s[])
    {
        int j;
        if(i<l)
        {
           s[i+1] = 1;
           i=i+1;
           return s;
        }
        else
        {
           for(j=l;j>=1;j--)
           {
               if(s[j]<k)
               {
                   s[j]=s[j]+1;
                   i=j;
                   return s;
               }
            }
        }
        i=0;
        return s;
    }
    
    
    
    
    int *bypass(int s[])
    {   int j;
        for(j=i;j>=1;j--)
        {
            if(s[j]<k)
            {
                s[j]=s[j]+1;
                i=j;
                return s;
            }
        }
        i=0;
        return s;
    }
    int totaldistance(int prefix[], int dna[][n])
    {
    //    int s[5]={1,1,1,1,1};
        int sum=0;
      //int s1=1;
      //s=&s1;
      int ls;
      ls=sizeof(prefix);
      cout<<"ls="<<ls<<"\n";
        int x,y,a,td=0;
        for(x=0;x<t;x++)
        {   int dist=0;
            for(y=0;y<n-ls+1;y++)
            {
               int a=0, c=0;
               for(int z=y;z<y+ls;z++)
               {
                    int w=dna[x][z];
                    int b=prefix[a];
                    if(dna[x][z]!=prefix[a])
                    {
                        c+=1;
                    }
                    a++;
              /*if(s[0]!=dna[x][y])
                {
                    a++;
                }
                cout<<a<<" ";
                td=td+a;*/
               }
               if(c<dist||y==0)
               {
                   dist=c;
               }
            }
            sum=sum+dist;
        }
        cout<<"\n";
        cout<<"total distance:"<<sum<<"\n";
        return sum;
    
    
    }
    
    
    int *branchandboundmediansearch(int dna[][n])
    {
       int s1=1;
        int *s=new int [l];
        s=&s1;
       // int *s=new int [l];
        int *prefix=new int [i];
       // prefix=new int[i];
    
    
        int word[8];
        int bestword[8];
        int bestdis=16;//96;
        int j, optdis, x, y;
        while(i>0)
        {
            if(i<l)
    
    
            {
                for(x=0;x<i;x++)
                {
                    prefix[x]=s[x];
                    cout<<"prefix:"<<prefix[x];
                }
                cout<<"\n";
                optdis= totaldistance(prefix,dna);
                if(optdis>bestdis)
                {
                    s=bypass(s);
                }
                else
                {
                    s=nextvertex(s);
                }
    
    
            }
            else
            {
                for(x=0;x<l;x++)
                {
                    word[x]=s[x];
                }
                if(totaldistance(word,dna)<bestdis)
                {
                    bestdis=totaldistance(word,dna);
                    for(x=0;x<l;x++)
                    {
                        bestword[x]=word[x];
                    }
                }
                s=nextvertex(s);
            }
        }
        return bestword;
        delete [] s;
        delete [] prefix;
    }
    
    
    main()
    {
        int x,y,opd;
        string str;
        int arr[12], dna[t][n];
       // int *l_mer1[8];
       int *l_mer;
       // int *l_mer=l_mer1;
       // char finall_mer1[8];
       // char finall_mer=finall_mer1;
        //char *finall_mer=finall_mer1;
       // int s1=1;
        //int *s=new int [l];
       //=new int[l];
       // s=&s1;
      /*  for(x=0;x<l;x++)
        {
            cout<<"s:"<<s[x]<<" ";
        }
        cout<<"\n";*/
       //out<<s;
       //nt *s2;
       char finall_mer[l];
      //2=nextvertex(s);
     //cout<<s2;
      /* ifstream myfile ("E:/Codeblocks codes/C++ codes/bbms/input.txt");
        if (myfile.is_open())
        {
            while ( !myfile.eof() )
            {
                myfile>>str;
                cout << str << "\n";
            }
            myfile.close();
        }*/
    
    
        cin>>str;
       // int s[11]={1,1,1,1,1,1,1,1,1,1,1};
       // cout<<str;
        //strcpy(str1,str);
        for(x=0;x<12;x++)
        {
            if(str[x]=='A')
            {
                arr[x]=1;
                //cout<<arr[i]<<" ";
            }
            else if(str[x]=='C')
            {
                arr[x]=2;
                //cout<<arr[i]<<" ";
            }
            else if(str[x]=='G')
            {
                arr[x]=3;
                //cout<<arr[i]<<" ";
            }
            else
            {
                arr[x]=4;
                //cout<<arr[i]<<" ";
            }
        }
       cout<<"\n ";
        for(x=0;x<12;x++)
        {
            cout<<arr[x];
        }
        cout<<"\n";
        int a=0;
        while(a<12)//a: for counting 1D array
        {
            for(x=0;x<t;x++)
            {
                for(y=0;y<n;y++)
                {
                    dna[x][y]=arr[a];
                    a++;
                }
            }
        }
        for(x=0;x<t;x++)
            {
                for(y=0;y<n;y++)
                {
                    cout<<dna[x][y]<<" ";
                    //a++;
                }
                cout<<"\n";
            }
    
    
    
    
    //   opd=totaldistance(s,dna);
       //cout<<"\n"<<opd;
      //s=nextvertex(s);
      //cout<<s;
        l_mer=branchandboundmediansearch(dna);
        for(x=0;x<l;x++)
        {
            if(l_mer[x]==1)
            {
                finall_mer[x]='A';
                cout<<finall_mer[x]<<"\n";
                //cout<<arr[i]<<" ";
            }
            else if(l_mer[x]==2)
            {
                finall_mer[x]='C';
                cout<<finall_mer[x]<<"\n";
                //cout<<arr[i]<<" ";
            }
            else if(l_mer[x]==3)
            {
                finall_mer[x]='G';
                cout<<finall_mer[x]<<"\n";
                //cout<<arr[i]<<" ";
            }
            else
            {
                finall_mer[x]='T';
                cout<<finall_mer[x]<<"\n";
                //cout<<arr[i]<<" ";
            }
        }
       // cout<<"\n"<<finall_mer;*/
    
    
    //   delete [] prefix;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In function branchandboundmediansearch() you return a pointer to a local variable this is NOT safe to do!

    Edit: Statements after a return is executable are NOT executed.

    Code:
        return bestword;
        delete [] s;
        delete [] prefix;
    }
    Edit3: I suggest getting rid of the global var "i"; if you refuse to get rid of it, at least give it a meaningful name!

    Tim S.
    Last edited by stahta01; 02-22-2014 at 07:52 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The compiler stopped working
    By akif13 in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2013, 07:08 PM
  2. Replies: 2
    Last Post: 12-23-2012, 04:52 AM
  3. ****.exe stopped working (Second project i have)
    By asediugeneral in forum C Programming
    Replies: 3
    Last Post: 09-03-2012, 01:25 PM
  4. ****.exe has stopped working
    By kawaikx15 in forum C Programming
    Replies: 10
    Last Post: 11-19-2011, 07:38 AM
  5. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 12:58 PM

Tags for this Thread