I wrote a program, then I found out that I need to use functions and templates, which I did not do. Like what I have not tried, it does not work. So after the day of digging through textbooks and google decided to unsubscribe here.

Actually, I ask you to make the functions:

void PrintMax(Array<int> WA); void PrintMin(Array<int> WA); void QuickSort(int *items, int len); void QS(int *items, int left, int right); 

Were set through templates.

Speaking of ABS (), I don’t know why, but abs () doesn’t work for me, even with #include <math.h>

Code:

 //********************************************************************** // Π—Π°Π΄Π°Π½ΠΈΠ΅: Π’ ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½ΠΎΠΌ массивС (int), считываСмом с ΠΊΠ»Π°Π²ΠΈΠ°Ρ‚ΡƒΡ€Ρ‹, Π½Π°ΠΉΡ‚ΠΈ // наибольший ΠΈ наимСньший элСмСнт, ΠΎΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ массив Ρ‚Π°ΠΊ, // Ρ‡Ρ‚ΠΎΠ±Ρ‹ элСмСнты Ρ€Π°Π²Π½Ρ‹Π΅ Π½ΡƒΠ»ΡŽ оказались Π² ΠΊΠΎΠ½Ρ†Π΅. ΠžΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ // использованиС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ ΠΈ шаблонов. ИспользованиС Π³Π»ΠΎΠ±Π°Π»ΡŒΠ½Ρ‹Ρ… // ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Π½Π΅ допускаСтся. //********************************************************************** #include <iostream> using namespace std; const int DefaultSize=8; //для создания массива Π±Π΅Π· указания Ρ€Π°Π·ΠΌΠ΅Ρ€Π° //Ρ€Π°Π·ΠΌΠ΅Ρ€ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π±Ρ€Π°Π½ автоматичСски //*************************Массив*************************************** template <class T> class Array { public: //конструкторы Array(int size); Array(const Array& rhs); ~Array() {delete [] pType;} //ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ Array& operator = (const Array&); T& operator[] (int offset) { if (offset>=0&&offset<GetSize()) return pType[offset]; throw xBoundary(); return pType[0]; } const T& operator [] (int offset) const { if (offset>=0&&offset<GetSize()) return pType[offset]; throw xBoundary(); return pType[0]; } //друТСствСнная функция для пСрСопрСдСлСния ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° Π²Ρ‹Π²ΠΎΠ΄Π° template <class X> friend ostream& operator<< (ostream&, Array<X>&); //ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ доступа int GetSize() const {return itsSize;} //ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ класса ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ с мноТСствСнным наслСдованиСм class xBoundary {}; //ΠΎΠ±Ρ€Π°Ρ‰Π΅Π½ΠΈΠ΅ ΠΊ элСмСнту Π²Π½Π΅ массива class xSize //ошибки связанныС с Π·Π°Π΄Π°Π½ΠΈΠ΅ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° { public: virtual void PrintError() {cout<<"Size error!\n";} }; class xBig :public xSize { public: virtual void PrintError() {cout<<"Entered size is big!\n";} }; class xSmall :public xSize { public: virtual void PrintError() {cout<<"Entered size is small!\n";} }; class xZero :public xSmall { public: virtual void PrintError() {cout<<"Can not be zero!\n";} }; class xNegative :public xSize { public: virtual void PrintError() {cout<<"Can not be negative!\n";} }; private: T *pType; int itsSize; }; //ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ конструкторов ΠΈΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² шаблона-класса Массив template <class T> ostream& operator << (ostream& output, Array<T>& theArray) { for (int i=0;i<theArray.GetSize();i++) output<< "["<<i<<"]"<<theArray[i]<<endl; return output; } template <class T> Array<T>::Array(int size): itsSize(size) { //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Π½Π° ошибки Ρ€Π°Π·ΠΌΠ΅Ρ€Π° массива if (size==0) throw xZero(); if (size>100) throw xBig(); if (size<1) throw xNegative(); if (size<2) throw xSmall(); pType=new T[size]; for (int i=0;i<itsSize;i++) pType[i]=0; } template <class T> Array<T>::Array(const Array& rhs) { itsSize=rhs.GetSize(); pType=new T[itsSize]; for (int i=0;i<itsSize;i++) pType[i]=rhs[i]; } template <class T> Array<T>& Array<T>::operator = (const Array& rhs) { if (this==&rhs) return *this; delete [] pType; itsSize=rhs.GetSize(); pType=new T[itsSize]; for (int i=0;i<itsSize;i++) pType[i]=rhs[i]; return *this; } //*******************Класс ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ !int****************************** class xType {}; //*******************ΠŸΡ€ΠΎΡ‚ΠΎΡ‚ΠΈΠΏΡ‹ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΉ********************************** void PrintMax(Array<int> WA); void PrintMin(Array<int> WA); void QuickSort(int *items, int len); void QS(int *items, int left, int right); Array<int> ConvertAndSend(Array<int> WA); int ABS(int N); //********************Главная функция*********************************** int main() { int choise; while (true) { cout<<"Please enter array size: "; cin>>choise; cout<<"At now enter array elementh:\n"; try //ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибок { Array<int> workArray(choise); for (int i=0;i<workArray.GetSize();i++) cin>>workArray[i]; PrintMax(workArray); PrintMin(workArray); workArray = ConvertAndSend(workArray); cout<<"Array after sort:\n"<<workArray<<endl; } catch (Array<int>::xBoundary) {cout<<"Boundary error!\n";} catch (Array<int>::xSize& theException) {theException.PrintError();} cout<<"Would do you like work on a new array? y(1)/n(other)\n"; cin>>choise; if (choise!=1) break; } return 0; } void PrintMax(Array<int> WA) { int max=0; for (int i=0;i<WA.GetSize();i++) if (max<WA[i]) max=WA[i]; cout<<"Maximum element of array: "<<max<<endl; } void PrintMin(Array<int> WA) { int min=2147483647; for (int i=0;i<WA.GetSize();i++) if (min>WA[i]) min=WA[i]; cout<<"Minimum element of array: "<<min<<endl; } //моя Π½Π΅ΠΎΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½Π½ΠΎΡΡ‚ΡŒ ΠΈΠ»ΠΈ ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡ‹ взаимопонимания Linux ΠΈ Ρ‡Π΅Π³ΠΎ-Ρ‚ΠΎ int ABS(int N) { if (N>0) return N; else return -N; } //**********************Π“ΠΎΡ€Π΅ ΠΎΡ‚ !ΡƒΠΌΠ°************************************ Array<int> ConvertAndSend(Array<int> WA) { int temp[WA.GetSize()]; for (int i=0;i<WA.GetSize();i++) temp[i]=WA[i]; QuickSort(temp,WA.GetSize()); for (int i=0;i<WA.GetSize();i++) WA[WA.GetSize()-i-1]=temp[i]; return WA; } //************Алгоритм быстрой сортировки ΠΌΠΎΠ΄ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ************** void QuickSort(int items[], int len) { //для ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡ΠΈ Π² ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Ρ€Π°Π· всСго массива QS(items, 0, len); } void QS(int items[], int left, int right) { int i, j, x, y; i = left; j = right; //Π½Π°Ρ…ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ сСрСдины x = items[( left+right) / 2 ] ; //ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ элСмСнтов do { while((ABS(items[i]) < ABS(x)) && (ABS(i) < ABS(right))) i++; while((ABS(x) < ABS(items[j])) && (ABS(j) > ABS(left))) j--; if(i <= j) { y = items[i]; items[i] = items[j]; items[j] = y; i++; j-- ; } } while(i <= j ); //Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ рСкурсивно Ссли массив нСдоотсортирован if(left < j) QS(items, left, j ) ; if(i < right) QS(items, i, right); } 
  • Yes, by the way, the quick sort algorithm is used, if you cannot drive it under the template, then please also inform about it - Tomagavk
  • everything can be driven under the template :-) - gecube

1 answer 1

 //моя Π½Π΅ΠΎΡΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½Π½ΠΎΡΡ‚ΡŒ ΠΈΠ»ΠΈ ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡ‹ взаимопонимания Linux ΠΈ Ρ‡Π΅Π³ΠΎ-Ρ‚ΠΎ int ABS(int N) 

This is because you need to read the help. We do this:

 #include <cmath> ... abs(10); // ΡƒΠΆΠ΅ Π½Π΅ ругаСтся! 

And under the templates simply redo:

 template <class T> void PrintMax(Array<T> WA) // измСнился ΠΏΡ€ΠΎΡ‚ΠΎΡ‚ΠΈΠΏ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΈ Π’Π‘Π•! { int max=0; // Π»ΡƒΡ‡ΡˆΠ΅ - T max = 0; for (int i=0;i<WA.GetSize();i++) if (max<WA[i]) max=WA[i]; cout<<"Maximum element of array: "<<max<<endl; } 

Another point: in C ++ it is more profitable to pass objects by reference. Those. instead

 void PrintMax(Array<T> WA); 

generally need to write

 void PrintMax(Array<T> &WA); 

As in the first case, a temporary object is obtained on the stack, which then automatically crashes down. Why do we need extra copying of data back and forth? If the object does not change in the function, then it is advantageous to specify it with the const modifier. This will allow the compiler to check - and not whether we want to change it inadvertently. Because the transfer by reference is essentially a veiled transfer by the pointer.

 void PrintMax(const Array<T> &WA); 

Another nuance:

 void PrintMin(Array<int> WA) { int min=2147483647; // Π»ΡƒΡ‡ΡˆΠ΅ int min = WA[0]. Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽ ΠΏΠΎΠ΄ΡƒΠΌΠ°Ρ‚ΡŒ ΠΏΠΎΡ‡Π΅ΠΌΡƒ. // Π° Π΅Ρ‰Π΅ Π»ΡƒΡ‡ΡˆΠ΅ ΡΠΎΡ…Ρ€Π°Π½ΡΡ‚ΡŒ индСкс минимального элСмСнта for (int i=0;i<WA.GetSize();i++) if (min>WA[i]) min=WA[i]; cout<<"Minimum element of array: "<<min<<endl; } 

Quick sort:

 //**********************Π“ΠΎΡ€Π΅ ΠΎΡ‚ !ΡƒΠΌΠ°************************************ template <class T> Array<T> ConvertAndSend(Array<T> WA) { //T temp[WA.GetSize()]; //for (int i=0;i<WA.GetSize();i++) temp[i]=WA[i]; QuickSort(WA,WA.GetSize()-1); // Π±Π΅Π· -1 ΠΈΠΌΠ΅Π΅ΠΌ boundary check error //for (int i=0;i<WA.GetSize();i++) WA[WA.GetSize()-i-1]=temp[i]; return WA; } //************Алгоритм быстрой сортировки ΠΌΠΎΠ΄ΠΈΡ„ΠΈΡ†ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ************** template <class T> void QuickSort(Array<T>& items, int len) { //для ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡ΠΈ Π² ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Ρ€Π°Π· всСго массива QS(items, 0, len); } template <class T> void QS(Array<T>& items, int left, int right) { int i, j; // Π° здСсь Π·Π°Ρ‡Π΅ΠΌ Ρ‚ΠΈΠΏΡ‹ ΠΌΠ΅Π½ΡΡ‚ΡŒ? Π­Ρ‚ΠΎ ΠΆ индСксы элСмСнтов, Ρ‚Π°ΠΊ Ρ‡Ρ‚ΠΎ порядок T x, y; i = left; j = right; //Π½Π°Ρ…ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ сСрСдины x = items[( left+right) / 2 ] ; //ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ элСмСнтов do { while((abs(items[i]) < abs(x)) && (abs(i) < abs(right))) i++; while((abs(x) < abs(items[j])) && (abs(j) > abs(left))) j--; if(i <= j) { y = items[i]; items[i] = items[j]; items[j] = y; i++; j-- ; } } while(i <= j ); //Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ рСкурсивно Ссли массив нСдоотсортирован if(left < j) QS(items, left, j ) ; if(i < right) QS(items, i, right); } 

Next yourself.

  • Thank you!) This is a really good answer) - Tomagavk
  • I will correct if you say how)) And I still have a question ... and if to rewrite PrintMin then T min what to equate ?? and how to check that the value of the desired type was entered from the keyboard? when a line is needed, the line is entered and the program ends immediately, I do not catch this moment - Tomagavk
  • one
    Well, look - the program, in general, will actually work for any types for which the operations ">", "<" are defined, and so on. Ie at least for numerical chips (char, int, double, float and their varieties) everything is fine. This is the first. <P> The second is that validation of the input lies with you. If the user entered rubbish, then cin simply does not change the variables that you gave him. - gecube
  • one
    <i> and if to rewrite PrintMin then T min what to equate ?? </ i> <p> Well, I would rewrite the program logic and keep the index of the minimum element. In general, you can use the specialization of templates and for each type you are interested in to make its minimum value. Templates are a very flexible mechanism. - gecube
  • Yes, I figured out the min, I looked through the tutorial and now I continue. I checked it by day, only the problem is that it doesn’t just fill the element with zero, but that it immediately fills the other elements with zero and runs through the program its completion, ignore everything cin plus tell me how you put it)) - Tomagavk