3 Replies - 171 Views - Last Post: 21 October 2019 - 02:14 AM Rate Topic: -----

#1 Evilgaga   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-October 19

c - appending elements from one array to the end of another array

Posted 20 October 2019 - 01:35 PM

So basically I'm making a program where users can shop for items from a store. I'm having trouble with a function 'givesItems' that causes all of shopper one's items to be given to shopper two to keep. If shopper two does not like any of the items or cannot keep them, then they won't take them or gets rid of them. Person one should have nothing in their bag by the end of the exchange, and person two should have their original items AND the items that they like/want to keep from person one's bag. I'm not quite sure how I'm supposed to append items to an array and keep growing it at the same time. I was thinking of using malloc or something like that but I don't have much experience with those methods. Any hints or help would be appreciated.
void giveItems(Person *shopperOne, Person *shopperTwo){
    int i;
    for(i = 0; i < shopperOne->numberItems; i++){
        if(likes(shopperTwo, &(shopperOne->bag[i]))){
            if(keep(shopperTwo, &(shopperOne->bag[i])));

                  //what do

                shopperOne->numberItems = 0;
        }
    }
}


Here are also the likes and keep functions if they're needed:
int likes(Person *shopper, Item *item) {
    return person -> keepSize <= item -> size;
}

int keep(Person *shopper, Item *item) {
    if(person -> numberItems == MAX_ITEMS_IN_BAG) return 0;
    person -> bag[person -> numberItems] = *item;
    person -> numberItems++;
    return 1;
 }


Structures:
typedef struct {
    unsigned char size;
    char *type;
} Item;


typedef struct {
    char *name;
    unsigned char keepSize;
    Item bag[MAX_ITEMS_IN_BAG];
    unsigned char numberItems;
} Person;


typedef struct {
    Item item[MAX_ITEMS_IN_STORE];
    unsigned char numberItems;
} Sto


This post has been edited by Skydiver: 20 October 2019 - 02:21 PM
Reason for edit:: Please use code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: c - appending elements from one array to the end of another array

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,221
  • Joined: 05-May 12

Re: c - appending elements from one array to the end of another array

Posted 20 October 2019 - 02:17 PM

Please use code tags when you post code.

Since you are using C++, you should be using the built in std::vector instead of managing a dynamically allocated array yourself. Also, in C++, if do decide to dynamically allocated memory, you should use new and delete, not malloc() and free()

Update: Missed that you wanted to do things is C, rather than C++.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,221
  • Joined: 05-May 12

Re: c - appending elements from one array to the end of another array

Posted 20 October 2019 - 02:43 PM

So with regards to your problem in C, either you'll have to respect the MAX_ITEM_IN_BAG and stop copying over items from one person's bag into another person's bag; or you can say that there is virtually no maximum and dynamically increase the size of each person's bag. I suggest looking at malloc() to do the initial bag size, and then calling realloc() to increase the size as needed. Obviously, when in is time to release the memory, use free().

Given the fact that you are using unsigned char to store the number of items in bag, my recommendation is simply set MAX_ITEM_IN_BAG to be 255, since unsigned char can only range from 0 to 255. This eliminates having the complexity of dealing with dynamic memory, as well as, with modern computers an array of 255 * 9 bytes is nothing.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7500
  • View blog
  • Posts: 15,541
  • Joined: 16-October 07

Re: c - appending elements from one array to the end of another array

Posted 21 October 2019 - 02:14 AM

If you've covered linked lists, now is probably the time to use them.

We don't see where you add items... I'd have the following:
// you implement these
void addItem(Person *, Item *);

void removeItem(Person *, int index);

// helper
int personEmpty(Person *p) {
    return p->numberItems == 0;
}

// this should work, if you've implemented those two functions
void giveItems(Person *giver, Person *taker) {
    while (!personEmpty(giver))  {
        Item *item = giver->bag + 0; // just keep plucking off the first item
        if (likes(taker, item) && keep(taker, item)) {
            addItem(taker, item);
        }
        removeItem(giver, 0);
    }
}



For running out of space, you can simply have add item fail. If you would like your array to change in size, then yes, *alloc functions are your friends. However, your structs will change a little. I also don't see the need for two item collections. I'd go with something like:
typedef struct {
    unsigned char size;
    char *type;
} Item;

typedef struct {
    Item *item;
    unsigned capacity, numberItems;
} Items;

typedef struct {
    char *name;
    unsigned keepSize;
    Items items;
} Person;

typedef Items Store;

Items *initItems(unsigned capacity);
void growItems(Items *);
void freeItems(Items *); // don't forget to clean up
void addItem(Items *, Item *);
void removeItem(Items *, int index);



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1