How To Shift Array Elements to right and replace the shifted index with string in Visual C++
VISUAL C++ Question
Hi,
I have array of 3 elements and I want to shift its elements to the right and replace the shifted index cell with "SHIFTED" string and this should loop until all the cells has "SHIFTED" string.
For example:
int a[x]={0,1,2};
Initial index and elements Order:
[0]=0
[1]=1
[2]=2
should become in the:
1st loop:
[0]=SHIFTED
[1]=0
[2]=1
2nd Loop:
[0]=SHIFTED
[1]=SHIFTED
[2]=0
3rd Loop:
[0]=SHIFTED
[1]=SHIFTED
[2]=SHIFTED
I know I can do that with memmove() but I don't want to use any function in it.
Would you please help me; here is my work:
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int const ARRAY_SIZE=3;
int main()
{
int Array[ARRAY_SIZE];
int iniPostion,newPostion;
string x="SHIFTED";
for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++)
{
Array[iniPostion] = iniPostion;
cout <&开发者_如何学Clt; "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
}
cout << endl;
for(newPostion=0; newPostion<ARRAY_SIZE; newPostion++)
{
Array[newPostion]=newPostion;
cout << "Cell [" << newPostion << "] New Element is: (";
if(Array[newPostion-1]<0)
{
cout << x << ")\n";
}
else
{
cout << Array[newPostion-1] << ")" << endl;
}
}
return 0;
}
As Peter mentioned in his answer, you cannot assign a string to a int. I've assumed SHIFTED
to be -1
. So every time you shift you bring in a -1
in the gap created.
You need two loops. The outer loops iterates N
(3) times and inner loop starts at the end of the array and copies (n-1)th
element to nth
position:
for(int count = 0;count < N;count++){
for(newPostion=ARRAY_SIZE-1; newPostion > count;newPostion--)
Array[newPostion]=Array[newPostion - 1]; // copy
Array[newPostion]= -1; // fill the gap.
// print.
for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++) {
cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
}
cout<<endl;
}
Sample run:
# g++ a.cpp && ./a.out
Cell [0] Initial Element is: (0)
Cell [1] Initial Element is: (1)
Cell [2] Initial Element is: (2)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (0)
Cell [2] Initial Element is: (1)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (0)
Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (-1)
It is a bit unclear, how you can expect to have string ("SHIFTED") in an array of integers.
However, for this operation you can use the rotate algorithm:
#include <iostream>
#include <algorithm>
#include <string>
int const ARRAY_SIZE=3;
void print(std::string* array) {
for (int i = 0; i != ARRAY_SIZE; ++i) {
std::cout << array[i] << ' ';
}
std::cout << '\n';
}
int main()
{
std::string Array[ARRAY_SIZE] = {"0", "1", "2"};
print(Array);
//the last item ends up at the beginning of the array
std::rotate(Array, Array + ARRAY_SIZE - 1, Array + ARRAY_SIZE);
//now overwrite the item that used to be last
Array[0] = "SHIFTED";
print(Array);
return 0;
}
It would probably be simpler and more efficient with a suitable container, such as std::deque
or std::list
where you could pop_back
the last value and push_front
the new value.
I have array of 3 elements and I want to shift its elements to the right and replace the shifted index cell with "SHIFTED" string and this should loop until all the cells has "SHIFTED" string.
This doesn't make sense at all. You have an array of numbers (integers), which of course can't contain a string. What you can do is e.g. inserting 0 or -1 to denote the shifted element.
The shifting itself can be easily implemented through a std::rotate operation.
But since all elements contain the same thing in the end, why don't you just assign them directly without all the shifting?
Here is my simple solution in plain old C
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {
int MAX_LEN = 11;
int numOfShifts = 1;
if ( argc > 1 ) {
numOfShifts = atoi(argv[1]);
}
printf("Number of shifts = %d\n",numOfShifts);
int arr[] = { 0,1,2,3,4,5,6,7,8,9,10 };
int i;
int n; // number of shifts index
for ( n = 0; n < numOfShifts; n++ ) {
for ( i = MAX_LEN - 1; i >= 0; i-- ) {
if ( i == 0 ) {
arr[i] = -1;
} else {
arr[i] = arr[i-1];
}
}
}
// output
for( i = 0; i < MAX_LEN; i++ ) {
printf("Arr[%d] = %d\n", i, arr[i]);
}
}
This reeks of homework...
Is there a reason you can't just keep track of how many shifts you've done and just take that into account when you print?
#include <iostream>
int const ARRAY_SIZE=3;
class Shifter
{
public:
Shifter(const int* array_, size_t size_)
: array(array_), size(size_), shifts(0) {}
void Shift(size_t steps = 1) { shifts += steps; if(shifts > size) shifts = size; }
void Print(std::ostream& os) const;
private:
const int* array;
size_t size;
size_t shifts;
};
void Shifter::Print(std::ostream& os) const
{
for(size_t i = 0; i < size; ++i)
{
os << "Cell [" << i << "] = ";
if(i < shifts)
os << "SHIFTED";
else
os << array[i - shifts];
os << std::endl;
}
}
std::ostream& operator <<(std::ostream& os, const Shifter& sh)
{
sh.Print(os);
return os;
}
int main(void)
{
// Initialize the array.
int a[ARRAY_SIZE];
for(size_t iniPostion=0; iniPostion < ARRAY_SIZE; iniPostion++)
a[iniPostion] = iniPostion;
Shifter sh(a, ARRAY_SIZE);
std::cout << "Initial contents:" << std::endl;
std::cout << sh << std::endl;
// Do the shifts.
for(size_t newPostion = 0; newPostion < ARRAY_SIZE; newPostion++)
{
std::cout << "Shift by 1..." << std::endl;
sh.Shift();
std::cout << sh << std::endl;
}
return 0;
}
You can simple move memory.
// shift down
int a[ 10 ];
memmove( a, a +1, sizeof( a ) -sizeof( a[ 0 ] ) );
精彩评论