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

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.

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

Comments
10x for your help :D

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 :)

@ 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

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....

This article has been dead for over six months. Start a new discussion instead.