1

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;
}
Community
  • 1
  • 1
JohnM
  • 13
  • 3

2 Answers2

0

You forgot to set sum to 0 before the second for-loop. In UPDATE 2 you still don't set sum to 0!?

after the first 'for' and before the second ... before you start adding the sums for a column ...

like this

for (int y = 0; y < col; y++)
{
    sum = 0;
    for (int x = 0; x < row; x++)
    {
        sum = sum + Matrix[x][y];
    }

    average = sum / row;
    cout << average << " for column " << y << "\n";
}
  • Thanks. Just found that out. But it still wont calculate the average for the columns. Trying to see what else I did wrong. – JohnM Dec 07 '15 at 23:09
0

You gave this:

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];

}

In this part of your code, it appears that you are trying to calculate the average at every row in every column (because you put average = sum / x; inside both of the for loops.) I would recommend calculating the sum of the entire column before calculating the average, in order to save code/time. Furthermore, you also put average = AvgArray[y];. Assuming you're trying to fill this array with the averages for each column, you will want to assign average to AvgArray[y] instead. Currently, you never assign any values to AvgArray[].

cout << average << "\n";

Like in the code Zsolt Marx gave, you will want to put this line inside the larger of the two for loops. Otherwise, if you leave it the way you have it, the code will only display the average for the last column calculated.

TheTrueJard
  • 391
  • 3
  • 14