Re: new() and Borland C++ compiler

Discussion in 'C++' started by John Harrison, Jul 12, 2003.

  1. "Chandrashekar" <> wrote in message
    news:...
    > Hi all,
    >
    > I am facing some problem with Borland C++ compiler. I am using new()
    > function to allocate memory. I am not getting any compilation errors.
    > But while executing
    > the code, I am getting the error.
    >
    > Code is here
    >
    > /*******************************/
    > #include<stdio.h>
    > #include<iostream.h>
    > #include <vector>
    > #include <algorithm>
    > #include <numeric>
    > using namespace std;
    > int periods;
    >
    > class Moving_Average{
    > private:
    > double* forecast;
    >
    > public:
    > Moving_Average(int size)
    > {
    > cout<<" Inm afd "<<endl;
    > forecast = new double[size];
    > cout<<"aldsn nf "<<endl;
    > }
    >
    > void calculate(double *da,int time) {
    > cout<<"In calculate function "<<endl;
    > double sum=0;
    > for(int i=1;i<=time;i++) {
    > sum+=*(da+i);
    > }
    > cout<<"sum of the first tge "<<sum<<endl;
    > forecast[time+1]=sum/time;
    > for(int i=time+1;i<=periods;i++) {
    >

    forecast[time+i]=forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time);
    > }
    > for(int i=1;i<=periods;i++) {
    > cout<<"moving average for period "<<i<<" -->
    > "<<forecast<<endl;
    > }
    > }
    >
    > };
    >
    >
    > void main()
    > {
    > cout<<"Enter the no.of periods of historic data"<<endl;
    > cin>>periods;
    > // Moving_Average MA(periods);
    > vector<double>data(periods);
    >
    > for(int i=1;i<=periods;i++) {
    > cout<<"Enter data for period --> "<<i<<" ";
    > cin>>data;
    > }
    > Moving_Average MA(periods);
    > MA.calculate(data.begin(),2);
    >
    > }
    > /********************************/
    >
    > Output is here
    >
    > Enter the no.of periods of historic data
    > 3
    > Enter data for period --> 1 1
    > Enter data for period --> 2 2
    > Enter data for period --> 3 3
    > Inm afd
    >
    > /************************************/
    > And I am getting following error message.
    >
    > "Thread stopped
    > F:\forecast\tria_array.exe:Fault:access
    > violation at 0x40b060:write of address
    > 0x40080008
    > "
    >
    >
    > Pls help me out. Is it problem of C++ compiler or my code?
    >
    > Thanks,
    >
    > Chandrashekar


    Your code of course.

    In C++ arrays and vectors start at 0

    This is wrong

    for(int i=1;i<=periods;i++) {
    cout<<"Enter data for period --> "<<i<<" ";
    cin>>data;
    }


    This is right

    for(int i=0;i<periods;i++) {
    cout<<"Enter data for period --> "<<i<<" ";
    cin>>data;
    }

    john
     
    John Harrison, Jul 12, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Gomez
    Replies:
    0
    Views:
    614
    Kenneth Gomez
    Dec 3, 2004
  2. Developwebsites

    Borland 5.5 compiler

    Developwebsites, Aug 7, 2003, in forum: C++
    Replies:
    7
    Views:
    576
    Janusz Szpilewski
    Aug 8, 2003
  3. daytripper
    Replies:
    2
    Views:
    486
    Agent Mulder
    Sep 14, 2003
  4. Replies:
    1
    Views:
    1,147
    SuperKoko
    Oct 2, 2006
  5. justmehere
    Replies:
    2
    Views:
    465
    justmehere
    Nov 1, 2007
Loading...

Share This Page