473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with vectors

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
6 4499
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

"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
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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
4
by: Brian Basquille | last post by:
Hello all, Well, we've gotten to it: the real meaty area of my Air Hockey game.. Any help or suggestions for the following would be much appreciated. In plain terms: My paddle needs to be...
11
by: natman | last post by:
Hi i need to write a funcition called install, its takes as arguments 1- a vector 2-a number 3-a list called next_level so heres where it get kinda weird: next_level is a list comprising of...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: Thelma Lubkin | last post by:
I use my own matrix and vector classes. I wrote them before such things were generally available and I've stuck with them ever since. I've just added an Octonion class derived from the vectors...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.