I have read others posts, but they don't answer my problem fully. I'm learning to delete elements from an array from the book and try to apply that code. As far as I can grasp I'm passing array wrong or it is sending integer by address(didn't know the meaning behind that).
#include <iostream>
#include <cstdlib>
using namespace std;
void delete_element(double x[], int& n, int k);
int main()
{
// example of a function
int mass[10]={1,2,3,45,12,87,100,101,999,999};
int len = 10;
for(int i=0;i<10;i++)
{
cout<<mass[i]<<" ";
};
delete_element(mass[10],10&,4);
for(int i=0;i<10;i++)
cout<<mass[i]<<" ";
return 0;
}
void delete_element(double x[], int& n, int k)
{
if(k<1 || k>n)
{
cout<<"Wrong index of k "<<k<<endl;
exit(1); // end program
}
for(int i = k-1;i<n-1;i++)
x[i]=x[i+1];
n--;
}
intanddoubleare not the same. This code won't compile. Also, it's really not very typical for C++ code.delete_element(mass[10],10&,4);:mass[10]accesses the 11-th element in your 10-sized array which will cause undefined behaviour (segfault at best),10&is half of an expression and won't compilekzero-based? If so, the test is wrong. If it isn't, this goes against how C++ usually refers to elements of an array. In C++, we start at 0, not 1.