PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 22, 2022

[FIXED] How can I solve the error -- error: invalid types ‘int[int]’ for array subscript?

 December 22, 2022     arrays, c++, error-handling, syntax     No comments   

Issue

#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;
}

Hi there, I have written this code to calculate the average of the elements of an 2D array. I am getting error while trying to access the array elements of a 2D array at line 12 -13 ar[i][j]

The error says- error: invalid types ‘int[int]’ for array subscript

How can I solve this error?

PS: I want to give row( no. of rows in 2D array) and col(no. of columns in 2D array) in the function parameter to make this more dynamic.


Solution

Your function parameter ar is a int*. But when you wrote sum+=ar[i][j] you're subscripting it as if we had a 2D array. You can only subscript it for one dimension like arr[i].

Additionally, row and col are not constant expressions. And in Standard C++ the size of an array must be a compile time constant(constant expression). So,

int ar[row][col]; //this statement is not standard c++

The above statement is not standard c++.

A better way(to avoid these problems) would be to use a 2D std::vector instead of a 2D array as shown below.

#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;
}

The output of the above program can be seen here.



Answered By - Jason Liam
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing