Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: new() and Borland C++ compiler

Reply
Thread Tools

Re: new() and Borland C++ compiler

 
 
John Harrison
Guest
Posts: n/a
 
      07-12-2003

"Chandrashekar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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[i]<<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[i];
> }
> 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[i];
}


This is right

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

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex and Borland Command Line Compiler justmehere C++ 2 11-01-2007 11:56 PM
QUERY: Borland C++ 4.5 vs Borland Turbo C++ 1.01 dhruba.bandopadhyay@hotmail.com C++ 1 10-02-2006 10:57 AM
Installation Question for libxml & Borland C++ 5 Compiler Kenneth Gomez XML 0 12-03-2004 04:03 PM
dos graphics with borland command line compiler 5.5 ? daytripper C++ 2 09-14-2003 08:35 PM
Borland 5.5 compiler Developwebsites C++ 7 08-08-2003 12:25 PM



Advertisments