10 Replies - 1615 Views - Last Post: 22 August 2011 - 12:15 AM Rate Topic: -----

#1 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

appending element at beginning of an array in C

Posted 18 August 2011 - 06:05 AM

Hello All,


I just want to know how to append an element in the beginning of the array for example if an array of integers
below

[1,2,3,4,5,6,7,8,9]
I want to append 7 in the beginning so how would I do it, sot it becomes

[7,1,2,3,4,5,6,7,8,9]

Cheers

assert©
Is This A Good Question/Topic? 0
  • +

Replies To: appending element at beginning of an array in C

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 06:17 AM

As long as the array size is large enough you will need to shift the entire contents of the array using a loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 06:32 AM

Thanks Jim but C arrays are not dynamic so how would that work for my example above
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 06:41 AM

If your array is not large enough then you need to create a new larger array and copy the characters into this new array.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 paradoxia  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 163
  • Joined: 02-July 11

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 06:53 AM

View Postjimblumberg, on 18 August 2011 - 06:17 AM, said:

As long as the array size is large enough you will need to shift the entire contents of the array using a loop.

Jim


I am not familiar with c so I have written it using c++



#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace System;

int main(array<System::String ^> ^args)
{
	
	int myarray[10]={1,2,3,4,5,6,7,8,9};
	int i;
	std::cout<<"Before rearrangment\n\n";
	for(i=0;i<10;i++)
		std::cout<<myarray[i]<<"\t";
	
	for(i=10;i>0;i--)
		myarray[i]=myarray[i-1];
         
	myarray[0]=7;

	std::cout<<"After rearrangment\n\n";
	for(i=0;i<10;i++)
		std::cout<<myarray[i]<<"\t";
	getch();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 06:58 AM

You may want to consider using a Linear Linked List if you have to stick to C. Basically it creates a struct of data and a pointer to the next struct. In a way it is just like an array, but you can add nodes anywhere in the list of nodes.

Here is a link that explains it better.
http://cslibrary.sta...dListBasics.pdf
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 07:09 AM

in C --
#1 user dynamic arrays.
#2 allocate an array large enough for the additional data
int *newdata = malloc(size+1);
#3 using memcpy to copy the old array to (newdata+1)
int len = memcpy(newdata+1, data, size);
#7 update book keeping:
free(data);
data = newdata;
size = size + 1;
data[0] = <new item to prepend >


of course you actually probably want to grow the size by more than 1 that way you don't have to keep allocating memory. You can use memmove() to "shift" values to the right.

however -- keep linked lists in mind as they are much better at this kind of thing. prepending to an array is costly.
Was This Post Helpful? 1
  • +
  • -

#8 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 08:01 AM

This is why I love this forums I post something but I the idea comes to me suddenly after I post thumbs up!!!!!!
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 10:09 AM

The problem with arrays is that they just don't know how big they are. Allocating slightly more than you need is good for what you want, but then you have to track that too.

I'd do something like:
typedef struct {
   int size, maxSize;
   int *data;
} Array;

Array *arrayCreate(int capacity);
void arrayDestroy(Array *);
void arrayPrint(const Array *);
void arrayGrow(Array *);
void arrayAdd(Array *, int value);
void arrayInsert(Array *, int index, int value);
void arrayAddFront(Array *a, int value) { arrayInsert(a, 0, value); }


Was This Post Helpful? 0
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: appending element at beginning of an array in C

Posted 18 August 2011 - 10:40 AM

ya i use something like baavgai showed. i make the optimization of only resizing every power of 2 though. i do something more like this.

typedef struct {
    int* begin, *end;
    int* front, *back;
} IntArray;

IntArray *arrayCreate(int capacity);
void arrayDestroy(IntArray*);
size_t arraySize(const IntArray*);
void arrayResize(IntArray*);
void arrayPushBack(IntArray *, int value);
void arrayPushFront(IntArray *, int value);
void arrayInsert(IntArray *, int index, int value);
int arrayGet(IntArray *a, int value);
void arraySet(IntArray *a, int index, int value);



i leave space open on both sides so that most of the time the operation that needs to take places is 1 small assignment and an increment of a pointer. it gives near liner time for insertion and deletion which is really nice.
Was This Post Helpful? 0
  • +
  • -

#11 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: appending element at beginning of an array in C

Posted 22 August 2011 - 12:15 AM

To paraphrase most of the above:
1. create a new array of eleven elements
2. use memcpy() to copy 1st array into new array
3. use memove()to move the 10 elements in new array one place to right
4. assign 7 to new array[0]position
QED :clap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1