hi,
thank you for replying. i started a new thread because nobody took a look at the old one. i finally figured out a way to add two arrays and using eArray.
i also wrote the fibonacci code. However, when i debug and print the fibonacci number, i get some numbers like this:
8
10
7
5 5 5 5
4 3 2 1
10 20 30 40
1 7 8 2 4
7 1 2 9 6
8
10 20 30 40 0 0 0 0
10 20
Input n:5
-6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6
-6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -
6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6
-6 -6 -6 -6 -6 -6 -6 -6 -6 -5 -9 -1 -8 0 -8 -8 -4 -8 -8 8
After a is copied to v, v becomes 9 3 4 5 6
After a is added to v, v becomes 8 6 9 1 2
>>it prints the 5th fibonacci number at the end of the gabbage.
when i try to print the 10th fibonacci number which is 89, i get 9 at the end of the gabbage.
Help me please. The eArray class is still the same with some more functions added:
#include <iostream>
using namespace std;
void compLfact(int);
void compLfib(int);
//class Definition
class eArray{
public:
//Constructors
eArray();
eArray(int);//constructor cos the name of the class is d same
//as that of the constructor. they also don't need return types.
//the two constructors are overloaded cos the have the same name.
//accessor function to find the state of the object
int getSize();
//Set the elements to some value
void set(int);//check notes
void set(int, int);//check notes
//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);
void copyTo(eArray);
void addTo(eArray);
private://only the class designer can access the class
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
//size of the array can change; non-static
}
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::copyTo(eArray t){
int i;
for(i=0; i<size; i++)
t.data[i] = data[i];
}
void eArray::addTo(eArray t){
int i, temp, carry;
for(i=size-1,carry=0; i > -1; i--){
temp=data[i] + t.get(i) + carry;
data[i]= temp%10;
carry=temp/10;
}
}
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);
compLfib(n);
eArray a(5);
eArray v(5);
a.set(0, 9);
a.set(1, 3);
a.set(2, 4);
a.set(3, 5);
a.set(4, 6);
cout<<"After a is copied to v, v becomes"<<" ";
a.copyTo(v);
a.dump();
cout<<endl;
cout<<"After a is added to v, v becomes"<<" ";
a.addTo(v);
a.dump();
cout<<endl;
}
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 compLfib(int n){
int i;
eArray a(100);
eArray b(100);
eArray c(100);
a.set(99, 1);
b.set(99, 1);
c.set(99, 1);
for(i=2; i<=n; i++){
c.addTo(a);
a.set(0);
b.copyTo(a);
b.set(0);
c.copyTo(b);
}
c.dump();
}
Thanks in advance for your help.