?
Solved

Arrays

Posted on 2006-03-23
10
Medium Priority
?
367 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.
0
Comment
Question by:cswebdev
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 16273135
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
 
LVL 2

Expert Comment

by:zulti
ID: 16274224
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
 
LVL 86

Expert Comment

by:jkr
ID: 16274259
>>t's look homework to me, so ...

Could well be like that ;o)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:zulti
ID: 16274400
jkr
No teacher will accept an answer with stl, your answer is perfect for non-homework question...  :~]}
0
 
LVL 8

Expert Comment

by:deepu chandran
ID: 16277893
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
 
LVL 8

Expert Comment

by:deepu chandran
ID: 16277907
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
 
LVL 5

Expert Comment

by:gerhardschoeman
ID: 16278520
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
 
LVL 2

Expert Comment

by:zulti
ID: 16278726
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
 
LVL 8

Expert Comment

by:deepu chandran
ID: 16278762
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
 
LVL 2

Expert Comment

by:zulti
ID: 16290346
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question