USISNG ONLY CODE BLOCKS C++ , Please write the program bellow: Please use the SH
ID: 3675970 • Letter: U
Question
USISNG ONLY CODE BLOCKS C++ , Please write the program bellow:
Please use the SHELL at the end of this page......
Write a program to read N data items into two arrays, X and Y, of size 30. Store the product of the corresponding pairs of elements of X and Y in a third array Z, also of size 30. Print a three column table that displays the arrays X, Y, and Z. Then compute and print the square root of the sum of the items in array Z. Compute and print the average of the values in array Z and print all values above the average of array Z. Determine the smallest value in each array using only one function.
Use the two data files named DATAX.TXT and DATAY.TXT.
You must use functions for the reading of the data, computing the average, printing the three column table and printing the values above average.
Demonstrate your output in lab.
Submit your source code and algorithm or flowchart in blackboard.
All values must be clearly labeled in the output.
You are required to demonstrate your output in Lab.
Documentation will be 20% of your grade.
Your source code must contain the following documentation.
Header information: (Source Code and Output)
Your Name, course & section number, assignment number and due date.
A brief description of your assignment.
Variable dictionary: A brief description of every variable used in your program.
Comments related to input and output of data and all formulas used in your code.
----------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
void readdata(ifstream &fx, int abc[], int &ct);
int main()
{ int x[30] = {0}, y[30] = {0};
int ctx = 0, cty =0;
ifstream f1 ("h:\datax.txt",ios::in);
ifstream f2 ("h:\datay.txt",ios::in);
readdata(f1,x,ctx);
readdata(f2,y,cty);
f1.close();
f2.close();
return 0;
}
void readdata(ifstream &fx, int abc[], int &ct)
{
ct = 0; cout<<" ";
while (fx>>abc[ct], !fx.eof())
{ cout<< abc[ct]<<" ";
ct++;
}
cout << " ";
}
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int readdata(ifstream &fx, int abc[], int &ct);
int arrayavg(ifstream &fx, int abc[], int &ct);
int min(ifstream &fx, int abc[], int &ct);
int main()
{
int x[30] = {0}, y[30] = {0},z[30][30];
int ctx = 0, cty =0,i,j,avgx,avgy,minx,miny;
ifstream f1 ("h:\datax.txt",ios::in);
ifstream f2 ("h:\datay.txt",ios::in);
ctx= readdata(f1,x,ctx);
cty= readdata(f2,y,cty);
f1.close();
f2.close();
return 0;
for(i=0;i<ctx;i++)
{
for(j=0;j<cty;j++)
z[i][j]=(x[i],y[j]);
}
for(i=0;i<ctx;i++)
for(j=0;j<cty;j++)
cout<<z[i][j];
avgx= arrayavg(f1,x,ctx);
avgy= arrayavg(f2,y,cty);
cout<<"Average of array Z is ("<<avgx<<","<<avgy<<")";
minx=min(f1,x,ctx);
miny=min(f2,y,cty);
cout<<"Smallest value in array X: "<<minx;
cout<<"Smallest value in array Y: "<<miny;
cout<<"Smallest value in array Z: ("<<minx<<","<<miny<<")";
}
int readdata(ifstream &fx, int abc[], int &ct)
{
ct = 0; cout<<" ";
while (fx>>abc[ct], !fx.eof())
{ cout<< abc[ct]<<" ";
ct++;
}
cout << " ";
return(ct);
}
int arrayavg(ifstream &fx, int x[], int &ct)
{
int sum=0,avg;
for(int i=0; i<ct; i++)
sum+=x[i];
avg= sum/ct;
return(avg);
}
int min(ifstream &fx, int x[], int &ct)
{
int min;
for(int i=0; i<ct; i++)
if(x[i]>x[i+1])
min=x[i+1];
else
min=x[i];
return(min);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.