Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,427 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!




Array - urgent

 
Reply to this topicStart new topic

Array - urgent

olams
17 Jan, 2008 - 08:50 AM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 22


My Contributions
Hello,
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.

CODE
#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.


*mod edit - added code tags
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Array - Urgent
17 Jan, 2008 - 08:53 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You are iterating through each element of the array to print. Why not simply check the value, and don't print if it's 0?
User is offlineProfile CardPM
+Quote Post

GigaRoc
RE: Array - Urgent
17 Jan, 2008 - 09:14 AM
Post #3

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 6

why not while outputing do something like this:

PS: you should really indent your code... makes is easier to read

CODE

void eArray::dump(){
int lz=0;  // lz is 0 if there hasn't been a non-zero int yet
for(int i=0; i<size; i++)
if ((data[i] !=0) || (lz !=0))
   {
       lz == 1;
       cout<<data[i]<<" ";
       cout<<endl;
   }
}


}
User is offlineProfile CardPM
+Quote Post

olams
RE: Array - Urgent
18 Jan, 2008 - 01:49 PM
Post #4

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 22


My Contributions
QUOTE(Amadeus @ 17 Jan, 2008 - 09:53 AM) *

You are iterating through each element of the array to print. Why not simply check the value, and don't print if it's 0?



Thank you.

Thank you very much.



QUOTE(GigaRoc @ 17 Jan, 2008 - 10:14 AM) *

why not while outputing do something like this:

PS: you should really indent your code... makes is easier to read

CODE

void eArray::dump(){
int lz=0;  // lz is 0 if there hasn't been a non-zero int yet
for(int i=0; i<size; i++)
if ((data[i] !=0) || (lz !=0))
   {
       lz == 1;
       cout<<data[i]<<" ";
       cout<<endl;
   }
}


}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month