vectors, average and variance functions (data structures)

I am lost on how to go about this assignment. I have most what we need for this assignment, or so we were told. Before I go on, this is the assignment:

Use the vector class template to create a vector of floats of size 100.
Input the numbers using cin until end-of-file (^D). This way you can input the numbers from either standard input (keyboard) or a file using input redirection. As you input the numbers count number of numbers (n). Then make two calles to functions average() and variance(). Remember that the name of an array is a pointer to the base of the array and that is what you pass to the functions.
Your program must run correctly for the following input:

10
2
5
20
8
1
15
6

Be sure to handle end cases such as n = 0, 1, or greater than 100.

So far I have the functions for average and variance and the vector.h file I am trying to tackle how to write the main() for this to do what it is intended to do. Any feedback on this would be greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//homework1.cpp
#include <iostream>
#include "vector.h"	//link to vector.h file (no changes since lab03)

using namespace std;

int main()
{
//Function for average
double avg ( vector& v )
{
        double return_value = 0.0;
        int n = v.size();
       
        for ( int i=0; i < n; i++)
        {
            return_value += v[i];
        }
       
        return ( return_value / size);
}
//****************End of average funtion****************


//Function for variance
double variance ( vector& v , double mean )
{
        double sum = 0.0;
        double temp =0.0;
        double var =0.0;
       
        for ( int j =0; j <= v-1; j++)
        {
            temp = pow(v[j] - mean),2);
            sum += temp;
        }
       
        return var = sum/(v.size() -2);
}
//****************End of variance funtion****************


//****************main from lab03****************
  vector<float> v; 	//creates vector of floats
  v.size(100); 		//size 100 vector of floats

  //Get input from user
  cout << "Please input any number: " << endl;
  for (float i=0; getFloat() != '\n'; i++)
    {
      cin >> v[i];
    }
//****************end of main from lab03****************

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//vector.h file 
#ifndef VECTOR_H
#define VECTOR_H

template <class T> //using templates, which they can substitute for int, double, etc. depending on their function.
class vector       //creating a vector class
{
public:
typedef T * iterator;
    
	// constructors
vector() {buffer = 0; reserve(0);}			
vector(unsigned int size) {buffer = 0; resize(size);}
vector(unsigned int size, T initial)
    {buffer = 0; resize(size); fill(begin(), end(), initial);}
vector(vector & v)
    {buffer = 0; resize(v.size()); copy(v.begin(), v.end(), begin());}
~vector() {delete buffer;}
    
	// member functions
T	back()      {return buffer[mySize-1];}
iterator      begin()     {return buffer;}
int	capacity()  {return myCapacity;}
iterator	end()       {return buffer + mySize;}
bool	empty()     {return mySize == 0;}
T	front()     {return buffer[0];}
void	pop_back()  {mySize--;}
void	push_back(T value);
void	reserve(unsigned int newSize);
void	resize(unsigned int newSize)	{reserve(newSize); mySize = newSize;}
int	size()      {return mySize;}

    // operators
T &	operator[ ](unsigned int index) {return buffer[index];}
private:
unsigned int mySize;
unsigned int myCapacity;
T * buffer;
};



template <class T> void vector<T>::reserve(unsigned int newCapacity)  //reserve capacity at leaset as large as argument value
{
if (buffer == 0)			    
  {					    //*No buffer, zero size
    mySize = 0;				    
    myCapacity = 0;                         
  }
					   //don't do anything if already large enough
if (newCapacity <= myCapacity)             //compares newCapacity to myCapacity if it does not meet the
return;                                    //argument then it will continue
					   //*Allocate new buffer, make sure successful
T * newBuffer = new T [newCapacity];
copy(buffer, buffer + mySize, newBuffer);  //copy this sequence (start at buffer, stop at buffer + mySize, 
                                           //destination is newBuffer) into newBuffer (resets data field)
myCapacity = newCapacity;
delete buffer;				   //deletes the buffer 
buffer = newBuffer;			   //assigns buffer to newBuffer 
}



template <class T> void vector<T>::push_back (T value)  //*Push value on to end of vector
{					  //*Grow buffer if necessary
if (mySize >= myCapacity)		  //if myCapacity is greater than or equal to mySize
reserve(myCapacity + 5);		  //set reserve to myCapacity + 5
buffer[mySize++] = value;
}

# endif
Last edited on
closed account (2AoiNwbp)
Very nice coding style.
Where is getFloat() defined? is it a standard function?
Don't you need to test end of file (EOF) instead of '\n', and check size in the for loop?

regards
Yes I was just writing out some pseudoish code for the main. I'm not sure how I should tackle it. I was told that I should have it take in input from a text file instead of having actual user keyboard input.
homework1.cpp line 10, 26: You can't nest functions. avg and variance can either precede main or follow main. If they follow main, you will need forward declarations for them.

@AbstractionAnon
Yes I did realized this.
After discussing this assignment with some of my colleagues they suggested I just use #include <vector> instead of using a vector.h file since it was not specified in the assignment to use a .h for the use of vectors.
This is the new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <ifstream>

using namespace std;


//Function for average
double avg ( vector& v )
{
        double return_value = 0.0;
        int n = v.size();
       
        for ( int i=0; i < n; i++)
        {
            return_value += v[i];
        }
       
        return ( return_value / size);
}
//****************End of average funtion****************


//Function for variance
double variance ( vector& v , double mean )
{
        double sum = 0.0;
        double temp =0.0;
        double var =0.0;
       
        for ( int j =0; j <= v-1; j++)
        {
            temp = pow(v[j] - mean),2);
            sum += temp;
        }
       
        return var = sum/(v.size() -2);
}
//****************End of variance funtion****************


int main() 
{
  vector<float> v(100); 	//creates vector of floats size 100
  

  //***************Get input from file*******************
  //cout << "Please input any number: " << endl;
  //float num;
  //int i = 0;  
  while(file.eof())
    {
      //CODE
      //cin >> v[i];
    }
}

Topic archived. No new replies allowed.