可变结果与cout不匹配

时间:2013-06-03 20:19:14

标签: c++ c-strings

所以我对C ++很新,我有这个课程作业。通常情况下,我会向我的教授寻求帮助,但他不回复学生的电子邮件,我们的课程每周一次。我试图让这项工作好几天,结果很奇怪,所以这就是问题所在。

以下是提示:

  

对单个列数组进行排序和/或对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;

输出:

The output from this problem.

The start of Problem 2, the sorting problem
    Lcekoeddhoffbmg
    Lkcmggjcdhhglif
    Cgldjhcekjigcdd
    Cgldjhcekjigcdn
    Bffmdbkcenlafjk
    Fggdijijegfblln
    Jjlncnimjldfedj
    Amliglfohajcdmm
    Balgfcaelhfkgeb
    Kmlhmhcddfoeilc

Sorting on column 15
    Cgldjhcekjigcdn
    Fggdijijegfblln
    Amliglfohajcdmm
    Bffmdbkcenlafjk
    Jjlncnimjldfedj
    Lcekoeddhoffbmg
    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);

现在问题是(我在触及所有内容之前就已经有了这个)...不确定如何解释它,但我添加了那些couts来查看问题所以我会发布输出 我选择第0行进行排序,所以我的方式只对每个元素中的一个字符进行排序,所以我还没有尝试做我教授想做的事情

 // 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

感谢任何帮助过的人,对我的烂摊子感到遗憾,但我真的需要这个,我无法弄清楚发生了什么,甚至我的教授在某些时候能够问他,不能但我们仍然必须把它打开,我只是无法修复此错误

0 个答案:

没有答案