1

だから私はC ++にかなり慣れていないので、このクラスの割り当てがあります。普段は教授に助けを求めるのですが、教授は学生へのメールには答えず、私たちのクラスは週に 1 回です。私は奇妙な結果で何日もこれを機能させようとしたので、ここに問題があります.

プロンプトは次のとおりです。

任意の列を指定して、1 つの列配列を並べ替えたり、文字の 2 次元配列を並べ替えたりします。これが、テンプレート仕様として使用したものです。

彼が使用したクラスは次のとおりです (これを変更することはできません)。

//This class sorts arrays either ascending or descending
template<class T>
class Prob2Sort
{
private:
    int *index;  //Index that is utilized in     the sort
public:
    Prob2Sort(){index=NULL;}; //Constructor
    ~Prob2Sort(){delete []index;};  //Destructor
    T * sortArray(const T*,int,bool); //Sorts a single column array
    T * sortArray(const T*,int,int,int,bool); //Sorts a 2 dimensional array
};

そして、ここにドライバープログラムがあります(これも触れられません):

cout<<"The start of Problem 2, the sorting problem"<<endl;
Prob2Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Problem2.txt",ios::in);
char *ch2=new char[10*16];
char *ch2p=ch2;
while(infile.get(*ch2)){cout<<*ch2;ch2++;}
infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,10,16,column,ascending);
for(int i=0;i<10;i++)
{
    for(int j=0;j<16;j++)
    {
        cout<<zc[i*16+j];
    }
}
delete []zc;
cout<<endl;

そして出力:

この問題からの出力。

問題 2、並べ替え問題の開始
    ルセコエドホフbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Cgldjhcekjigcdn
    Bffmdbkcenlafjk
    Fggdijijegfblln
    Jjlncnimjldfedj
    Amliglfohajcdmm
    Balgfcaelhfkgeb
    Kmlhmhcddfoeilc

列 15 での並べ替え
    Cgldjhcekjigcdn
    Fggdijijegfblln
    Amliglfohajcdmm
    Bffmdbkcenlafjk
    Jjlncnimjldfedj
    ルセコエドホフbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Kmlhmhcddfoeilc
    Balgfcaelhfkgeb

今、私はそれを機能させるためだけにいくつか変更しました。これがmain.cppの私のクレイジーなコードです:

cout<<"The start of Problem 2, the sorting problem"<<endl;
Prob2Sort<char> rc;
bool ascending=true;
ifstream infile;
infile.open("Problem2.txt",ios::in);
char *ch2=new char[(4*17)+ 1];
char *ch2p=ch2;
while(infile.get(*ch2)){ch2++;}

infile.close();
cout<<endl;
cout<<"Sorting on which column"<<endl;
int column;
cin>>column;
char *zc=rc.sortArray(ch2p,4,17,column,ascending);
for(int i=0;i<4;i++)
{
        for(int j=0;j<17;j++)
        {
                cout<<zc[i*17+j];
        }
}
delete []zc;
cout<<endl;

今ここに私のクラスがあります:

template<class T>
T* Prob2Sort<T>::sortArray(const T* arry, int rows, int cols, int column, bool order)
{
    // put into new array to sort and return
    T* a_ray = new T[(rows*cols) + 1];

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
               a_ray[i*cols+j] = arry[i*cols+j];
        }
        cout << endl;
    }

    for(int i=0;i<rows;i++)
    {
        for(int j=0;j<cols;j++)
        {
               cout << a_ray[i*cols+j] ; // displays fine
        }
        cout << endl;
    }

    T temp;
    bool test = true;

    do
    {
        test = false;
        for(int i = 0; i < rows - 1; i++)
        {
            cout << "Row " << i << endl;
            if(a_ray[i*cols+column] > a_ray[(i+1)*cols+column])
            {
                cout << "ELEMENT 1 IS " << a_ray[i*cols+column] << endl;
                temp = arry[i*cols+column];
                cout << "TEMP IS " << temp << endl;
                a_ray[i*cols+column] = a_ray[(i+1)*cols+column];
                cout << "ELEMENT 1 NEW is  " << a_ray[(i)*cols+column] << endl;
                cout << "ELEMENT 2 is " << a_ray[(i+1)*cols+column] << endl;
                a_ray[(i+1)*cols+column] = temp;
                cout << "ELEMENT 2 NEW is  " << a_ray[(i+1)*cols+column] << endl;
                test = true;
             } 

        }      
    }while(test);

今問題は(そして私がすべてに触れる前にこれを持っていた)でした...それを説明する方法がわかりませんが、問題を表示するためにそれらのカウントを追加したので、ソートするために行0を選択した出力を投稿します各要素でその1文字だけをソートするので、教授が望んでいることをまだ試みていません

 // this is the file read in (i also has 2 elements at end of each line for the newline 
 //characters which I can see in a hex editor) 

abcdefghijklmno
ABCDEFGHIJKLMNO
1234567890pqrst
uvwxyzPQRSTUVWX
Row 0
ELEMENT 1 IS a
TEMP IS a
ELEMENT 1 NEW is  A
ELEMENT 2 is A
ELEMENT 2 NEW is  a
Row 1

// **PORBLEM HERE***


ELEMENT 1 IS a
TEMP IS A

//**the two couts above should be the same... as you can see why here,

   cout << "ELEMENT 1 IS " << a_ray[i*cols+column] << endl;
   temp = arry[i*cols+column];
   cout << "TEMP IS " << temp << endl;

//////////////////////////////// should be same value!!!! but it is not
// t only does this every other loop if I remember right. it will not change the vlauebut retain the last one in it

ELEMENT 1 NEW is  1
ELEMENT 2 is 1

ELEMENT 2 NEW is  A

助けてくれてありがとう、私の混乱で申し訳ありませんが、私はこれが本当に必要であり、何が起こっているのか理解できません.このエラーを修正できません

4

0 に答える 0