提问:编写程序,创建包含10个整数的数组,使用循环语句为数组赋值,输出其中最大值及对应的元素下标。
网友回答:
程序有多个错,以下是改好的及说明
#include <iostream> using namespace std; int main() { int i; // = 0; i不用初始化的 int maxindex; int Array[10]; //10个整形数组,你的是字符数组 cout << "输入10个数:"; // cin.get(chArray, 10); for (i=0;i<10;i++) //要求使用循环语句为数组赋值 cin >> Array[i]; //整形数组输入要用循环的 //} 大括号位置不对 // char maxindex = 0; 上面已有定义,只要赋值了 maxindex=0; for(i = 1; i < 10; i++) { //从1开始就可以了 if(Array[maxindex] < Array[i]) { //条件完全不对。[maxindex]没有意义,另外哪来的char[i]? maxindex = i; } } // 大括号位置不对 //输出也不对,哪来的input? cout << "最大值为:" <<Array[maxindex] << ",下标为:" <<maxindex << endl; }