Click here to Skip to main content
15,895,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?
Posted
Comments
nv3 13-Aug-13 13:41pm    
Yes, it's possible in C :-) Just allocate the two other arrays and copy the elements from a to b respectively c. The copying can be done with a little loop or with a call of memcpy. It is so easy that I don't dare to write it down in code as this would spoil your homework.
H.Brydon 13-Aug-13 17:39pm    
Ha ha - I owe you a +5 for that pseudo-answer. Watch for it... :-)
nv3 14-Aug-13 1:59am    
Thanks, Harvey :-) I think, is OP still on the first pages of his C tutorial and cannot appreciate yet the fine points about dynamic arrays and pointers. Hope, you are doing fine.
sash_kp 14-Aug-13 12:40pm    
Thank you for your help...Appreciate it.

What you need is a dynamically allocated/sized array that isn't natively supported by the language but you can create one for yourself. Since you are a beginner I would recommend you to write an array that doesn't reallocate its storage as needed. By leaving out memory management you make your task simpler. Its ok to bother with memory management later when you are better at the C language. Without memory management your dynamically sized array will be a combination of a fixed size array and an integer that indicates how many items are valid in the array (of course at the beginning of the array). The rest of the items are unused. Without memory management this array has a limit on the maximum size and it occupies that maximum size all the time even if the list is empty. Still, as a first try it is simpler to implement.
C++
#define MAX_ARRAY_LEN 20

typedef struct MyArray
{
    size_t length;
    int items[MAX_ARRAY_LEN];
} MyArray;


void MyArray_Init(MyArray* arr)
{
    arr->length = 0;
#if _DEBUG
    memset(arr->items, 0, sizeof(arr->items));
#endif
}

void MyArray_Add(MyArray* arr, int item)
{
    assert(arr->length < MAX_ARRAY_LEN);
    arr->items[arr->length] = item;
    ++arr->length;
}

void MyArray_Remove(MyArray* arr, size_t index)
{
    assert(index < arr->length);
    memmove(&arr->items[index], &arr->items[index+1], (arr->length-index-1)*sizeof(int));
    --arr->length;
}

// You additional methods come here...
// Like: int MyArray_Contains(MyArray* arr, int item);
 
Share this answer
 
Comments
CPallini 13-Aug-13 14:39pm    
5.
pasztorpisti 13-Aug-13 14:52pm    
Thank you!
Sergey Alexandrovich Kryukov 13-Aug-13 15:26pm    
5ed.
—SA
pasztorpisti 13-Aug-13 15:38pm    
Thank you!
ridoy 13-Aug-13 16:36pm    
good explanation with working example,5ed!
Depending on your actual needs, the quick and dirty solution could even be
C
int * b = &a[0];
int * c = &a[2];

Of course you have to keep track (at least implicitly) of b and c sizes.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Aug-13 15:26pm    
In certain cases, why not? A 5.
—SA
CPallini 13-Aug-13 16:11pm    
Thank you very much, Sergey.
ridoy 13-Aug-13 16:36pm    
really quick,+5 :)
CPallini 13-Aug-13 17:32pm    
Thank you.
pasztorpisti 13-Aug-13 17:47pm    
+5, just realized the use case for this... :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900