How can I solve the error -- error: invalid types ‘int[int]’ for array subscript?
How can I solve the error -- error: invalid types ‘int[int]’ for array subscript?
#include <iostream>
#include <iomanip>
using namespace std;
int col=10;
int row=0;
void avg(int * ar,int row, int col)
{
float size= row * col;
int sum=0, ave;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++){
sum+=ar[i][j];
cout<<sum;}
}
ave=sum/size;
cout<<sum<<endl;
cout<<ave;
}
int main()
{
int row, col;
cout<<"How many rows does the 2D array have: ";
cin>>row;
cout<<"How many columns does the 2D array have: ";
cin>>col;
int ar[row][col];
cout<<"Enter the 2D array elements below : \n";
for(int i=0; i<row; i++){
cout<<"For row "<<i + 1<<" : \n";
for(int j=0; j<col; j++)
cin>>ar[i][j];
}
cout<<"\n Array is: \n";
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<setw(6)<<ar[i][j];
cout<<endl;
}
cout<<"\nAverage of all the elements of the given D array is: \n";
avg((int*)ar,row,col);
return 0;
}
您好,我编写了这段代码来计算二维数组元素的平均值。我在第 12 -13 行 ar[i][j]
尝试访问二维数组的数组元素时出错
错误说-错误:数组下标的无效类型“int[int]”
如何解决这个错误?
PS: 我想在函数参数中给出行(二维数组中的行数)和列(二维数组中的列数)以使其更具动态性。
您 运行 陷入问题的原因是因为您试图 动态地 分配二维数组(而不是 静态地 ).
静态分配是指在偶数运行之前指定[row]和[column]的值。
示例:int ar[35][35]
这将在编译时创建一个 35x35 二维数组。
动态分配意味着在运行时间内(编译后,运行宁)改变[行]和[列]的值。这将需要使用 C++ 中的 new
语句在函数 avg
的开头创建指定大小的新数组。 C++中数组动态分配的资料很多,比如这个posthere
你的函数参数 ar
是一个 int*
。但是当你写 sum+=ar[i][j]
时,你就像我们有一个二维数组一样对其进行下标。您只能为一维下标,例如 arr[i]
.
另外、row
和col
是不是常量表达式。在标准 C++ 中,数组的大小必须是编译时常量(常量表达式)。所以,
int ar[row][col]; //this statement is not standard c++
上面的语句不是标准的c++.
更好的方法(避免这些问题)是使用二维 std::vector
而不是二维数组,如下所示。
#include <iostream>
#include <iomanip>
#include <vector>
//this function takes a 2D vector by reference and returns a double value
double avg(const std::vector<std::vector<int>> &arr)
{
int sum=0;
for(const std::vector<int> &tempRow: arr)
{
for(const int &tempCol: tempRow){
sum+=tempCol;
//std::cout<<sum;
}
}
return (static_cast<double>(sum)/(arr.at(0).size() * arr.size()));
}
int main()
{
int row, col;
std::cout<<"How many rows does the 2D array have: ";
std::cin>>row;
std::cout<<"How many columns does the 2D array have: ";
std::cin>>col;
//create a 2D vector instead of array
std::vector<std::vector<int>> ar(row, std::vector<int>(col));
std::cout<<"Enter the 2D array elements below : \n";
for(auto &tempRow: ar){
for(auto &tempCol: tempRow){
std::cin>>tempCol;
}
}
std::cout<<"\n Array is: \n";
for(auto &tempRow: ar)
{
for(auto &tempCol: tempRow)
std::cout<<std::setw(6)<<tempCol;
std::cout<<std::endl;
}
std::cout<<"\nAverage of all the elements of the given D array is: \n";
std::cout<<avg(ar);
return 0;
}
上面程序的输出可见here.
#include <iostream>
#include <iomanip>
using namespace std;
int col=10;
int row=0;
void avg(int * ar,int row, int col)
{
float size= row * col;
int sum=0, ave;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++){
sum+=ar[i][j];
cout<<sum;}
}
ave=sum/size;
cout<<sum<<endl;
cout<<ave;
}
int main()
{
int row, col;
cout<<"How many rows does the 2D array have: ";
cin>>row;
cout<<"How many columns does the 2D array have: ";
cin>>col;
int ar[row][col];
cout<<"Enter the 2D array elements below : \n";
for(int i=0; i<row; i++){
cout<<"For row "<<i + 1<<" : \n";
for(int j=0; j<col; j++)
cin>>ar[i][j];
}
cout<<"\n Array is: \n";
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<setw(6)<<ar[i][j];
cout<<endl;
}
cout<<"\nAverage of all the elements of the given D array is: \n";
avg((int*)ar,row,col);
return 0;
}
您好,我编写了这段代码来计算二维数组元素的平均值。我在第 12 -13 行 ar[i][j]
尝试访问二维数组的数组元素时出错错误说-错误:数组下标的无效类型“int[int]”
如何解决这个错误?
PS: 我想在函数参数中给出行(二维数组中的行数)和列(二维数组中的列数)以使其更具动态性。
您 运行 陷入问题的原因是因为您试图 动态地 分配二维数组(而不是 静态地 ).
静态分配是指在偶数运行之前指定[row]和[column]的值。
示例:int ar[35][35]
这将在编译时创建一个 35x35 二维数组。
动态分配意味着在运行时间内(编译后,运行宁)改变[行]和[列]的值。这将需要使用 C++ 中的 new
语句在函数 avg
的开头创建指定大小的新数组。 C++中数组动态分配的资料很多,比如这个posthere
你的函数参数 ar
是一个 int*
。但是当你写 sum+=ar[i][j]
时,你就像我们有一个二维数组一样对其进行下标。您只能为一维下标,例如 arr[i]
.
另外、row
和col
是不是常量表达式。在标准 C++ 中,数组的大小必须是编译时常量(常量表达式)。所以,
int ar[row][col]; //this statement is not standard c++
上面的语句不是标准的c++.
更好的方法(避免这些问题)是使用二维 std::vector
而不是二维数组,如下所示。
#include <iostream>
#include <iomanip>
#include <vector>
//this function takes a 2D vector by reference and returns a double value
double avg(const std::vector<std::vector<int>> &arr)
{
int sum=0;
for(const std::vector<int> &tempRow: arr)
{
for(const int &tempCol: tempRow){
sum+=tempCol;
//std::cout<<sum;
}
}
return (static_cast<double>(sum)/(arr.at(0).size() * arr.size()));
}
int main()
{
int row, col;
std::cout<<"How many rows does the 2D array have: ";
std::cin>>row;
std::cout<<"How many columns does the 2D array have: ";
std::cin>>col;
//create a 2D vector instead of array
std::vector<std::vector<int>> ar(row, std::vector<int>(col));
std::cout<<"Enter the 2D array elements below : \n";
for(auto &tempRow: ar){
for(auto &tempCol: tempRow){
std::cin>>tempCol;
}
}
std::cout<<"\n Array is: \n";
for(auto &tempRow: ar)
{
for(auto &tempCol: tempRow)
std::cout<<std::setw(6)<<tempCol;
std::cout<<std::endl;
}
std::cout<<"\nAverage of all the elements of the given D array is: \n";
std::cout<<avg(ar);
return 0;
}
上面程序的输出可见here.