C updateDatatxt is as following 23 56 140 19 65 77 87 109 24
Solution
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 << \"\\t\"; // 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

