1

So i need to change Element (a integer) from column which is closest to that column average number. For example:

We have this 2D array array[3][3]

1 4 7

2 5 8

3 6 9

So from first column i would need to change/replace 2 ((1+2+3)/3=2) Because it is closest to average of column

For second column i would need to change/replace 5 ((4+5+6)/3=5)

For third 8

so in final version i would get

1 4 7

3 6 9


I "remove" element just by shortening the lenght of array and pushing numbers per 1 slot, here is how i do it for one-dimensional array

 int search(char A[], int B[], int n, char tp, int dd) //Search for number
{
    int ind = -1;
    for (int i = 0; i < n; i++)
        if ((A[i] == tp) && (B[i] == dd))
            ind = i;
    return ind;
}
void remove(char A[], int B[], int & n, int ind) //remove number from array
{
    for (int i = ind; i < n - 1; i++)
    {
        A[i] = A[i + 1];
        B[i] = B[i + 1];
    }
    n--;

I know how to remove elements in simple 1d array, but cant really come up with way of implementing to remove elements from 2d array

Any help would be appreciated

18
  • 1
    Please show your attempt at solving this task - even if it was unsuccessful. What exactly do you mean by "remove element from an array"? Do you know about std::vector? Commented Nov 18, 2018 at 19:07
  • "We have this 2D array" That's a bunch of numbers, not a 2d array. Note that you can't remove any elements from raw c-style arrays. To do that you'll need a std::vector<std::vector<int>> representation of the 2D array in your code. Commented Nov 18, 2018 at 19:08
  • I see, you have text, that represents (after interpretation) a 2d array. Are you storing the data in some int array[3][3] or some std::vector<std::vector<int>> container? Commented Nov 18, 2018 at 19:08
  • 1
    @Dj_Bear I know how to remove elements in simple 1d array -- How can you do the impossible? You cannot remove elements from an array. You can change elements, but you cannot simply remove elements. Commented Nov 18, 2018 at 19:14
  • 1
    @Dj_Bear -- In your code you posted (please post it here, not on pastebin), you are changing elements, you are not removing elements from the array. Please note the difference between changing and removing. The array is still n items in size. Commented Nov 18, 2018 at 19:19

1 Answer 1

1

The goal is to remove one element in each column.

Therefore, the number of elements remains constant in each column, not necessarly in each row.

A solution is to use a vector of vectors, and to use it to represent the transpose of the matrix. In a matrix M, for each M[i][j] element, it is a pure convention that i represents a row and j a column. Taking the inverse is equivalent to reverse this convention.

So you will only have to remove one element in each vector M[i] representing a column.

It is easy to adapt this method for classical C-style arrays, if one is forced to.

PS: the mentioned convention is not universal. For channel coding for example, the specialists use line-vectors. Then the matrices used are the transpose of what they should be with a classical raw-vector convention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.