Re: why cross out cout make result different?
Please take this to the gcc-help mailing list, as requested. On 3 August 2013 16:34, eric lin wrote: > Hello, I follow your suggestion change from Macro to function, it improved a > little bit, but still not correct result > -- > root@eric-laptop:/home/eric/fundamentalsofdatastructuresincpp/ch7# ./a.out > 26 5 1 37 61 11 59 15 48 19 > bal[1]= 5 > bal[1]= 5 > bal[1]= 5 > bal[1]= 5 > bal[1]= 5 > bal[1]= 5 > 1 5 1 15 15 11 15 15 48 19 > -- > the place I change is > --- > void InterChange(Element *list, const int i, const int j) > { > Element t; > t = list[j]; list[i]=list[j]; list[j]=t; > } > //#define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t; > /*-*/ > I certainly am glad to share a part of gcc job, if someone can give me hint > where I can access Intel assembly language of Celeron chip and gcc 4.6.1's > source code. > E. L. > > > > --- oleg.e...@t-online.de wrote: > > From: Oleg Endo > To: fs...@luxmail.com > Cc: gcc@gcc.gnu.org, gcc-help > Subject: Re: why cross out cout make result different? > Date: Sat, 03 Aug 2013 09:54:03 +0200 > > Hello, > > This mailing list is for the development of GCC, not for using it. > gcc-help might be more appropriate for this kind of question, although > it doesn't seem to be GCC related. > Please do not send any follow ups to gcc@gcc.gnu.org > > On Fri, 2013-08-02 at 18:25 -0700, eric lin wrote: >> >> I have tried to copy QuickSort c++ programs: >> --- >> #include >> using namespace std; >> >> >> class Element >> { >> public: >> int getKey() const { return key;}; >> void setKey(int k) { key=k;}; >> private: >> int key; >> // other fields >> >> }; >> >> #define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t; > > This is probably wrong. In your code it expands to > > if (i < j) > t = list[j]; > > list[i] = list[j]; > list[j] = t; > > Make InterChange a function instead of a macro and try again. > >> /*-*/ >> >> >> void QuickSort(Element list[], /* const */ int left, /*const */ int right) >> // Sort records list[left], ..., list[right] into nondescreasing order on >> field key. >> // Key pivot = list[left].key is arbitrarily chosen as the pivot key. >> Pointer i and j >> // are used to partition the sublist so that at any time list[m].key <= >> pivot, m < i; >> // and list[m].key >= pivot, m>j. It is assumed that list[left].key >> <=list[right+1].key. >> { >> Element t; >> >> if (left> int i = left, >> j=right+1, >> pivot=list[left].getKey(); >> do { >> do i++; while(list[i].getKey() < pivot); >> do j--; while(list[j].getKey() > pivot); >> if (i> } while(i> InterChange(list, left, j); >> >> cout << "---show bankaccount1[0]= " << list[0].getKey() << " >> bankaccount1[1]= " << list[1].getKey() << " bankaccount1[7]= " << >> list[7].getKey() << " its left= " << left << endl; >> QuickSort(list, left, j-1); >> QuickSort(list, j+1, right); >> } >> } >> >> /**/ >> >> int main() { >> Element bankaccount1[10]; >> int l1, r1; >> >> bankaccount1[0].setKey(26); >> bankaccount1[1].setKey(5); >> bankaccount1[2].setKey(37); >> bankaccount1[3].setKey(1); >> bankaccount1[4].setKey(61); >> bankaccount1[5].setKey(11); >> bankaccount1[6].setKey(59); >> bankaccount1[7].setKey(15); >> bankaccount1[8].setKey(48); >> bankaccount1[9].setKey(19); >> l1=0; >> r1=9; >> >> for (int i=0; i<10; i++) >> cout << bankaccount1[i].getKey() << " " ; >> cout << endl; >> >> QuickSort(bankaccount1, l1, r1); >> for (int i=0; i<10; i++) >> cout << bankaccount1[i].getKey() << " " ; >> cout << endl; >> >> return 0; >> } >> /*-*/ >> if I (or you) commnet out cout show bankaccount1 that line, it will show >> different results >> both result s are not what I expected(accroding to books) >> I am in 4.6.1 >> >> _ >> Luxmail.com > > > > > > > _ > Luxmail.com
Re: why cross out cout make result different?
Hello, I follow your suggestion change from Macro to function, it improved a little bit, but still not correct result -- root@eric-laptop:/home/eric/fundamentalsofdatastructuresincpp/ch7# ./a.out 26 5 1 37 61 11 59 15 48 19 bal[1]= 5 bal[1]= 5 bal[1]= 5 bal[1]= 5 bal[1]= 5 bal[1]= 5 1 5 1 15 15 11 15 15 48 19 -- the place I change is --- void InterChange(Element *list, const int i, const int j) { Element t; t = list[j]; list[i]=list[j]; list[j]=t; } //#define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t; /*-*/ I certainly am glad to share a part of gcc job, if someone can give me hint where I can access Intel assembly language of Celeron chip and gcc 4.6.1's source code. E. L. --- oleg.e...@t-online.de wrote: From: Oleg Endo To: fs...@luxmail.com Cc: gcc@gcc.gnu.org, gcc-help Subject: Re: why cross out cout make result different? Date: Sat, 03 Aug 2013 09:54:03 +0200 Hello, This mailing list is for the development of GCC, not for using it. gcc-help might be more appropriate for this kind of question, although it doesn't seem to be GCC related. Please do not send any follow ups to gcc@gcc.gnu.org On Fri, 2013-08-02 at 18:25 -0700, eric lin wrote: > > I have tried to copy QuickSort c++ programs: > --- > #include > using namespace std; > > > class Element > { > public: > int getKey() const { return key;}; > void setKey(int k) { key=k;}; > private: > int key; > // other fields > > }; > > #define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t; This is probably wrong. In your code it expands to if (i < j) t = list[j]; list[i] = list[j]; list[j] = t; Make InterChange a function instead of a macro and try again. > /*-*/ > > > void QuickSort(Element list[], /* const */ int left, /*const */ int right) > // Sort records list[left], ..., list[right] into nondescreasing order on > field key. > // Key pivot = list[left].key is arbitrarily chosen as the pivot key. > Pointer i and j > // are used to partition the sublist so that at any time list[m].key <= > pivot, m < i; > // and list[m].key >= pivot, m>j. It is assumed that list[left].key > <=list[right+1].key. > { > Element t; > > if (left int i = left, > j=right+1, > pivot=list[left].getKey(); > do { > do i++; while(list[i].getKey() < pivot); > do j--; while(list[j].getKey() > pivot); > if (i } while(i InterChange(list, left, j); > > cout << "---show bankaccount1[0]= " << list[0].getKey() << " > bankaccount1[1]= " << list[1].getKey() << " bankaccount1[7]= " << > list[7].getKey() << " its left= " << left << endl; > QuickSort(list, left, j-1); > QuickSort(list, j+1, right); > } > } > > /**/ > > int main() { > Element bankaccount1[10]; > int l1, r1; > > bankaccount1[0].setKey(26); > bankaccount1[1].setKey(5); > bankaccount1[2].setKey(37); > bankaccount1[3].setKey(1); > bankaccount1[4].setKey(61); > bankaccount1[5].setKey(11); > bankaccount1[6].setKey(59); > bankaccount1[7].setKey(15); > bankaccount1[8].setKey(48); > bankaccount1[9].setKey(19); > l1=0; > r1=9; > > for (int i=0; i<10; i++) > cout << bankaccount1[i].getKey() << " " ; > cout << endl; > > QuickSort(bankaccount1, l1, r1); > for (int i=0; i<10; i++) > cout << bankaccount1[i].getKey() << " " ; > cout << endl; > > return 0; > } > /*-*/ > if I (or you) commnet out cout show bankaccount1 that line, it will show > different results > both result s are not what I expected(accroding to books) > I am in 4.6.1 > > _ > Luxmail.com _ Luxmail.com
Re: why cross out cout make result different?
Hello, This mailing list is for the development of GCC, not for using it. gcc-help might be more appropriate for this kind of question, although it doesn't seem to be GCC related. Please do not send any follow ups to gcc@gcc.gnu.org On Fri, 2013-08-02 at 18:25 -0700, eric lin wrote: > > I have tried to copy QuickSort c++ programs: > --- > #include > using namespace std; > > > class Element > { > public: > int getKey() const { return key;}; > void setKey(int k) { key=k;}; > private: > int key; > // other fields > > }; > > #define InterChange(list, i, j) t=list[j]; list[i]=list[j]; list[j]=t; This is probably wrong. In your code it expands to if (i < j) t = list[j]; list[i] = list[j]; list[j] = t; Make InterChange a function instead of a macro and try again. > /*-*/ > > > void QuickSort(Element list[], /* const */ int left, /*const */ int right) > // Sort records list[left], ..., list[right] into nondescreasing order on > field key. > // Key pivot = list[left].key is arbitrarily chosen as the pivot key. > Pointer i and j > // are used to partition the sublist so that at any time list[m].key <= > pivot, m < i; > // and list[m].key >= pivot, m>j. It is assumed that list[left].key > <=list[right+1].key. > { > Element t; > > if (left int i = left, > j=right+1, > pivot=list[left].getKey(); > do { > do i++; while(list[i].getKey() < pivot); > do j--; while(list[j].getKey() > pivot); > if (i } while(i InterChange(list, left, j); > > cout << "---show bankaccount1[0]= " << list[0].getKey() << " > bankaccount1[1]= " << list[1].getKey() << " bankaccount1[7]= " << > list[7].getKey() << " its left= " << left << endl; > QuickSort(list, left, j-1); > QuickSort(list, j+1, right); > } > } > > /**/ > > int main() { > Element bankaccount1[10]; > int l1, r1; > > bankaccount1[0].setKey(26); > bankaccount1[1].setKey(5); > bankaccount1[2].setKey(37); > bankaccount1[3].setKey(1); > bankaccount1[4].setKey(61); > bankaccount1[5].setKey(11); > bankaccount1[6].setKey(59); > bankaccount1[7].setKey(15); > bankaccount1[8].setKey(48); > bankaccount1[9].setKey(19); > l1=0; > r1=9; > > for (int i=0; i<10; i++) > cout << bankaccount1[i].getKey() << " " ; > cout << endl; > > QuickSort(bankaccount1, l1, r1); > for (int i=0; i<10; i++) > cout << bankaccount1[i].getKey() << " " ; > cout << endl; > > return 0; > } > /*-*/ > if I (or you) commnet out cout show bankaccount1 that line, it will show > different results > both result s are not what I expected(accroding to books) > I am in 4.6.1 > > _ > Luxmail.com