Tuesday, July 28, 2009

Write a program to insert an element at the specified position in the given array in c language?

With a problem like this in C it's easy to get yourself into trouble. You have to be careful about managing the memory used by your array, and making sure not to index outside of the array bounds. For example, if you had an array declared like this:


int a[ ] = { 1, 2, 3 };


and you wanted to insert a new value in between 1 and 2, where does it go? The array a[ ] is a fixed size, there's no room for another element. So you need to use dynamic memory.





In an object-oriented language you would want to define Array class that manages the array storage and provides operations for array element insertion, deletion, etc. In C, you can still think and program this way, even though the language gives you little or no support. I wrote the example below to show you a way to create and use a simple IntArray "class" in C. The array insertion operation is there, which is what you were asking about. You might like to make it more complete by adding other operations like replace, append, and delete.





I have a feeling that some of the concepts and code here may be beyond where you are in your learning of C. If you study and understand this code, I'm sure you can learn something.





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;





typedef struct {


unsigned size; /* total size of array */


unsigned arrayLen; /* number of elements in use */


int *a; /* pointer to start of array */


} IntArray, *IntArray_p;





IntArray_p newIntArray(unsigned size);


void deleteIntArray(IntArray_p);


IntArray_p insert(IntArray_p, unsigned pos, int val);


IntArray_p grow(IntArray_p, unsigned newSize);


void print(const IntArray_p, const char *);





int main(int argc, char *argv[]) {


int i;


IntArray_p a0 = newIntArray(5);


for (i = 0; i %26lt; a0-%26gt;size; i++) {


a0 = insert(a0,i,i+1);


}


print(a0,"a0");


a0 = insert(a0,3,99);


print(a0,"a0 (inserted 99 at pos = 3)");


a0 = insert(a0,0,88);


print(a0,"a0 (inserted 88 at pos = 0)");


a0 = insert(a0,10,77);


print(a0,"a0 (inserted 77 at pos = 10)");


a0 = insert(a0,9,66);


print(a0,"a0 (inserted 66 at pos = 9)");


deleteIntArray(a0);


return 0;


}





IntArray_p newIntArray(unsigned size) {


IntArray_p newIntArray = calloc(1,sizeof(IntArray));


newIntArray-%26gt;size = size;


newIntArray-%26gt;arrayLen = 0;


newIntArray-%26gt;a = calloc(size,sizeof(*(newIntArray-%26gt;a)));


return newIntArray;


}





void deleteIntArray(IntArray_p this) {


free(this-%26gt;a);


free(this);


}





IntArray_p insert(IntArray_p this, unsigned pos, int val) {


int i;


if (pos %26gt;= this-%26gt;size) {


this = grow(this,pos+1);


this-%26gt;arrayLen = pos;


} else if (this-%26gt;arrayLen == this-%26gt;size) {


this = grow(this,this-%26gt;size+1);


}


++this-%26gt;arrayLen;


for (i = this-%26gt;arrayLen-1; i %26gt; pos; --i) {


this-%26gt;a[i] = this-%26gt;a[i-1];


}


this-%26gt;a[pos] = val;


return this;


}





IntArray_p grow(IntArray_p this, unsigned newSize) {


if (newSize %26gt; this-%26gt;size) {


IntArray_p a1 = newIntArray(newSize);


a1-%26gt;arrayLen = this-%26gt;arrayLen;


memcpy(a1-%26gt;a,this-%26gt;a,sizeof(*(a1-%26gt;a)) * this-%26gt;arrayLen);


deleteIntArray(this);


return a1;


} else {


return this;


}


}





void print(const IntArray_p this, const char *s) {


int i;


printf("%s :\n",s);


printf(". total size = %d\n",this-%26gt;size);


printf(". in use = %d\n",this-%26gt;arrayLen);


printf(". a[] = ");


for (i = 0; i %26lt; this-%26gt;arrayLen; i++) {


printf("%d ",this-%26gt;a[i]);


}


puts("\n");


}

Write a program to insert an element at the specified position in the given array in c language?
I am not sure your request falls with in the abilities of most folks on yahoo answers.





Attention to the guy above: I had my total Yahoo answer account "what is the word" lost, forfit, deleted, ect, for giving a much softer answer than you did. Good luck





Good luck on finding a solution.


No comments:

Post a Comment