Create a C++ program that will (in the main function): -declare 2 arrays that ca
ID: 3693176 • Letter: C
Question
Create a C++ program that will (in the main function):
-declare 2 arrays that can each store 20 double type values
-declare the variables necessary to store a file name (string) and represent a file (ifstream)
interactively prompt the user for the name of an input file and read the file name
open the file (using an ifstream type variable to represent the file)
read the content of the file (20 double type values) into one of the arrays
close the input file
call a void function that will be passed the 2 arrays (the array with the input values and the array that was not initialized) and the size of the arrays (int)
using the values in the input array, the function should do the following
double the value of each of the first 10 elements and store them in the 11th-20th locations of the second array
determine the cube root of the values in the 11th-20th locations of the input array and store those values in the 1st-10th locations of the second array
print (to the screen) the content of both arrays (in 2 right justified columns with headings)
return to main
call a function to sum all the values in the input array and then call the function again to sum the first 10 values in the second array
the function should be designed to receive 2 parameters, an array of doubles and an integer, n
the function will add the first n values in the array and return the result
print the sum of all the values in the input array with a label
print the sum of the first 10 values in the second array with a label
REQUIREMENTS
Program must read input from a file using a filestream.
All header files referenced must be included.
No global variables may be used.
The void function must set the values for the 2nd array as described above and print the content of the 2 arrays in right justified columns (see example below) with 5 digits to the right of the decimal.
Only one value returning function can be implemented. It must be called twice.
ASSUMPTIONS
The input file will contain 20 double type values that are >= 0.0.
Each value will be separated by blanks or linefeeds.
The maximum value that will have to be displayed by the void function is 9999.99999.
Sample terminal session:
[key]$ more data4eleven
0 13 5 21 9 7 11 20 1 15
27 4 125 1934 8 852 64 134 0 216
[key]$ g++ exercise11.cpp
[key]$ ./a.out
Please enter name of input file
data4eleven
Input Array 2nd Array
0.00000 3.00000
13.00000 1.58740
5.00000 5.00000
21.00000 12.45907
9.00000 2.00000
7.00000 9.48011
11.00000 4.00000
20.00000 5.11723
1.00000 0.00000
15.00000 6.00000
27.00000 0.00000
4.00000 26.00000
125.00000 10.00000
1934.00000 42.00000
8.00000 18.00000
852.00000 14.00000
64.00000 22.00000
134.00000 40.00000
0.00000 2.00000
216.00000 30.00000
sum of input values = 3466.00000
sum of 1st 10 values in changed array = 48.64380
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
void process(double inputArray[],double outputArray[],int size){
for(int i = 0;i<10;i++){
outputArray[i+10] = 2*inputArray[i];
outputArray[i] = cbrt(inputArray[i+10]);
}
for(int i = 0;i<20;i++){
cout.width(10);
cout<<fixed<<setprecision(5)<<right<<inputArray[i]<<" ";
cout.width(10);
cout<<right<<outputArray[i]<<endl;
}
return;
}
double sum(double Array[],int n){
double result =0;
for(int i = 0;i<n;i++)
result += Array[i];
return result;
}
int main(){
double inputArray[20],outputArray[20];
cout<<"Enter filename";
string fileName;
getline(cin, fileName);
ifstream file(fileName.c_str());
for(int i=0;i<20;i++)
file>>inputArray[i];
process(inputArray,outputArray,20);
cout<< "sum of input values = "<<sum(inputArray,20)<<endl;
cout<< "sum of input values = "<<sum(outputArray,10);
return 0;
}