Earlier I asked how to export a 2D array with random numbers as the data.
Link: Converting 2D array to text using c++ functions
Now I'm trying to write a separate program that can calculate the average of each column of that array.
But now I'm having issues with "uninitialized variables" that I'm pretty sure are initialized.
Not really sure what to do from here. Any help would be appreciated!
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
string Data, FileName;
int row, col, x, y;
float Matrix[50][50], sum, average;
cout << "Enter the name you gave the matrix file.\n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)\n";
cin >> FileName;
FileName = "C:\\Users\\Public\\Documents\\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
row=0;
while(!Fin.eof())
{
getline(Fin, Data);
stringstream ss(Data);
col=0;
while(ss >> Matrix[row][col])
{
col++;
}
row++;
}
Fin.close();
}
else
cout << "Unable to open file";
for (int y = 0; y < col; y++)
{
for (int x = 0; x < row; x++)
{
sum = sum + Matrix[x][y];
}
average = sum / row;
cout << average << " for column " << col << "\n";
}
system("pause");
return 0;
}
UPDATE: Solved the "uninitialized variables" error.
But now get "-nan(ind)" when I try to calculate the average.
Here's the new code...
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
string Data, FileName;
int row, col;
float Matrix[50][50], sum, average;
sum = 0;
cout << "Enter the name you gave the matrix file.\n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)\n";
cin >> FileName;
FileName = "C:\\Users\\Public\\Documents\\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
row=0;
while(!Fin.eof())
{
getline(Fin, Data);
stringstream ss(Data);
col=0;
while(ss >> Matrix[row][col])
{
col++;
}
row++;
}
Fin.close();
}
else
cout << "Unable to open file";
for (int y = 0; y < row; y++)
{
for (int x = 0; x < col; x++)
{
sum = sum + Matrix[x][y];
}
average = sum / col;
cout << average << "\n";
}
system("pause");
return 0;
}
UPDATE 2: All I can seem to get is the average for the first column. Can't really work out how to repeat this step. I've tried using do and for loops, but this got me a bunch of errors and losing the only average I get.
If anyone want's to take a look, be warned its very messy...
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
string Data, FileName;
int row, col;
float Matrix[50][50], sum, average;
sum = 0;
cout << "Enter the name you gave the matrix file.\n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)\n";
cin >> FileName;
FileName = "C:\\Users\\Public\\Documents\\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
row=0;
while(!Fin.eof())
{
getline(Fin, Data);
stringstream ss(Data);
col=0;
while(ss >> Matrix[row][col])
{
col++;
}
row++;
}
Fin.close();
}
else
cout << "Unable to open file";
double AvgArray[50];
for (int y = 0; y < 50; y++)
{
for (int x = 1; x < 50; x++)
{
if (Matrix[x][y]<0)
{
break;
}
sum = sum + Matrix[x][y];
average = sum / x;
}
if (Matrix[y][y]<0)
{
break;
}
average = AvgArray[y];
}
cout << average << "\n";
system("pause");
return 0;
}