2

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--;
} 
John Odom
  • 1,189
  • 2
  • 20
  • 35
Peter
  • 89
  • 1
  • 11
  • 1
    Note that `int` and `double` are not the same. This code won't compile. Also, it's really not very typical for C++ code. – unwind Apr 06 '16 at 14:58
  • 2
    delete_element(mass, len, ?); usually you should send the variables and not numbers – Khalil Khalaf Apr 06 '16 at 15:00
  • 3
    you cannot "delete" an element from an array you can only move the values of them around. as for the line `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 compile – BeyelerStudios Apr 06 '16 at 15:02
  • x - is primary array n - array length k- element to delete (actually remove as is not actually possible to flat out to delete element as i'm understanding from other posts) – Peter Apr 06 '16 at 15:05
  • @Peter -- Is `k` zero-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. – PaulMcKenzie Apr 06 '16 at 15:39
  • actually k can be any number the function handles if it negative or higher than number of elements. – Peter Apr 06 '16 at 15:43
  • @Peter But look at your test. You are testing if `k < 1`, when `k == 0` is a perfectly valid index. – PaulMcKenzie Apr 06 '16 at 15:47
  • Yes it is accustomed to normal counting comparing to computer counting, but there is no mistike as it is adjusted in for loop k-1, n-1 – Peter Apr 06 '16 at 15:50

5 Answers5

1

Line 15: syntax error you can't pass a number& If you want to pass by reference, you need to create a variable first, like:

your delete_element function signature conflicts with your declared arrays. Either use a double array or int array and make sure the signatures match.

delete_element(mass, len , 4);

when you write the name of an array without the brackets, then it's the same as &mass[0] ie. pointer to the first element.

complete changes should be:

#include <iostream>
#include <cstdlib>
using namespace std;


void delete_element(int 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] << " "; };
    cout << endl;

    delete_element(mass, len , 4);

    for (int i = 0; i<10; i++)cout << mass[i] << " ";
    cout << endl;

    cin.ignore();
    return 0;
}

void delete_element(int 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--;
}
And Wan
  • 314
  • 1
  • 3
  • 12
1

There are a couple of errors in your code. I highlight some of the major issues in question 1-3:

  1. You call exit, which does not provide proper cleanup of any objects since it's inherited from C. This isn't such a big deal in this program but it will become one.

    One proper way too handle such an error is by throwing an exception
    cout<<"Wrong index of k "<< k <<endl; exit(1);
    Should be something like this:
    throw std::runtime_error("invalid index");
    and should be handled somewhere else.
  2. You declare function parameters as taking a int& but you call the function like this: delete_element(mass[10],10&,4); 10& is passing the address of 10. Simply pass the value 10 instead.
  3. You are "deleting" a function from a raw C array. This inherently doesn't make sense. You can't actually delete part of such an array. It is of constant compile time size created on the stack. The function itself doesn't do any deleting, try to name the functions something more task-oriented.
  4. You are using C-Arrays. Don't do this unless you have a very good reason. Use std::array or std::vector. These containers know their own size, and vector manages it's own memory and can be re sized with minimal effort. With containers you also have access to the full scope of the STL because of their iterator support.

I suggest you rewrite the code, implementing some type of STL container

Nowhere Man
  • 475
  • 3
  • 9
0

There are a couple of mistakes in your program. Apart from some syntax issues you are trying to pass an int array to a function which wants a double array.

You cannot pass a lvalue reference of a int literal. What you want is to pass a reference to the length of the int array. see also http://en.cppreference.com/w/cpp/language/reference.

Here is an updated version of your program.

#include <iostream>
#include <cstdlib>

using namespace std;

void delete_element(int 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 < len;i++)  
    cout << mass[i] << " "; ;
  cout << endl;

  delete_element(mass, len, 4);

  for (int i = 0;i < len;i++) // len is 9 now
    cout << mass[i] << " ";
  cout << endl;

  return 0;
}

void delete_element(int 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--;
}
Lukas
  • 1,320
  • 12
  • 20
0

Although it does not answer your question directly, I would like to show you how you can use C++ to solve your problem in a simpler way.

#include <vector>
#include <iostream>

void delete_element(std::vector<int>& v, const unsigned i)
{
    if (i < v.size())
        v.erase(v.begin() + i);
    else
        std::cout << "Index " << i << " out of bounds" << std::endl;
}

int main()
{
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
    delete_element(v, 4);

    for (int i : v)
        std::cout << i << std::endl;

    return 0;
}
Chiel
  • 6,006
  • 2
  • 32
  • 57
  • @BeyelerStudios. That is of course a good argument, but I am very much support to make new programmers immediately aware of the fact that they are working with C++ rather than C. But you are right, I have edit my answer to satisfy your comment. – Chiel Apr 06 '16 at 15:15
0

You cannot delete elements from an array, since an array's size is fixed. Given this, the implementation of delete_element can be done with just a single call to the appropriate algorithm function std::copy.

In addition, I highly suggest you make the element to delete a 0-based value, and not 1-based.

Another note: don't call exit() in the middle of a function call.

#include <algorithm>
//...
void delete_element(int x[], int& n, int k) 
{
    if (k < 0 || k > n-1 )
    {
       cout << "Wrong index of k " << k << endl;
       return;
    }

    std::copy(x + k + 1, x + n, x + k);
    n--;
}

Live Example removing first element

The std::copy call moves the elements from the source range (defined by the element after k and the last item (denoted by n)) to the destination range (the element at k). Since the destination is not within the source range, the std::copy call works correctly.

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45