Hello, I am working on a array with 7 columns and 8 rows and I am attempting to calculate the average of each row individually and also each column individually. So far I am able to get the titles to appear but each calculation is way above what it should be. Would anyone happen to have any advice or anything to fix my issue? Thanks ahead of time!
#include<iostream> //required for cin, cout
#include<cstdlib>
#include<fstream> //required for ifstream, ofstream
#include<string> //required for string characters
#include<cmath> //reqired for math operators
using namespace std;
int main()
{
const int NROWS{ 7 };
const int NCOLS{ 8 };
double plant[NROWS][NCOLS], sum, avg;
string filename;
ifstream data1;
cout << "Enter input name of file.\n";
cin >> filename;
data1.open(filename);
if (data1.fail())
{
cerr << "Error opening data file\n";
exit(1);
}
for (int i = 0; i < 7; i++)
{
sum = 0;
for (int j = 0; j < 1; j++)
{
sum =sum + plant[i][j];
}
avg = sum / 7;
Yeah, I have the data saved on a file called plant.dat then when I run the program I am able to enter the file name in and the program collects the data from the file. It may not be reading it correctly though.
I thought the lines with the " for(int j=0;j<8;j++) and for (int i=0;i<7;i++)" were taking data from the file. Sorry I am still new to the c++ language.