Hi every one well i have this problem: Im creating a method for multiplying two different lists sorted in arrays example: MyList a [9,9,9,9] * MyList B[1,1,9]

the problem is that i was told not to convert to a large int and then multiply like this 9999*9 +9999*1+9999*9

Since the objective is to use array Lists i already figure out that i have to take the firs number of one of the array's multiply it by each number on the second list (array) and stored each individual result in a new array and then use the method AddList (that i already create).

there's no problem on doing the "for" or anything else my only question is :

How can i create as many arrays as number on the list b and rename them on the process this is my idea of the algorithm:

void MyLIST::mult (MyList a, MyList B)
{
int count;
    for ( int i = b.size ,i>0;i-- ) //in our case b.size=3 so it should create 3 new list
          {
             myList i = new myList(); // this is the problem!!!!!! can't rename like this.
               
             for(int j =b.size-1; j>0; j-- )
                   {
                       for(int y=a.size-1; y>0; y--) 
                         {
                      i[count ]= a[y]*b[j]; //multiplication not every case contemplated here
                         count++;//counter to fill the new array 
                        }
                   }
          }
}

this algorithm wont work is just an example;
so again does any one have an idea of how can create the lists and renamed them with out knowing a fix number for the array size????

thanks in advance.
and just to be clear this isn't a homework just personal hobbie and asking here was my last resource thank you.

Edited 5 Years Ago by WolfPack: Added code tags

To start with, you need to be more careful about capitalization. C++ is a case-sensitive language; MyList is not the same as MyLIST or myList . This alone could be the source of some of the errors.

Secondly, you don't want to rename an object to something else; you want to copy the object to another object. Talking of 'renaming' is likely to confuse everyone, since you cannot do that.

Now, the reason that the MyList i = new MyList(); doesn't work is because the new operator returns a pointer to a newly allocated object, whereas declaring the List the way you have creates a local object of type MyList - not a pointer. if you declare it as

myList * list = new myList();

you'd be on the right track.

Note, however, that you need to delete the allocated memory when you're done with it:

MyList* MyList::mult (MyList a, MyList B)
{
    int count;

    for ( int i = b.size; i>0; i-- ) //in our case b.size=3 so it should create 3 new list
    {
        MyList* list = new MyList();

        for(int j = b.size - 1; j>0; j-- )
        {
           for(int y = a.size - 1; y>0; y--) 
           {
                list[count]= a[y]*b[j]; //multiplication not every case contemplated here
                count++;//counter to fill the new array 
           }
        }
    }
    return list;
}

This still isn't complete, however, as you need to be able to allocate a MyList with a given list size, which means that the MyList() constructor has to get a size and allocate the appropriately sized array, and the destructor has to free it.

Edited 5 Years Ago by Schol-R-LEA: n/a

Ok
before anything else thank you for your quick answer. Since it was my first post in the community i wasn't sure how specific i should be with my code but now i get and i also get that what i was asking for can`t be done as i wanted too il try to figure out a better way.

THANK YOU and Happy X-mas Schoil-R-LEA and DaniWeb Community

This article has been dead for over six months. Start a new discussion instead.