Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to find averages of columns and rows of an array from data in a txt file.

Status
Not open for further replies.

tashjade

Newbie level 1
Joined
May 5, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,299
Issues regarding coding to find the averages of column of an array which has been converted from a txt file. (below). Very inexperienced with C++.

SampleSet.txt
x y
1 3
2 4
3 5
4 8
5 11


#include <iostream> //For basic input and output of data.
#include <iomanip> //For manipulation of data output.
#include <conio.h> //?
#include <fstream> //For file output.
#include <string>
#include <cmath> //For math functions.
using namespace std;

int main ()
{

int SampleSet[5][2]; //An array with 5 rows and 2 columns
ifstream inFile ("SampleSet.txt");
int x = 0;
int y = 0;
if(!inFile) //Always test the file open.
{
cout << "Erroe opening output file" <<endl;
system ("pause");
return -1;
}

inFile >> SampleSet[x][y]
while(!inFile.eof())
{
for (int x = 0; x < 5; x++)
{
for(int y = 0; y < 2; y++)
{
SampleSet[x][y] =?
}
}
}
inFile.close();

inFile(5, SampleSet.txt)
cout << "Reading SamplesSet.txt...\n";
cout << "Number of elements =" << x << "\n";
cout << "Simple Linear Regression Model : y = 2* x +0.2\n";

//Calculate the simple linear regression model
{
//This Function calculates the average of each col, and row.
void calcAverage(int x, int y, float SampleSet[][5])
{



SampleSets[x-1][y-1] = 0;

for (int i = 0; i <x-1; i++)
{
float sum = 0;
for (int j = 0; j < y-1; j++)
{
sum += SampleSet[j];
}
numbers[x-1] = sum/(y-1);
}


for (int j = 0; y < y-1; j++)
{
float sum = 0;
for (int i = 0; i < x-1; i++)
{
sum += SampleSet[j];
}
SampleSet[x-1][j] = sum/(x-1);
}
}


float average(float a[],int n);

{

float average, sum = 0;
for (int i = 0; i < n; i++)
sum += a;
average = sum / n;
return 0;

}

void output(int x, int y, float SampleSet [][5])
{
cout << setprecision(2) << fixed << showpoint;
for(int i = 0;i < x;i++)
{
for(int j = 0;j < y;j++)
cout << setw(7) << SampleSet[j];
cout << endl;
}
}
}
 

this will read the data into your array
Code:
#include <iostream> //For basic input and output of data.
#include <iomanip> //For manipulation of data output.
#include <conio.h> //?
#include <fstream> //For file output.
#include <string>
#include <cmath> //For math functions.
using namespace std;

int main ()
{

int SampleSet[5][2]; //An array with 5 rows and 2 columns
ifstream inFile ("SampleSet.txt");
int x = 0;
int y = 0;
if(!inFile) //Always test the file open.
{
cout << "Erroe opening output file" <<endl;
//system ("pause");
return -1;
}

for (int x = 0; x < 5 && !inFile.eof(); x++)
{
for(int y = 0; y < 2; y++)
{
inFile >> SampleSet[x][y];
cout <<SampleSet[x][y] << " ";
}
cout << endl;
}

inFile.close();
}
you can now work on the rest
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top