Link to home
Start Free TrialLog in
Avatar of cswebdev
cswebdev

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zulti
zulti

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).
>>t's look homework to me, so ...

Could well be like that ;o)
jkr
No teacher will accept an answer with stl, your answer is perfect for non-homework question...  :~]}
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


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;
}
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
b.t.w. to find  min and maimum of an array there is a faster and more elegant way than use 2 if statements...
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
 
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.