answersLogoWhite

0


Best Answer

Arrays cannot be left-shifted. That term applies to bit values (it is a bitwise operator). Although conceptually the same, when we shift an array's elements we must traverse the array from the first to penultimate element, copying the next element in place of the current element as we go. This is usually referred to as shunting, and can be done in either direction (left and right).

It's actually much easier think of a left shunt by one element as simply meaning delete the first element. But in reality what you actually do is shunt all the elements (except the first) one position to the left and delete the last element since it will then be a copy of the penultimate element. Alternatively, after shunting all the elements to the left, you can replace the final element with a default value (akin to filling the last bit with a zero as per a bitwise left shift).

The following example demonstrates both techniques using a C++ vector (which is exactly the same as a dynamic array in C but is considerably easier to implement):

#include <iostream>

#include <vector>

// Type definitions to simplify the code.

typedef std::vector<int> int_array;

typedef int_array::iterator it;

void shift_left(int_array& a)

{

// Shift all elements to the left by one position

// and delete the final element.

if( a.size() )

{

unsigned int idx=0;

for(it i=a.begin()+1; i!=a.end(); ++i)

{

a[idx]=a[idx+1];

++idx;

}

a.pop_back();

a.shrink_to_fit();

}

}

void shift_left(int_array& a, int default_value)

{

// Shift all elements to the left by one position

// and replace the final element with the given

// default value.

if( a.size() )

{

unsigned int idx=0;

for(it i=a.begin()+1; i!=a.end(); ++i)

{

a[idx]=a[idx+1];

++idx;

}

a[idx]=default_value;

}

}

void print_array(int_array& a)

{

using namespace std;

for(it i=a.begin(); i!=a.end(); ++i )

cout<<" "<<*i;

cout<<endl;

}

int main()

{

int_array a;

for(int i=0; i<9; ++i )

a.push_back( i+1 );

print_array( a );

shift_left( a );

print_array( a );

shift_left( a, 0 );

print_array( a );

return(0);

}

Output:

1 2 3 4 5 6 7 8 9

2 3 4 5 6 7 8 9

3 4 5 6 7 8 9 0

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Program to left shift of an array by one position?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write an algorithm to insert an item to a given location in sorted array?

To insert a number N into array A at index I: // Resize A if necessary If A is too small to add a new element then resize A // Right-shift all elements starting from position I For i = A.length to I A[i] = A[i - 1] // Insert new item A[I] = N


Insert an element after an element in an array?

To begin, obtain the element to be added, such as x Then, say pos, get the position where this element will be put. Then shift the array items one position ahead, then do the same for all the other elements next to pos. Because the location pos is now empty, insert the element x there. To learn more about data science please visit- Learnbay.co


What is the difference between an array shift and unsetting a variable in PHP?

By shifting the values in an array, you are moving a key's value to the previous key. The very first key's value is obliterated. By shifting all values in the array, all keys will have a value of NULL. Unsetting a variable is entirely different -- performing a variable unsetting causes the variable to have a value of NULL, as if it was never set.


Write a program to shift a 8-bit number left by two bits?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int n,i; clrscr(); printf("Enter the number on which left shift operation is to be performed: "); scanf("%d",&amp;n); printf("\nBefore shifting the number was: %d\n",n); i=n&lt;&lt;2; //LEFT SHIFT OPERATION printf("After shifting the number is: %d\n",i); getch(); }


How do you write a c program to shift the entered number by 3 bits left and also 5 bits right and display the result?

Input and output should be trivial; the core of it is: m = (n5Note: it is not the same as m = n>>2

Related questions

Write an algorithm to insert an item to a given location in sorted array?

To insert a number N into array A at index I: // Resize A if necessary If A is too small to add a new element then resize A // Right-shift all elements starting from position I For i = A.length to I A[i] = A[i - 1] // Insert new item A[I] = N


Write a program using 8086 instructions to double a number?

There isn't a reason to write a complete program to do this; in any assembly language just shift the value 1 bit to the left to double it.


How do you insert brackets in cells?

To insert a bracket in a cell, you just press the [ or ] key.If you are trying to enter braces { or } (usually Shift-[ and Shift-]) to indicate an array formula, you use a special procedure:Type your array formula in a cell.Press CTRL+SHIFT+ENTER.If you edit the formula, you need to press Press CTRL+SHIFT+ENTER again to let Excel know you want an array formula.


How do you write 0.000415 in scientific notation?

Shift four decimal places to the right from the starting position to get: 4.15 x 10-4


Insert an element after an element in an array?

To begin, obtain the element to be added, such as x Then, say pos, get the position where this element will be put. Then shift the array items one position ahead, then do the same for all the other elements next to pos. Because the location pos is now empty, insert the element x there. To learn more about data science please visit- Learnbay.co


How do you enter an array formula?

After you enter your formula, press CTRL+SHIFT+ENTER at the same time. You will see the array formula displayed with brackets ({}) around it. If you type the same text, including the brackets and press just ENTER, you will not tell Excel the formula is an array and may display only text. You need to enter the formula, without the brackets, and press CTRL+SHIFT+ENTER to let Excel know it is an array. See related links for more details about array formulas.


Write an assembly program to multiply a numberr by 4?

To multiply a number by 4, do a left shift twice. If the number is in the accumulator, do... RAL RAL XRI 0FCH


Can you wait a month to get retainers after braces or will your teeth shift to the position it was before?

get them asap they will shift


What is a stars shift in position called?

Motion


What is a stars apparent shift in position?

Motion


In Toyota rav4 2002 the doors lock when shift the gear to P position?

In Toyota rav4 2002 why the doors lock when shift the gear to P position?


How do you write a power of a number on the keyboard?

That really depends where you want to write it - in what program. For example, in Microsoft Word or Microsoft Excel, you can go to character format, and select "superscript". The shortcut key (at least in Word) is Ctrl-Shift-Plus.