I am having problems with loading my data file into a 2D vector, and then take the average of the elements inside of the vector. I do not know much about 2D vectors can I get some help please?
Here is my code:
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<vector>
#include<iostream>
using namespace std;
ifstream indata;
void average_row(vector<vector<double> > vec,vector<double>& r_avg);
int main(){
double grade;
vector<double> r_avg;
vector<vector<double> > vec;
average_row(vec,r_avg);
return 0;
}
void average_row(vector<vector<double> > vec, vector<double>& r_avg){
double grade, sum=0;
string filename;
cout<<"Enter the name of the file ";
cin>>filename;
ifstream indata;
indata.open(filename.c_str());
if(indata.is_open()){
cerr<<"File has opened"<<endl;
}
while(indata>>grade){
vec.push_back(grade);
}
indata.close();
for (int i=0; i<vec.size(); i++){
for (int j=0; j<vec.size(); i++){
indata>>vec[i][j];
sum=sum+vec[i][j];
}
r_avg=sum/(vec.size());
cout<<"The average is "<<r_avg<<endl;
}
}