I have this little problem....

I have a dynamic array.... I have to creat a function to give me the max and min values of that array... I can't think of a way to send data to a function without knowing the number variables.... can anyone give me a nudge in the right direction.... thanks in advance

Dani AI

Generated

Short diagnosis and a few practical fixes.

The strange output in 's second program is exactly what warned about: when you pass a C-style array to a function it decays to a pointer to the first element, so inside the function the parameter is just a pointer and sizeof gives the pointer size, not the element count. Either pass the length explicitly or use a container that records its own size. (cppreference.com)

A concise, modern C++ solution is to use std::vector and the algorithms in <algorithm>. std::vector manages memory for you; std::minmax_element returns both min and max in one call. Example:

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

int main() {
    std::vector<int> v = {3, 1, 4, 1, 5};
    auto mm = std::minmax_element(v.begin(), v.end());
    std::cout << "min=" << *mm.first << " max=" << *mm.second << '\n';
}

This avoids manual new[]/delete[] and is clear to read. (cppreference.com)

If you must use raw arrays (legacy APIs, C interop), use a function signature that takes a pointer plus a size (for example int find_max(const int* arr, std::size_t n)), and be disciplined about ownership. Better still, use RAII wrappers such as std::unique_ptr<T[]> or std::vector to prevent leaks and handle exceptions safely. (cppreference.com)

Style notes tied to this thread: avoid naming variables vector/string (confusing), avoid reinventing max/min when standard algorithms exist, and if using new[] remember delete[] as pointed out. Experimenting is fine — it’s how bugs reveal important lessons, as observed.

Recommended Answers

All 11 Replies

I don't think you can do it without passing the size of the array to the function since once inside the function you can't use the sizeof operator on the passed arrray since it would only yield the size of pointer.

Every sorting or searching routine out there requires you to pass the length of the array to it.

thanks....

Oki,i dind't really understand what u are trying to do , but here is what i 've come with:

#include <iostream>

using namespace std;

int max(int vector[],int size);

int main()
{

    int *p=NULL;
    int n,i;

    cout<<"Array size :";
    cin>>n;
    
    p=new int[n];
    
    for(i=0;i<n;i++)
    {
        cout<<"p["<<i<<"]= ";
        cin>>p[i];
    }

    cout<<"The maximum value is : "<<max(p,n)<<endl;

    return 0;
}

int max(int vector[],int size)
{
    int max=vector[0],i;

    for(i=1;i<size;i++)
        if(vector[i]>max)
            max=vector[i];

return max;
}

Now, i've tried to make the same program ,only that the size of array isn't passed to the function instead i use sizeof like ~s.o.s~ suggested

The only thing is that doesn't work , and i don't know why

#include <iostream>

using namespace std;

int max(int vector[]);

int main()
{

    int *p=NULL;
    int n,i;

    cout<<"Array size :";
    cin>>n;
    
    p=new int[n];
    
    for(i=0;i<n;i++)
    {
        cout<<"p["<<i<<"]= ";
        cin>>p[i];
    }

    cout<<"The maximum value is : "<<max(p)<<endl;

    return 0;
}

int max(int vector[])
{
    int max=vector[0],i;

    for(i=1;i< sizeof vector / sizeof vector[0];i++)
        if(vector[i]>max)
            max=vector[i];

return max;
}

The program compiles succesfully , but the output is wrong
If some can take a look at and see what's wrong , pls .I'm really curios what the bug is

Good luck

commented: 10x for your help :D +1

Oki,i dind't really understand what u are trying to do

He wanted to know whether it was necessary to pass the size of array to the function which calculates the maximum of all the elements of the array.

Instead i use sizeof like ~s.o.s~ suggested

I didn't suggest him to use sizeof since it wouldnt work.;)

The program compiles succesfully , but the output is wrong
If some can take a look at and see what's wrong , pls .

Simple....arrays in C and C++ are passed by reference, meaning instead of passing the entire array like normal variables, only the pointer to the array is passed. Hence when you try to use sizeof operator inside the function, you end up using sizeof for the pointer, which yields the pointer size on that machine.

In short, you can't as such find the size of the array once it has been passed to the function.

Here is a sample run which I got after putting some output stmts to make matter more clear:

Array size :4
p[0]= 1
p[1]= 2
p[2]= 3
p[3]= 4
Sizeof vector = 4 and sizeof vector[0] is 4
The maximum value is : 1

Hope you catch the drift...


I'm really curios what the bug is

I like that kind of approach....

Thank you guys for your time :) it was really helpfull but my question was answered by sos's 1st post....

now I am thinking of moving the date from the array to a vector and then getting the max/min .... as this should work :)

@ ~s.o.s~ Thanks,very nice explained , I understand now .
U got my vote

Member Avatar for Member #46692

@ Eko, you need to slow your little self down a bit, there are a many things you be doing wrong. :lol:

1. Using new without delete[]
2. Naming your own variables with names that should only be reserved for STL functions such as std::string and std::vector is generally a bad idea.

There might be more.

@ Eko, you need to slow your little self down a bit, there are a many things you be doing wrong. :lol:

1. Using new without delete[]
2. Naming your own variables with names that should only be reserved for STL functions such as std::string and std::vector is generally a bad idea.

There might be more.

1.You are learning by making mistakes :lol:
2.Because the program isn't that big, i let the operating system to free the memory when the program finishes(oki ,onest i forget to include delete):lol:
I'll try to remember next time.
3.string,vector aren't keywords,so I can use them without a problem

Member Avatar for Member #46692

string,vector aren't keywords,so I can use them without a problem

Still a bad idea though.

You are learning by making mistakes

You mean you are learning by making a lot of mistakes. :cheesy:

Still a bad idea though.

Agreed -- it turns out to be confusing.

You mean you are learning by making a lot of mistakes.

Yes people learn from their mistakes. Only when you experiment, make mistakes will you know that what not to do.

But maybe you never made mistake, so its all peace. :cheesy:

You cant progress without experimenting... you cant experiment without making mistakes... so this is just part of the path.... btw a great deal of findings were by accident coz someone made a mistake.... anti-biotics... came into existance coz one dude droped yeast into a bacteria culture....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.