So this is the 2nd part of the program, please do this only if you are done with
ID: 3626080 • Letter: S
Question
So this is the 2nd part of the program, please do this only if you are done with the 1st part, I put one program in 2 parts so you can get more karma points seperately for each. =)The second program will use the file you just created with the first program. It will ask the user for input file name, read the file into a matrix, and find the average of each column of the matrix. You have to convert the ASCII back to numbers first. There averages should be stored in a one-dimensional array. The program should print the average of each column in order.
Use functions to:
1. Fill the two-dimensional array with random numbers in program 1.
2. Write the matrix out to the file in program 1.
3. Read the file into the matrix in program 2.
4. Find the average of each column.
5. Print the average of each column.
Thank you so much! =)
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void getSize(int&,int&,ifstream&);
void fillArray(int[][50],int,int,ifstream&);
void outArray(int[][50],int,int);
double getaverages(int[][50],int,int);
void printaverages(double[],int);
int main()
{
int rows,cols,i;
int a[50][50];
double avg[50];
char filename[50];
ifstream in;
char num[20];
cout<<"Enter the file name to input from: ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
getSize(rows,cols,in);
fillArray(a,rows,cols,in);
outArray(a,rows,cols);
for(i=0;i<cols;i++)
avg[i]=getaverages(a,rows,i);
printaverages(avg,cols);
in.close();
system("pause");
return 0;
}
void printaverages(double a[],int c)
{int i;
cout<<"Averages Column Average ";
for(i=0;i<c;i++)
cout<<i<<" "<<a[i]<<endl;
}
double getaverages(int a[][50],int r,int c)
{int sum=0;
int i;
for(i=0;i<r;i++)
sum+=a[i][c];
return sum/(double)r;
}
void getSize(int& r,int &c,ifstream &in)
{in>>r;
in>>c;
}
void fillArray(int a[][50],int r,int c,ifstream &in)
{
int i,j;
char data[20];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{in>>data;
a[i][j]=atoi(data);
}
}
void outArray(int a[][50],int r,int c)
{int i,j;
cout<<"The array ";
for(i=0;i<r;i++)
{for(j=0;j<c;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.