Arrays

I am new to arrays in C++:

I would like a function Range that receives an array of reals and the number of elements stored in the array, and returns the range of values stored in the array; that is, the difference between the largest value and the smallest value stored in the array.

Array must be one dimensional.
cswebdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You could use

#include <valarray>
using std::valarray;

double Range(double arr[], size_t elements) {

valarray<double> va(arr, elements);

return va.max() - va.min();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zultiCommented:
it's look homework to me, so ...
please post something, and we'll help.

One though, even without a penny, use a for loop to go throu all the elements and finding the maximum and minimum in the array... (maybe I talked too much).
0
jkrCommented:
>>t's look homework to me, so ...

Could well be like that ;o)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

zultiCommented:
jkr
No teacher will accept an answer with stl, your answer is perfect for non-homework question...  :~]}
0
deepu chandranCommented:
hi friend,
   
here is the function for finding the range

double Range(double arr[],int noarg)
{
    double min=arr[0],max=arr[0],range=0;
    for(int i=1;i<noarg;i++)
    {
       min=(min<arr[1])?min:arr[1];
       max=(max>arr[1])?max:arr[1];
    }
    range=max-min;
    return range;
}

i think this is the best method to find range


0
deepu chandranCommented:
double Range(double arr[],int noarg)
{
    double min=arr[0],max=arr[0],range=0;
    for(int i=1;i<noarg;i++)
    {
       min=(min<arr[i])?min:arr[i];
       max=(max>arr[i])?max:arr[i];
    }
    range=max-min;
    return range;
}
0
gerhardschoemanCommented:
deepudeepam, you are obviously a new EE user. On EE you don't just post code for homework questions. It doesn't help anybody.

regards
0
zultiCommented:
b.t.w. to find  min and maimum of an array there is a faster and more elegant way than use 2 if statements...
0
deepu chandranCommented:
sorry friends,

  i am new to ee so pls forgive me.
i know that another method using if statements.
but using  ? : is more understandable i think
 
0
zultiCommented:
No I ment the recursive way, that will find both min and max in N comaprisons, and not 2N.
the ? and if stetement produce the same assembly code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.