C++ After a program finishes executing, intermediate results are lost unless we
ID: 3725096 • 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 The function 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 T"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 name and do not include the line with Avg: Name: first value from the array If the input is not in a valid format the function should not write to the file · . 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.5333Explanation / Answer
/*
You can use fstream library to do read and write operations on file
If you want to write then open connection of type ofstream
if you want to read then open connection of type ifstream
I have used atof function of stdlib library to convert to string.
The average is calculate like
sum of all values
average=sum of all value/n;
Sample Output:
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 write 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
if(n==0 && size==1)
{
if(arrOfStr[0].length()>0)
{
myfile<<"Name:"<<arrOfStr[0];
}
}
else if(n!=0 && size!=0) //If file has name and values
{
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[1]={""};
//Calling function saveData with file name, please the file name
saveData("output1.txt",data,0,1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.