Hi,
i am getting frustrated with my code. please, please, please help me.
i wrote a class eArray with different functions and i have all of them working but 2.
for the first i am trying to get the nth fibonacci number.
the second one, i am trying to add two equal sized eArrays where both are two multi digit numbers.
the operation is of the form P+=Q.
i am getting some errors like:
Error 1 error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'eArray' h:\ece 304\ece304w1fp1(earray)\ece304w1fp1(earrray)\ece304w1fp1(earrray)\code.cpp 213
please help me. any help is greatly appreciated.
Below is my posted code:
#include <iostream>
using namespace std;
void compLfact(int);
void copyFrom();
void addTo();
void lfib(int);
//class Definition
class eArray{
public:
//Constructors
eArray();
eArray(int);
//accessor function to find the state of the object
int getSize();
//Set the elements to some value
void set(int);
void set(int, int);
//Get the value of an element
int get(int);
//Print functions
void dump();//print everything
void dumpR();
void vMpyC(int);//vector multiply by an integer constant
void aMpyC(int);//arithmetic multiply by an integer
void expandBy(int);
void trimTo(int);
private://
int size; //current size of the array
int* data;//pointer to the array
void init(int);
};
//class Implementation
eArray::eArray(){
init(4); //default size = 4 elements
}
eArray::eArray(int s){
init(s);//whatever value u want
}
void eArray::init(int n){
size = n;
data = new int[n];//dynamically declared array
}
void eArray::set(int v){
for(int i=0; i<size; i++)
data[i]=v;
}
void eArray::set(int index, int val){
if (index < 0 || index >= size){
cout<<"Error******"<<endl;
cout<<"Invalid index --- Operation aborted"<<endl;
}
else data[index] = val;
}
int eArray::getSize(){
return size;//tells the no. of active elements there are.
}
int eArray::get(int index){
if (index < 0 || index >= size){
cout<<"Error******"<<endl;
cout<<"Invalid index --- Operation aborted"<<endl;
return -1;//if the index is wrong
}
else return data[index];
}
void eArray::dump(){
int i;
for(i=0; i<size; i++){
if ((data[i] !=0))
break;
}
while (i<size)
cout<<data[i++]<<" ";
cout<<endl;
}
void eArray::dumpR(){
for(int i=size-1; i>=0; i--)
cout<<data[i]<<" ";
cout<<endl;
}
void eArray::vMpyC(int c){
for(int i = 0; i < size; i++)
data[i] *=c;//multiply each component of the array by constant c
}
void eArray::aMpyC(int c){
int i,carry,temp;
for(i=size-1,carry=0; i > -1; i--){
temp=data[i]*c + carry;
data[i]= temp%10;
carry=temp/10;
}
}
//This member function expands the size of the array by adding
//n more elements to it.
void eArray::expandBy(int n){
int news=size+n;//news means new size
int *temp = new int[news];//create a temp array
int i;
//copy the old data into temp
for(i=0; i<size; i++)
temp[i]=data[i];
//set the newly added elements to zero
for(i=size; i<news; i++)
temp[i]=0;
// we don't need the old data anymore
//so we delete them
delete []data;
data=temp; //point data to temp
size=news;//update the size
}
//This function trims the vector by removing all but first
//n components
void eArray::trimTo(int n){
int *temp, i;
if(n > size){
cout<<"Error in size spec****"<<endl;
cout<<"Operation is aborted"<<endl;
}
else {
temp = new int[n];
//copy the first n numbers into temp
for (i=0; i<n; i++)
temp[i]=data[i];
//Now the old data isn't needed
delete []data;
//make data points to the temp
data=temp;
size=n;//update size
}
}
/*void eArray::lfib(int f){
int i, x, y;
x = 1;
y = 1;
for(i=1; i<=f; i++){
x = x + y;
y = y + x;
cout<<x<<endl;
cout<<y<<endl;
}
}*/
void main(){
eArray x(8);
x.set(10);
cout<<x.getSize()<<endl;
cout<<x.get(4)<<endl;//4th element is 10
x.set(4,7);
cout<<x.get(4)<<endl;
eArray y;
y.set(5);
y.dump();
int i;
for(i=1; i<5; i++)
y.set(i-1, i);
y.dumpR();
y.vMpyC(10);
y.dump();
eArray z(5);
z.set(0, 1);
z.set(1, 7);
z.set(2, 8);
z.set(3, 2);
z.set(4, 4);
z.dump();
z.aMpyC(4);
z.dump();
y.expandBy(4);//increase the size of the vector y by
//4 more components. size is 4, after expansion,
//size will be 8.
cout<<y.getSize()<<endl;
y.dump();
y.trimTo(2);//to keep 2 numbers instead of 8 numbers
y.dump();
int n;
cout<<"Input n:"; cin>>n;
compLfact(n);
copyFrom();
lfib(5);
}
void compLfact(int n){
eArray d(20);//create an expandable array of 20 numbers.
int i;
d.set(0);//set all the numbers to zero.
d.set(19,1);//factorial of zero is one.
for(i=2; i<=n; i++)
d.aMpyC(i);
cout<<"The answer is"<<" ";
d.dump();
cout<<endl;
}
void lfib(int a){
int i;
eArray x(500), y(500), ans(500);
x=y=1;
for(i = 1; i<=a; i++){
ans = x + y;
x = y;
y = ans;
}
}
void copyFrom(){
int i;
eArray P(5);
P.set(0, 2);
P.set(1, 3);
P.set(2, 4);
P.set(3, 5);
P.set(4, 6);
cout<<" "<<endl;
cout<<"P elements:";
P.dump();
cout<<endl;
eArray Q(5);
for(i=0; i<=5; i++)
Q = P;
cout<<"After P is copied into Q, Q is:";
Q.dump();
cout<<endl;
}
void addTo(){
int i;
eArray P(5);
eArray Q(5);
P.set(0,10);
P.set(1,20);
P.set(2,30);
P.set(3,40);
P.set(4,50);
for(i=0; i<=5; i++)
Q.set(i-1, i);
P+=Q;
cout<<"The sum is"<<" ";
P.dump();
cout<<endl;
}