C++ updateData.txt is as following 23 56 140 19 65 77 87 109 244 36 87 22 -61 10
ID: 3940982 • Letter: C
Question
C++ updateData.txt is as following 23 56 140 19 65 77 87 109 244 36 87 22 -61 10 987 Part Four: Create a function called updateArray which will change the values in an array using the values in the file named "updateData.txt". This file is available for download in D2L. If the file has fewer values than the array size, leave the remaining array elements with their original values. Call this function from the main function using the array you created in Part Two. 1. 2. 3. Part Five: Create a function called divideBy7 which will display the quotient for each element in the array when divided by 7. [Remember the issue with integer division.] Display each answer with 2 decimal places. Each answer should be in a column which is 10 characters wide. There should be only five (5) answers per line of output. That is, you will need to insert a newline character after every 5 answers. Write this output to an output named "quotients.txt". Call this function from the main function using the updated array from Part Four. 1. 2. 3. 4. 5. 6.
Explanation / Answer
I have used an sample array for the code please change it to the correct array from part two.
THE C++ PROGRAM
#include <iostream>
#include <fstream> // file i/o stream
using namespace std;
void updateArray( int *arr,int *n); // the function to update array
void divideBy7(int *arr, int *n); // the function to print quotient
int main()
{ // Size of the sample array
int n = 20;
// Sample array this have to be generated using part two
int arr[n] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
updateArray(arr,&n); // calling update array function
divideBy7(arr,&n); // calling divide by 7 function
return 0;
}
// the function to update array
void updateArray( int *arr,int *n)
{
fstream datfile;
datfile.open("updateData.txt");// open file to read data
int i = 0;
do
{
datfile >> arr[i]; // reading
i++;
}while(datfile);
if(i > *n) *n = i-1; // update value of n
}
// the function to print quotient in the file
void divideBy7(int *arr, int *n)
{
fstream outfile;
outfile.open("quotients.txt"); // opening file to write
int i,j;
for(i=0;i<*n;i=i+5) // loops to print in a column of 5 element each
{
for(j=0;j<5;j++)
{
outfile << (double) arr[i+j]/7 << " "; // printing
}
outfile << endl; // newline after 5 elem
}
}
OUTPUT
The updateData.txt file after the execution of the programme
23 56 140 19 65 77 87 109 244 36 87 22 -61 10 987
The quotients.txt file after the execution of the programme
3.28571 8 20 2.71429 9.28571
11 12.4286 15.5714 34.8571 5.14286
12.4286 3.14286 -8.71429 1.42857 141
2.14286 2.28571 2.42857 2.57143 2.71429
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.