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

on
Medium Priority
399 Views
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

## View Solution Only

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.

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

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

Could well be like that ;o)

Commented:
jkr

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

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

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

Commented:
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.
##### Thanks for using Experts Exchange.

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