当前位置: 代码迷 >> 综合 >> c++访问数组元素
  详细解决方案

c++访问数组元素

热度:105   发布时间:2023-09-27 15:10:31.0

数组元素可以通过数组名称加索引进行访问,元素的索引是放在方括号内,跟在数组名称的后边,例如:

double salary=balance[9];

上面的语句将把数组中第十个元素的值赋给salary变量,下面的实例使用了上述的三个概念,即声明数组,数组赋值,访问数组

#include <iostream>

using namespace std;

#include <iomanip>

using std::setw;

int main()

{

int n[10]

for (int i=0;i<10;i++)

{

n[i]=i+100;

}

cout<<"element"<<setw(13)<<"Value"<<endl;

for (int j=0;j<10;j++)

{

cout<<setw(7)<<j<<setw(13)<<n[j]<<endl;

}

return 0;

}

上面的程序使用了setw()函数来格式化输出,当上面的代码被编译和执行时,他会如列表般打印出来

  相关解决方案