Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ After a program finishes executing, intermediate results are lost unless we

ID: 3726139 • Letter: C

Question

C++

After a program finishes executing, intermediate results are lost unless we save them somewhere. Frequently, this can be accomplished by writing the results to a file. For this problem, you must write a function saveData which takes four arguments: the filename, an array of strings, an integer n which represents the number of values in the array, and the size of the array. The given array of strings has the following structure: The first n entries of the array will be values, and the n+1 entry will be a name. ["value1","value2","value3","name"] This function will convert the first n values of the array into double and compute their average. Your function should then write to the file so that the result is in the following format: Name: name at n+1 position Avg: average of n numbers If n=0, then print just the first line to the file and do not include the line with Avg: Name: first value from array If the input is not in valid format the function should not write to the file.

void saveData(string fileName, string data[], int n, int size)

{ //Your code here }

Example: string data[4] = {"2.3", "-1.5", "0.8", "Garth"};

saveData("my_data.txt", data, 3, 4);

The file “my_data.txt” should have the following two lines: Name: Garth Avg: 0.5333

Explanation / Answer

/*
In C++, Mostly ofstream and ifstream are used to do write and read operations from file

ofstream => to do write operations
ifstream => to read from file

both the function are available in fstream library.

I have used atof function of stdlib library to convert string to the double

The average is calculate like
sum of all values

average=sum of all value/n;

Sample Output of file my_data.txt:

Name:Garth
Avg:0.533333

*/

//Copy the code from below line to the end of answer to your .cpp file and execute.


//C++ Program to do write operation to a file

#include <iostream> //standard input/out
#include <fstream> //File read write
#include<string> //for string
#include <stdlib.h> // atof
using namespace std;

//Function to write data to the file
void saveData(string filename,string arrOfStr[],int n,int size)
{
ofstream myfile;

//Opening file c_str is used to convert str to cont char * with null terminated
myfile.open(filename.c_str());

//If file has only name , means value of n is 0 and size=1
if(n==0 && size==1)
{
if(arrOfStr[0].length()>0)
{
//Writing only name to the file only when name string in not empty
myfile<<"Name:"<<arrOfStr[0];
}

}
else if(n!=0 && size!=0) //If file has name and values both
{
double sum=0;
for(int i=0;i<n;i++)
{
//converting string to double
double val = atof(arrOfStr[i].c_str());

//Adding value to previous sum
sum=sum +val;
}

//Calculating average
double average=sum/n;

//Writing to the file, first name and second average, name is available at index size-1
myfile<<"Name:"<<arrOfStr[size-1]<<endl;
myfile<<"Avg:"<<average<<endl;
}
else{
cout<<"Please check function parameters"<<endl;
}
//Closing the file
myfile.close();
}

//main function
int main()
{

//Creating data array
string data[4] = {"2.3", "-1.5", "0.8", "Garth"};

//Calling function saveData with file name, please the file name
//Please change the value of n and size carefully while calling function
saveData("my_data.txt", data, 3, 4);
}

**********************************************************************

#include <iostream> //standard input/out
#include <fstream> //File read write
#include<string> //for string
#include <stdlib.h> // atof
using namespace std;

//Function to write data to the file
void saveData(string filename,string arrOfStr[],int n,int size)
{
ofstream myfile;

//Opening file c_str is used to convert str to cont char * with null terminated
myfile.open(filename.c_str());

//If file has only name , means value of n is 0 and size=1
if(n==0 && size==1)
{
//Writing only name to the file only when name string in not empty
myfile<<"Name:"<<arrOfStr[0];

}
else if(n!=0 && size!=0) //If file has name and values both
{
double sum=0;
for(int i=0;i<n;i++)
{
//converting string to double
double val = atof(arrOfStr[i].c_str());

//Adding value to previous sum
sum=sum +val;
}

//Calculating average
double average=sum/n;

//Writing to the file, first name and second average, name is available at index size-1
myfile<<"Name:"<<arrOfStr[size-1]<<endl;
myfile<<"Avg:"<<average<<endl;
}
else{
cout<<"Please check function parameters"<<endl;
}
//Closing the file
myfile.close();
}

//main function
int main()
{

//Creating data array
string data[4] = {"2.3", "-1.5", "0.8", "Garth"};

//Calling function saveData with file name, please the file name
//Please change the value of n and size carefully while calling function
saveData("my_data.txt", data, 3, 4);
}