hlao

Mar 15, 2008 at 1:28am
can any one give an example of a programm usess output from a file to find maximum, minimum, average and variance?
i really cant find a solution for it.
The question is

Write a program that uses a function template called min, max, ave and var to determine the minimum, maximum, average and variance of the numbers as shown as below:
24
101
1001
55
69
25
14
23
75
885
3548
5
4
65
69
99
You must use the external input file as the input in your program in order to calculate the output of minimum, maximum, average and variance of these given numbers.
Mar 15, 2008 at 2:35am
Tackle this one portion at a time.

First, see if you can open the file and get a number from it.

Then, see if you can open the file and get all the numbers from it, in order.

Next, see if you can update some variable with the value of those numbers - like, a total.

Finally, think about how you would get the average of a set of numbers.
Mar 15, 2008 at 2:48am
An easy way to do this is to make a temporary storage for all the numbers. Then pass the storage to the function that takes in generics.

Since you are not given any function prototypes, then I guess you are free to use any collection class and/or data structures.
Mar 15, 2008 at 3:04am
thanks ya. i'll try first
Mar 15, 2008 at 3:27am
i have try a bit before posting this. can any one help me?

#include <iostream>
#include <fstream>
using namespace std;

double max (ifstream&);
double min (ifstream&);
float ave (ifstream);
double var (ifstream&);

int main()
{
ifstream file ("assignment4.1.txt");
int i;

for (i=1;i<17;i++)
{
max (file);
min (file);
ave (file);
var (file);
}
cout<<max<<endl<<min<<endl<<ave<<endl<<var<<endl;

return 0;
}

double max(ifstream& readfile)
{
double w;

static double max=-1e50;
readfile>>w;
max=(w>max)?w:max;
return (max);

}

double min(ifstream& seefile)
{
double x;

static double min=50;
seefile>>x;
min=(x<min)?x:min;
return (min);
}

float ave(ifstream callfile)
{
float y;

static float ave=0.0;

callfile>>y;
ave+=y;
return (ave);
}




it is not complete yet. but i have try to commmpile but unable.
appreciate for your help. thanks
Mar 15, 2008 at 4:49am
Using that solution does not solve the problem.

The question you posted states that you need to write a program that uses function templates. The code you wrote does not use generics.
Mar 15, 2008 at 5:42am
sorry to ask.
can u give a simple example of generic and function templates?
Mar 15, 2008 at 6:20am
is it by using array??
Mar 15, 2008 at 6:21am
but i dun have an idea of putting array inside the program.....
Mar 15, 2008 at 6:26am
On your header file, you need to have something like:

1
2
3
4
5
6
7
template <typename T>

//function prototypes
double max(const T&);
double min(const T&);
double ave(const T&);
double var(const T&);


Since all the return types are primitives, we can use specifics and let casting do its job.

The example I gave assumes you are using some collective data structure. If you want, you can iterate through the list of items and process them two at a time.

1
2
3
4
5
6
7
8
template <class T> //class or typename shouldn't really matter.

T max(const T t1, const T t2)
{
    return ((t1 > t2) ? t1 : t2);
}

//Follow the same pattern for the rest 
Last edited on Mar 15, 2008 at 6:29am
Mar 15, 2008 at 6:32am
wao, my standard is lower than that. but anyway,
thanks. i'll save down your example and try it later.
thanks.
Mar 15, 2008 at 6:35am
Ok after reading your last reply after my previous post, it seems to me that you are somewhere at entry level.

If you are, then I do not think you should be solving the problem. Unless of course your goal is to make a program that behaves the same as what the problem states.

If this is not something required of you to be done, then just ignore all the generics examples I posted, continue on with what you have. If this is for school work or something of the sort, then you need a lot of catching up to do.
Mar 15, 2008 at 6:42am
haha, thats right. is assignment. i am too lazy before this and we are use to spoon feed by our lectural.
so maybe after this have too work harder la. i just found the template file in the tutorial of cplusplus.com
so maybe i will take a look in it.
Thanks anyway.
Topic archived. No new replies allowed.