0
int* filtrationBiggerValues(int* values, int nrValues, int givenValue) {
    int j = 0;
    int *new_array=NULL;
    new_array = new int[nrValues];
    for (int i = 0; i < nrValues; i++)
        if (values[i] >= givenValue)
        {
            new_array[j] = values[i];
            j++;
        }
    return new_array;

}



void main() {
    int y[] = { 1,2,100,18,20,94 };
    cout<< filtrationBiggerValues(y, 6, 8)<<"\n";

}

I should see the new array with the values bigger than a certain value, but instead I get its address.

trincot
  • 317,000
  • 35
  • 244
  • 286
Andreea Elena
  • 135
  • 1
  • 8
  • 3
    Possible duplicate of [Is there any way to output the actual array in c++](https://stackoverflow.com/questions/17248462/is-there-any-way-to-output-the-actual-array-in-c) – Yksisarvinen Nov 03 '19 at 13:14
  • 3
    Use `std::array` or `std::vector`, *NOT* a C-style array or manually allocated/managed memory. C++ is not C and we have better options. – Jesper Juhl Nov 03 '19 at 13:14
  • 4
    Prefer `std::vector` over raw owning pointer for array. – Jarod42 Nov 03 '19 at 13:15
  • Hmm. These answers are too specific TBH. Templates are an overkill. This one [Printing an array in C++?](https://stackoverflow.com/questions/1370323/printing-an-array-in-c) deals with printing array in reverse, but the principle is the same really. – Yksisarvinen Nov 03 '19 at 13:17
  • @Yksisarvinen ?? The accepted/best answer is terribly out-dated... not using reverse (const) iterators... – JHBonarius Nov 03 '19 at 13:25
  • @JHBonarius the goal of the accepted answer is to minimize the changes to original code. Of course there is better answers if you want to change the code entirely ;) – Iman Kianrostami Nov 03 '19 at 13:54
  • @ImanKianrostami terrible goal IMHO... if we don't teach people to become better programmers, we'll end up with a generation of bad programmers – JHBonarius Nov 03 '19 at 13:58
  • @JHBonarius It should be done step by step not making them jump to completely different scope. – Iman Kianrostami Nov 03 '19 at 14:00
  • @ImanKianrostami step-by-step is something different then teaching them bad or very outdated habits... If you look at good C++ starters books like "C++ primer" by Lippman, Lajoie, and Moo, they introduce vectors and references way before they introduce pointers. I.e. if learning C++, don't learn C. C and C++ are not the same language... – JHBonarius Nov 03 '19 at 14:12
  • No they are not. But I believe if some one needs a bike for any possible reason you should not make them to ride a Ferrari even if every one or every book says it is better. – Iman Kianrostami Nov 03 '19 at 14:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201788/discussion-between-jhbonarius-and-iman-kianrostami). – JHBonarius Nov 03 '19 at 14:17

3 Answers3

1

That is not how it works. You are returning a pointer from your function not the value. If you want to see the values as output you should iterate on the array and print out each element separately. Also note returning size of your output array from the function for easy iteration.

int* filtrationBiggerValues(int* values, int nrValues, int givenValue, int& outputSize) {
    int j = 0;
    int *new_array=NULL;
    new_array = new int[nrValues];
    for (int i = 0; i < nrValues; i++)
        if (values[i] >= givenValue)
        {
            new_array[j] = values[i];
            j++;
        }
    outputSize = j;
    return new_array;

}

void main() 
{
    int y[] = { 1,2,100,18,20,94 };
    int outputSize = 0;

    int* output = filtrationBiggerValues(y, 6, 8, outputSize);
    for(int i=0; i<outputSize; ++i)
    {
        cout<< output[i] <<"\n";
    }
}

Update (If you want to keep signature of the function as it is)

int* filtrationBiggerValues(int* values, int nrValues, int givenValue) {
    int j = 0;
    int *new_array=NULL;
    new_array = new int[nrValues];
    for (int i = 0; i < nrValues; i++)
        if (values[i] >= givenValue)
        {
            new_array[j] = values[i];
            j++;
        }
    new_array[j] = 0;
    return new_array;

}

void main() 
{
    int y[] = { 1,2,100,18,20,94 };

    int* output = filtrationBiggerValues(y, 6, 8);
    for(int i=0; output[i]>0; ++i)
    {
        cout<< output[i] <<"\n";
    }
}
Iman Kianrostami
  • 482
  • 3
  • 13
  • Is there any option without changing the function signature ? – Andreea Elena Nov 03 '19 at 13:45
  • @AndreeaElena Basically you can not determine when to stop your loop if you don't know the size of the array and you can not find out using a pointer to it. But you can do some trick. For example you can add an out of bond value (for example less than `givenValue`) and break the loop if you encounter this value – Iman Kianrostami Nov 03 '19 at 13:51
  • @AndreeaElena why would you want that? the signature is C-style... is this homework? Are you supposed to use C instead of C++? – JHBonarius Nov 03 '19 at 13:52
0

the name of the array is actually a pointer. if you try this:

  int main() 
   {
        int y[] = { 1,2,100,18,20,94 };
        cout << y <<endl;
        cout<< filtrationBiggerValues(y, 6, 8)<<"\n";
        return 0;
    }

the output is two address

0x7ffd7ac2bc10
0xfadc30
yaodav
  • 1,126
  • 12
  • 34
0

So much to say:

int* new_array = NULL; C++ uses nullptr. I.e. int* new_array = nullptr;

However, you should inlue the initialization you do next line:

int* new_array = new int[nrValues];

But you just created a new object on the heap, that you don't delete. That is called a memory leak.. Nowadays we use unique pointers to help us there.

std::unique_ptr<int[]> new_array = new int[nrValues];

However, in C++ we have STL containers handle all the C-style array stuff for you. As new_array is a different size then values, you probably want to use std::vector, which has a dynamic size. The STL containers have something called iterators, which can go over the element more efficiently. However, the STL containers don't have default output functions, so you'll have to write your own.

#include<vector>
#include<iostream>

std::vector<int> getBiggerThen(std::vector<int> const& input, int givenValue) {
    std::vector<int> output;

    for (auto it = input.cbegin(); it != input.cend(); it++) {
        if (*it > givenValue) {
            output.push_back(*it);
        }
    }
    return output;
}

std::ostream& operator<< (std::ostream& out, std::vector<int> const& vec) {
    for (auto const& el : vec) out << el << " ";
    return out;
}

int main() {
    auto y = std::vector<int>{ 1,2,100,18,20,94 };
    std::cout << getBiggerThen(y, 8) << "\n";
}

oh... also important: in C++ main should always return an int.

Finally, what you are doing is required so often, that the STL library has an built-in algorithm for it. Which can reduce everything to

#include<vector>
#include<algorithm>
#include<iostream>

int main() {
    auto y = std::vector<int>{ 1,2,100,18,20,94 };
    std::vector<int> output{};
    int givenValue = 8;
    std::copy_if(std::cbegin(y), std::cend(y), std::back_inserter(output),
        [givenValue](int val) { return val >= givenValue; });
    for (auto const& el : output) std::cout << el << " ";
    std::cout << "\n";
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41