Write a function that receives a pointer to an array and its size. The function should remove all prime numbers from the array and return a pointer to the new array. Why does the program display garbage? If you can comment on the errors, please.

#include<iostream> #include <cmath> using namespace std; int prostae_chislo(int n){ int f = 1; if ((n==2)||(n==3)) f=1; else{ for (int i = 2; i < floor(sqrt(n)); i++) if (n % i == 0) { f = 0; break; } } return (f && (n != 1)); } int* proverca(int* A, int size){ int i,j,*B; int k=0; for (i=0; i<size; i++){ if ( prostae_chislo(A[i]) == 1); k++; } B=new int [k]; j=-1; for (i=0; i<size; i++) if (prostae_chislo(A[i]) == 0) B[j++]=A[i]; for(int j = 0; j < k; j++) cout << B[j] << " "; delete [] A; return B; } int main() { setlocale(LC_ALL, "rus"); int size; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива: "; cin >> size; int* A = new int[size]; cout << "Π—Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ массив: "; for(int i = 0; i<size; i++) cin >> A[i]; int* res = proverca(A, size); cout <<" "<< res << endl; delete []A; return 0; } 
  • What does the program display? - Cerbo
  • 2
    One of the errors - you call delete twice for A. Once in the main function, the second - in proverca. At the same time, the memory allocated for In flows, tk is not released anywhere - Malov Vladimir
  • @Cerbo immediately two large eight-digit numbers and four zeros if size = 6 - Asya Filatova
  • j = -1; B [j ++] = A [i]; here you are accessing index -1 for the first time. In addition, prostae_chislo does not work correctly, returns 1 for a value of 4. In general, an error on the error - Malov Vladimir

1 answer 1

Corrected most of the obvious errors. I strongly recommend that you first analyze them and try to write the code more intelligently and accurately.

From gross mistakes:

1) Pay attention to the correctness of the allocation and release of memory

2) Watch for calls to arrays - you are accessing a nonexistent index -1

Rest:

3) The signature of the bool prostae_chislo (int n) function should obviously return a boolean type. Returning int and checking the result for 1 is not obvious.

4) Try to declare variables where you use them. Do not store them all at the beginning of the function.

 #include<iostream> #include <cmath> using namespace std; bool prostae_chislo(int n){ bool result(true); if ((n==2)||(n==3)) result = true; else{ for (int i = 2; i <= floor(sqrt(n)); ++i) if (n % i == 0) { result = false; break; } } return (result && (n != 1)); } int* proverca(int* A, int size){ int count = 0; for (int i = 0; i < size; ++i){ if (!prostae_chislo(A[i])) ++count; } int* B = new int[count]; int j = 0; for (int i = 0; i < size; ++i) if (!prostae_chislo(A[i])) B[j++] = A[i]; for(int j = 0; j < count; j++) cout << B[j] << " "; return B; } int main() { setlocale(LC_ALL, "rus"); int size; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива: "; cin >> size; int* A = new int[size]; cout << "Π—Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ массив: "; for(int i = 0; i<size; i++) cin >> A[i]; int* res = proverca(A, size); delete []A; delete []res; return 0; } 

Result:

 Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива: 5 Π—Π°ΠΏΠΎΠ»Π½ΠΈΡ‚Π΅ массив: 2 3 4 5 6 4 6 
  • TS asked to comment on the errors, you just did the work on it. - Cerbo