Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will have to write two programs for this project. The first will ask the use

ID: 3626078 • Letter: Y

Question

You will have to write two programs for this project.
The first will ask the user for the number of rows and columns in a two-dimensional array; the number of each should be between two and fifty for each. The program will then fill the two-dimensional array with different random numbers. The program will then ask the user to provide a file name; this file name will be used to open a file, for output with this name. The program should write the dimensions of the matrix to the first line of the file, and then fill the rest of the lines of the file with random numbers. Store the random numbers in ASCII by using the appropriate conversion function. Use a tab as a delimiter between each number in a line. Put the numbers in the file with the same dimensions the user entered, that is each line should have x numbers in it, where x is the number of columns in the input matrix. There should be y+1 lines, where y is the number of rows in the matrix. The extra line is the first line that has the dimensions of the matrix in it, this line has only two numbers, x and y.
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

part 2

#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;
     }

}

--------------------------------------

part 1

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int getSize(int,int,string);
void fillArray(int[][50],int,int);
void outArray(int[][50],int,int);
int main()
{

int rows,cols,i,j;
int a[50][50];
rows=getSize(2,50,"rows");
cols=getSize(2,50,"columns");
fillArray(a,rows,cols);
outArray(a,rows,cols);
return 0;
}
int getSize(int l,int h,string mess)
{int n;
cout<<"Enter number of "<<mess<<": ";
cin>>n;
while(n<l||n>h)
     {cout<<"must be between "<<l<<" and "<<h<<endl;
      cout<<"Enter number of "<<mess<<": ";
      cin>>n;
      }
return n;
}
void fillArray(int a[][50],int r,int c)
{srand(time(0));
int i,j;
for(i=0;i<r;i++)
    for(j=0;j<c;j++)
         a[i][j]=rand()%100+1;    //random numbers are between 1 and 100;
}
void outArray(int a[][50],int r,int c)
{int i,j;
char filename[50];
ofstream out;
char num[20];
cout<<"Enter the file name to output to: ";
cin>>filename;
out.open(filename);
out<<r<<" "<<c<<endl;
for(i=0;i<r;i++)
   {for(j=0;j<c;j++)
        out<<itoa(a[i][j],num ,10)<<" ";
     out<<endl;
     }
out.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote