正在回答
2回答
你好,循环打印数组建议参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int arr[10]; void showData( int a[], int length) { for ( int i = 0; i < length; i++) { cout<<a[i]<< " " ; } cout<<endl; } int main () { int shuzu[10]; for ( int i=0;i<9;i++) { cout<< "请输入第" <<(i+1)<< "个数据:" <<endl; int num; cin>>num; shuzu[i]=num; } showData(shuzu, sizeof (shuzu) / sizeof (shuzu[0])-1); } |
如果解决您的问题请采纳,作业其他不懂的地方建议提出具体的问题,祝学习愉快!
guly
2018-03-13 19:02:45
你好,建议参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; int arr[10]; void insertData() { int n; // 少接收一个数据,为在指定位置处插入数据做准备 for ( int i = 0; i < 9; i++) { cout<< "请输入第" <<i + 1<< "个数据:" ; cin>>n; if (n==0){ cout<< "数据不能为0,请重新输入" <<endl; i--; continue ; } arr[i]=n; } } void showData( int a[], int length) { for ( int i = 0; i < length; i++) { cout<<a[i]<< " " ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | } cout<<endl; } /** * 提示信息 */ void notice() { cout<< "********************************************" <<endl; cout<< " 1--插入数据" <<endl; cout<< " 2--显示所有数据" <<endl; cout<< " 3--在指定位置处插入数据" <<endl; cout<< " 4--查询能被3整除的数据" <<endl; cout<< " 0--退出" <<endl; cout<< "********************************************" <<endl; } int main( int argc, const char * argv[]) { |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | int input = 0; while ( true ) { notice(); cout<< "请输入对应的数字进行操作:" ; cin>>input; switch (input) { case 1: //插入数据 insertData(); //显示数据 cout<< "数组元素为:" <<endl; showData(arr, sizeof (arr) / sizeof (arr[0])-1); break ; } } return 0; } |
祝学习愉快!
C++零基础入门 热门编程语言 二级考试必备
- 参与学习 529 人
- 提交作业 110 份
- 解答问题 593 个
无论您是零基础、还是想晋升,亦或是想转型,C++无疑都是最佳选择。本路径共分为基础语法、指针与引用、面向对象三大模块,为你开启入门C++编程的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧