Alg 3: Insert \'x\' at the beginning of the list stored within the array a by sh
ID: 3664482 • Letter: A
Question
Alg 3: Insert 'x' at the beginning of the list stored within the array a by shifting all elements to the right from 0 to 25.
Assume the following declarations:
char a[100]; int aSize=26; char ch;
Assume also that the first 26 elements have been initialized to some values and the remaining elements have garbage in them.
In the question below the term list stored within the array a means the elements found from position 0 to position 25. The phrase last element in the list refers to the element found in 'a[25]'. The phrase element at position n, refers to the n-th element of a list, which is stored in 'a[n-1]'.
If you add or delete anything from the array, make sure you adjustaSize approriately.
Alg 3: Insert 'x' at the beginning of the list stored within the array a by shifting all elements to the right from 0 to 25.
USE C++
Explanation / Answer
// Algorithm to insert a element at begging of an array
char a[100];
int aSize = 26;
void insert(char c) {
int i ;
for(i=aSize, i>0; i--){// shifting element from left to right(towards end) by one
a[i] = a[i-1];
}
// now value of i shoud be 0, so insert
a[0] = element;
//increase size of a
aSize = aSize + 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.