We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Arrays

cswebdev
cswebdev asked
on
Medium Priority
399 Views
Last Modified: 2010-04-01
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.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
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();
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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).
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>t's look homework to me, so ...

Could well be like that ;o)

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

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

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.