There is a task1() function that returns an array, and passes it to the button, which in turn passes it to the textBox .

Button

 #pragma once #include <ctime> #include <iomanip> namespace Lab { using namespace std; void initArr(int* arrPtr, const int size) { for (int* arrPtrCpy = arrPtr; arrPtrCpy < arrPtr + size; ++arrPtrCpy) { *arrPtrCpy = rand() % 51 - 25; } } /*int printArr(int* arrPtr, const int size) { for (int* arrPtrCpy = arrPtr; arrPtrCpy < arrPtr + size; ++arrPtrCpy) { if (arrPtr + size == arrPtrCpy) { return *arrPtrCpy; } else { printArr(arrPtr, size); } } }*/ bool isElementExists(const int* arrPtr, const int size, const int item) { bool isExists = false; for (const int* arrPtrCpy = arrPtr; arrPtrCpy < arrPtr + size; ++arrPtrCpy) { if (*arrPtrCpy == item) { isExists = true; break; } } return isExists; } bool isElementExists(const int* arrPtr, const int* endArrPtr, const int item) { bool isExists = false; for (const int* arrPtrCpy = arrPtr; arrPtrCpy < endArrPtr; ++arrPtrCpy) { if (*arrPtrCpy == item) { isExists = true; break; } } return isExists; } int conver(int arrPtr, const int size, int &mass) { for (int i=0;i<size;i++) { *mass[i] = *arrPtr[i]-48; } } int task1() { int sizeA = 5; int sizeB = 5; int* arrAPtr = new int[sizeA]; initArr(arrAPtr, sizeA); int* arrBPtr = new int[sizeB]; initArr(arrBPtr, sizeB); //printArr(arrAPtr, sizeA); //printArr(arrBPtr, sizeB); int sizeC = 0; for (int* arrAPtrCpy = arrAPtr; arrAPtrCpy < arrAPtr + sizeA; ++arrAPtrCpy) { if (!isElementExists(arrAPtr, arrAPtrCpy, *arrAPtrCpy) && !isElementExists(arrBPtr, sizeB, *arrAPtrCpy)) { ++sizeC; } } int* arrCPtr = new int[sizeC]; for (int* arrAPtrCpy = arrAPtr, *arrCPtrCpy = arrCPtr; arrAPtrCpy < arrAPtr + sizeA; ++arrAPtrCpy) { if (!isElementExists(arrAPtr, arrAPtrCpy, *arrAPtrCpy) && !isElementExists(arrBPtr, sizeB, *arrAPtrCpy)) { *arrCPtrCpy = *arrAPtrCpy; ++arrCPtrCpy; } } char *mass= new char [sizeC]; delete[] arrAPtr; delete[] arrBPtr; return arrCPtr; delete[] arrCPtr; } } 

At the moment my first array element is displayed.

Question: How can I output the entire array?

Since the courses we have not yet been told what OOP and classes are. I just need a template.

I want the teacher to send homework in a normal form, and not 10 different sourse files.

  • We do not want to watch screenshots, the code is not copied from them. - nick_n_a
  • and where does the knowledge of the PLO? - pavel
  • code - Vlad Kindyushov
  • If I knew, I didn’t ask :) - Vlad Kindyushov
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

In order

First of all:

 return arrCPtr; delete[] arrCPtr; 

In this case, delete does not work, since after return , the function exits.

Secondly:

The return type from the function is int , but in fact you return a pointer return arrCPtr; which is of type int*

Thirdly:

To display all the values ​​of variables in a textBox , you need not just to convert the returned value into a string, but to pre-paste them into one string, for example this way:

 int* array = Lab::task1(); String^ str = gcnew String(""); for(int i = 0; i<arraySize; ++i){ textBoxString += System::Convert::ToString(array[i])+" "; } textBox1->Text = textBoxString; 

arraySize is the length of the array.

  • Yes, I know about the release of memory, I wanted to clarify with the prepad how to release it. And how can I, along with the array, also transfer its size or can sizeof be used? And my compiler swears a picture - Vlad Kindyushov
  • sizeof will not work, because you have a pointer, not the array itself, in this case the sizeof will only return the size of the pointer itself (usually 4 bytes) - Komdosh
  • about the size, you can pass a pointer to the function task1 and put it in the function, it is done like this void func(int* size){*size = 5; } void func(int* size){*size = 5; } and call it in the program so int* size = new int; func(size); //size = 5 int* size = new int; func(size); //size = 5 int* size = new int; func(size); //size = 5 after exiting the function, the pointer will have the value set in the function - Komdosh
  • @ VladKindyushov String corrected - Komdosh
  • Thanks to all of this, the result is the following code: `private: System :: Void button2_Click (System :: Object ^ sender, System :: EventArgs ^ e) {int size = new int; int arra = Lab :: task1 (size); String ^ str = gcnew String (""); for (int i = 0; i <* size; ++ i) {str + = Convert :: ToString (Convert :: ToInt32 (arra [i])) + (Convert :: ToString ("")); } textBox1-> Text = str; ` - Vlad Kindyushov