By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 930 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Help with vectors

P: n/a
Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
}

void calculate(double *da, int time){
/* This function will calculate the moving averages (one method of
forecasting)
and then store the values in the vector
*/
double sum=0;
for(int i=1;i<=time;i++) {
sum+=*(da+i);
}
cout<<"sum of the first tge "<<sum<<endl;
forecast.pushback(sum/time);

for(int i=time+1;i<=size;i++) {
forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time));
}
for(int i=1;i<=forecast.size();i++) {
cout<<"moving average for period "<<i<<" -->
"<<forecast[i]<<endl;
}
}
};
void main()
{
Moving_Average MA;
cout<<"Enter the no.of periods of historic data"<<endl;
cin>>periods;
MA(periods);
vector<double>data(periods); // This vector holds the data

for(int i=1;i<=periods;i++) {
cout<<"Enter data for period --> "<<i<<" ";
cin>>data[i];
}
MA.calculate(data.begin(),2);

}

/******************************************* /

I am getting following erros.

1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header
2)tria.cpp(9,14):Type name expected
3)tria.cpp(9,14):Declaration missing;

and so on..

Pls mail me what is wrong with my code.

Thanks in advance..

Chandrashekar
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Chandrashekar wrote:
Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
<iostream.h> is a non-standard header. Use <iostream> instead, and
<stdio.h> is deprecated and should be replaced by <cstdio>. But it
doesn't seem that you use anything from that header anyway.
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;
vector is in namespace std. So write:

std::vector<double> forecast;
public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
}

void calculate(double *da, int time){
/* This function will calculate the moving averages (one method of
forecasting)
and then store the values in the vector
*/
double sum=0;
for(int i=1;i<=time;i++) {
sum+=*(da+i);
That's not an error, but it would look cleaner if you wrote:

sum+=da[i];

I wonder: does da really point to time+1 values, and why aren't you
using the first value? Remember, indexes start at 0.
}
cout<<"sum of the first tge "<<sum<<endl;
cout and endl are also in namespace std.
forecast.pushback(sum/time);

for(int i=time+1;i<=size;i++) {
Where is size defined?
forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time)); }
for(int i=1;i<=forecast.size();i++) {
cout<<"moving average for period "<<i<<" -->
"<<forecast[i]<<endl;
}
}
};
void main()
main() must return int in C++. Some compilers let you get away with it,
but it's still an error.
{
Moving_Average MA;
Here you create a Moving_Average object using the default constructor.
Unfortunately, that class doesn't have a default constructor, so you
can't do that.
cout<<"Enter the no.of periods of historic data"<<endl;
cin>>periods;
MA(periods);
Is that supposed to construct the MA object? You can't do that. This
will instead try to call operator() of the Moving_Average class. Since
there is no operator() defined, this can't work. You cannot construct
an object twice. Either defer the construction to this place, i.e.
remove the first line of main() and write here:

Moving_Average MA(periods);

or create an own member function to specify the size. Anyway,
reserve()ing the space in a vector is only an optimization. It's not
mandatory. If you don't do that, push_back() will automatically
allocate the needed space.
vector<double>data(periods); // This vector holds the data

for(int i=1;i<=periods;i++) {
cout<<"Enter data for period --> "<<i<<" ";
cin>>data[i];
Here, you have the index problem again (and at various other places).
You don't put anything in the first element of the vector and you write
one element past its end.
}
MA.calculate(data.begin(),2);
Why 2? Shouldn't it be periods? Also, you can't specify data.begin()
here. This function returns an iterator to the first element of the
vector. While a vector iterator can be a pointer, it's not required to,
and some compilers (like gcc) have an own iterator class. Use &data[0]
instead.

}

/******************************************* /

I am getting following erros.

1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header
2)tria.cpp(9,14):Type name expected
3)tria.cpp(9,14):Declaration missing;

and so on..
You should have listed all of them and marked the lines your compiler is
referring to. It makes it a lot easier to find the errors.

Pls mail me what is wrong with my code.


No. This is not a write-only newsgroup. Post here - read here.

Jul 19 '05 #2

P: n/a

"Chandrashekar" <ch******@bhelhyd.co.in> wrote in message
news:94*************************@posting.google.co m...
Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here


No you aren't, you are reserving memory for the vector, its size is still
zero.

If you want to resize a vector use resize.

forecast.resize(size); //I am specifying the size of vector

john
Jul 19 '05 #3

P: n/a
John Harrison wrote:

"Chandrashekar" <ch******@bhelhyd.co.in> wrote in message
news:94*************************@posting.google.co m...
Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector
forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
No you aren't, you are reserving memory for the vector, its size is
still zero.


That's actually what the OP meant, I'd say.

If you want to resize a vector use resize.

forecast.resize(size); //I am specifying the size of vector


He's later using push_back to add the values to the vector, in which
case using resize instead of reserve is fatal.

Jul 19 '05 #4

P: n/a
Hi all,

Thanks a lot for your response. I made some changes to code. I am
writing here the code.
/***********************************************/

#include<iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Moving_Average{
public:
std::vector<double> forecast;
// vector<double>forecast;

public:
Moving_Average(int size) {
cout<<" IN size "<<endl;
forecast.resize(size);
cout<<" Out sized "<<endl;
}

void calculate(double *da,int time) {

/* This function is merely copying the elements of vector "data" into
vector "forecast"
*/
for(int i=0;i<time;i++) {
forecast.pushback(*(da+i));
}
}
void prasint(int time) {
for(int i=0;i<time;i++) {
cout<<"asd "<<forecast[i]<<endl;
}
}
};
int main()
{
int periods;
cout<<"Enter the no.of periods of historic data"<<endl;
cin>>periods;
vector<double>data(periods);
for(int i=0;i<periods;i++) {
cout<<"Enter data for period --> "<<i<<" ";
cin>>data[i];
}
Moving_Average MA(periods);
MA.calculate(data.begin(),periods);
MA.prasint(periods);
}

/************************************************** *********/

I am getting following messages
1)Cannot create pre-compiled header:code in header
2)Functions containing for are not expanded inline
3)Functions containing for are not expanded inline
4)'pushback' is not a member of 'std::vector<double>'

It seems last one is an erros message while the rest are warnings.
Pls help me

Thanks,
Chandrashekar
Jul 19 '05 #5

P: n/a
hi,

In continuation to my earlier mail. When I used pish_back() in place
of pushback(), i didnt get any compilation errors. I am pasting the
code here

/**********************?
void calculate(double *da,int time) {
cout<<" IN calc";
double te;
for(int i=0;i<time;i++) {
cout<<" value is -->"<<(*(da+i))<<endl; // Here it is printing
correct value
te=*(da+i);
forecast.push_back(te); // But here it is storing wrong values
// ^^^^^^^^^

cout<<" Out of cal";
}
/********************************/
Can somebody tell me what is the difference between pushback() and
push_back()?

And I am not getting proper output. In the above program I am copying
the elements of vector 'data' (i.e da) to vector forecast. But I am
getting a wrong output. Here is the sample out put
/******************************/

Enter the no.of periods of historic data
3
Enter data for period --> 0 1
Enter data for period --> 1 2
Enter data for period --> 2 3

IN calc value is -->1
Out of cal value is -->2
Out of cal value is -->3
Out of cal
in print
asd 1.88323e-307
asd 1.88323e-307
asd 1.88323e-307

/**********************************/

Pls help me out..
Thanks,
Chandrashekar
Jul 19 '05 #6

P: n/a


Chandrashekar wrote:
I am getting following messages
1)Cannot create pre-compiled header:code in header
Precompiled headers are specific to your implementation.
So look up your documentation for 'precompiled headers'.
2)Functions containing for are not expanded inline
3)Functions containing for are not expanded inline
Those are the same. The compiler tells you that your request
for inlineing a function is not honored and why it will not
do it.
4)'pushback' is not a member of 'std::vector<double>'
Sure. There is no such function. It is called push_back

It seems last one is an erros message while the rest are warnings.
Pls help me


Start by helping yourself. This includes: using the documentation
that came with your compiler and reading some books about C++.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.