c – 数组移位到下一个元素

c – 数组移位到下一个元素,第1张

概述如何将数组中的元素移动到下一个元素 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, 如何将数组中的元素移动到下一个元素
eg: x[5] = { 5,4,3,2,1 }; // initial values    x[0] = 6; // new values to be shifted    x[5] = { 6,5,2 }; // shifted array,it need to be shifted,// not just increment the values.

这就是我到目前为止所做的.这是错的,这就是我在这里需要帮助的原因.提前致谢.

#include <iostream>using namespace std;int main() {  int x[5] = { 5,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,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;}
总结

以上是内存溢出为你收集整理的c – 数组移位到下一个元素全部内容,希望文章能够帮你解决c – 数组移位到下一个元素所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1237550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存