I urgently need help with this program that i wrote. it is an expandable array. it performs different functions as shown below.

I have shown the outputs of the code below.
My problem is with the output of the compLfact(int) function. How do i get rid of the leading zeros. Please show me how.

#include <iostream>
using namespace std;
void compLfact(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();
    void dumpR();
    void vMpyC(int);
    void aMpyC(int);
    // 
    void expandBy(int);
    void trimTo(int);
private:
    int size;  // Current size
    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);
}
void eArray::init(int n){
   size=n;
   data=new int[n];
}
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;
}
int eArray::get(int index){
    if ( index < 0 || index >= size ){
        cout<<"Error******"<<endl;
        cout<<"Invalid index --- Operation aborted"<<endl;
        return -1;
    }
    else return data[index];
}
void eArray::dump(){
    for(int i=0; i<size; i++)
        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;
}
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 memeber function exapands the size of the 
// array by adding n more elements to it.
void eArray::expandBy(int n){
int news=size +n;
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 dont need the old data no more
    // 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 guys into temp
          for(i=0; i<n; i++)
              temp[i]=data[i];
          //Now you dont need the old data
          delete []data;
          //make data points to the temp
          data=temp;
          size=n; //update size
    }
}

void main(){
eArray x(8);
    x.set(10);
    cout<<x.getSize()<<endl;
    cout<<x.get(4)<<endl;
    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);
    cout<<y.getSize()<<endl;
    y.dump();
    y.trimTo(2);
    y.dump();
    int n;
    cout<<"Input n:"; cin>>n;
    compLfact(n);

}
void compLfact(int n){
eArray d(20);
int i;
       d.set(0);
       d.set(19,1);
       for (i=2; i<=n; i++)
           d.aMpyC(i);
       cout<<"The answer is:"<<endl;
       d.dump();
}

// the output of the array is:

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:6
The answer is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 2 0
}

My main problem is with the output of the factorial. How do i get rid of all the leading zeros before the number.
instead of 000000......720, i want it to display just 720. I want this to work for any number. Please help me.

Recommended Answers

All 2 Replies

>I urgently need help
Nobody cares if it's urgent for you. Stating that your problem is urgent will cause the nice people to do nothing different than they normally would and the sadistic people to actively ignore you.

>How do i get rid of the leading zeros.
Do a partial loop that skips over the leading zeros:

void eArray::dump(){
  int i;

  // Skip leading zeros
  for ( i = 0; i < size; i++ ) {
    if ( data[i] != 0 )
      break;
  }

  // Dump the significant value
  while ( i < size )
    cout<< data[i++]<<' ';

  cout<<'\n';
}

thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.