Hi, i need to write a program that inserts and deletes an element and dont know how to go about it, please assist
Arrays
Collapse
X
-
Well, you create an array of integers of say 10 elements withOriginally posted by RaysharHi, i need to write a program that inserts and deletes an element and don't know how to go about it, please assist
you fill it with commands likeCode:int a[10];
So the forth (starts counting at zero) element is 2.Code:a[3]=2;
I don't see why you need to delete an element, you just can give it another value (as long as it is not defined as constant). In case typing
seems to work but I don't guarantee that (I'm not sure about it)Code:a[3]='\0';
-
To insert an element into an array you need to make sure that the array size is at least 1 element larger than the number of elements it currently holds then assign the value to the n-1 element.
if you mean to insert a value into an element in the correct order the array must be sorted in assending order. Then each element of higher value than the one to be inserted has to be moved one place up. Then the value can be inserted at the correct element location to preserve the order. For loops are usually used to control this activityComment
-
As long as that is where you want to put the element.Originally posted by whodgsonTo insert an element into an array you need to make sure that the array size is at least 1 element larger than the number of elements it currently holds then assign the value to the n-1 element.
Once again, that depends entirely on what you want to do. For some cases "correct" may be random, or descending order.Originally posted by whodgsonif you mean to insert a value into an element in the correct order the array must be sorted in assending order. Then each element of higher value than the one to be inserted has to be moved one place up. Then the value can be inserted at the correct element location to preserve the order. For loops are usually used to control this activityComment
Comment