Hello,
Could you provide the code to perform the following task below.
declare 2 arrays that can each store a maximum of 20 double type values
interactively prompt the user for the name of an input file and read the file name
read the content of the file
o first value in the file will be an integer, n, that is greater than 0 and less than or equal
to 20
o n indicates how many values are to be stored in one of the arrays
o read the n values and store them into one of the arrays
double the value of each of the elements in the input array that have even
subscripts (0,2,4,...) and store them in the corresponding locations of the
second array
§ determine the cube root of each of the values in the input array that have odd
subscripts (1,3,5,...) and store those values in the corresponding 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 value-returning function to sum all the values in the input array and then call the
function again to sum the first int(n/2) values in the second array
o the function should be designed to receive 2 parameters, an array of doubles and an
integer, size
o the function will add the first size 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 int(n/2) values in the second array with a label
sample of the type of data it might read.
11
0.0 13.0 27.0 -27.0 -9.0 5.0 11.0 1.0 21.0 -1.0 16.0
|
Expected output to look like.
Input Array 2nd Array
0.00000 0.00000
13.00000 2.35133
27..00000 54.00000
-27.00000 -3.00000
-9.00000 -18.00000
5.00000 1.70000
11.00000 22.00000
1.00000 1.00000
21.00000 42.00000
-1.00000 -1.00000
16.00000 32.00000
Sum of all Values in array 1: 57.00000
Sum of first 5 values in array 2: 35.35133
|
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
// sample code not tested, need your help.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAX = 20;
int main() {
double table01Odds[MAX]
double table02Even[MAX]
ifstream infile; //infile represents the input file
string fname; //actual name of file
ofstream outfile;
int n;
//prompt for name of input file to be opened
cout << “Please enter name of input file” << endl;
cin >> fname; //read name of file to open
//open input file for reading
infile.open(fname.c_str());
/* c_str() is a function that converts a string
variable into a null-terminated array of characters,
which is the type of parameter the open function
expects */
if (infile.is_open())
//infile successfully opened
else
cout << “Error opening file\n”;
if (num % 2 == 0)
{
evens = 2 * evens; // add to table02Even and double each even number.
while (!cin.eof())
{
count++;
cin >> table02Even[Max];
}
}
else
{
odds; // add to table01Odds and find the cube root.
while (!cin.eof())
{
count++;
cin >> table01Odds[Max];
}
}
cout<<"Sum of x+y = " << z;
}
void ValuesSets (int& secondArracy)
{
/*
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.
*/
cout << right << setw (10) << fixed << setprecision(5);
}
int sumofall (double sum)
{
/*
call a value-returning function to sum all the values in the input array and then call the
function again to sum the first int(n/2) values in the second array
- the function should be designed to receive 2 parameters, an array of doubles and an
integer, size
- the function will add the first size 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 int(n/2) values in the second array with a label
*/
return sum;
}
|
e]