阵列转移到下一个元件 [英] Array shifting to the next element
本文介绍了阵列转移到下一个元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以移动数组中的元素到下一个元素
例如:X [5] = {5,4,3,2,1}; //初始值
X [0] = 6;要移位//新值
X [5] = {6,5,4,3,2}; //偏移阵列,它需要被移动,
//不只是增加值。
这是我到目前为止已经完成。这是错误的,这就是为什么我需要帮助这里。先谢谢了。
的#include<&iostream的GT;使用命名空间std;诠释的main()
{
INT X [5] = {5,4,3,2,1}; INT ARRAY_SIZE = sizeof的(X)/ sizeof的(X [0]); X [0] = 6; INT M = 1; 对于(INT J = 0; J< ARRAY_SIZE; J ++){
X [M + J] = X [J]。
COUT<< X [J]<< ENDL;
} 返回0;
}
解决方案
的#include<&iostream的GT;
INT
main()的
{
INT X [5] = {5,4,3,2,1}; INT ARRAY_SIZE = sizeof的(X)/ sizeof的(X [0]); 为(中间体J = ARRAY_SIZE - 1; J&大于0; j--)
{
X [J] = X [J - 1];
} X [0] = 6; 对于(INT J = 0; J< ARRAY_SIZE; J ++)
{
性病::法院LT&;< X [J]。
} 返回0;
}
How can I move elements in an array to the next element
eg: x[5] = { 5, 4, 3, 2, 1 }; // initial values
x[0] = 6; // new values to be shifted
x[5] = { 6, 5, 4, 3, 2 }; // shifted array, it need to be shifted,
// not just increment the values.
This what I've done so far. It's wrong, that's why I need help here. Thanks in advance.
#include <iostream>
using namespace std;
int main()
{
int x[5] = { 5, 4, 3, 2, 1 };
int array_size = sizeof(x) / sizeof(x[0]);
x[0] = 6;
int m = 1;
for(int j = 0; j < array_size; j++) {
x[m+j] = x[j];
cout << x[j] << endl;
}
return 0;
}
解决方案
#include <iostream>
int
main ()
{
int x[5] = { 5, 4, 3, 2, 1 };
int array_size = sizeof (x) / sizeof (x[0]);
for (int j = array_size - 1; j > 0; j--)
{
x[j] = x[j - 1];
}
x[0] = 6;
for (int j = 0; j < array_size; j++)
{
std::cout << x[j];
}
return 0;
}
这篇关于阵列转移到下一个元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文