1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
//declaring global variables
double matrix[4][8];
double avg;
const int NumberOfColumns = 8;
const int NumberOfRows = 4;
double row, col;
void openFile(ifstream& avgfile, double matrix[][NumberOfColumns]);//prototypes
void averages(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns]);
void printMatrix(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns]);
int main()
{
ifstream avgfile;
openFile (avgfile, matrix);// calling function to open file
system ("pause");
return 0;
}
void openFile(ifstream& avgfile, double matrix[][NumberOfColumns])
{
string inputFile;//playing around with you inputing file name yourself =)
cout << "Enter the input file name:";
cin >> inputFile; //opens file
avgfile.open(inputFile);
if(!avgfile) //If not file or no file there then this will proceed
{
cout << "Input file opening failed."<< endl;
}
while(!avgfile.eof())//while not end of file
{
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
avgfile >> matrix[i][j]; //stupid attempt to pass information from file into array
}
}
}
averages(avgfile, matrix); //initializes function averages to calculate sum and average
}
void averages(ifstream& avgfile, double matrix[NumberOfRows][NumberOfColumns])//this function doesnt work at all
{
cout << fixed << showpoint;
cout << setprecision(2);
double sum=0;
double avg=0;
for (int row=0; row < NumberOfRows; row++){
for (int col = 0; col < NumberOfColumns; col++){
sum = sum + matrix[row][col];//
avg = sum / 7; //finding the average of the matrix
}
printMatrix(avgfile, matrix);
}
}
void printMatrix(ifstream& avgfile, double matrix[][NumberOfColumns])
{
cout << fixed << showpoint;
cout << setprecision(2);
//Next line is an attempt at making a 1d array to display this information
string Days[9]= {"Name", "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Averages"};
cout << Days;
cout << endl;
cout << matrix[4][8];
}
| |