2,3,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,19,18,23, 21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11, 19,18,23,21,13,16,18,19,3,4,5,6,9,4,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5, 6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18, 19,3,4,5,6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Please write a C ++ program to find the mean value of data in the file data.txt

2,3,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,19,18,23,
21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,
19,18,23,21,13,16,18,19,3,4,5,6,9,4,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,
6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18,
19,3,4,5,6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4
Transcribed Image Text:2,3,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,19,18,23, 21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11, 19,18,23,21,13,16,18,19,3,4,5,6,9,4,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5, 6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18, 19,3,4,5,6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4
Expert Solution
Program Instructions

The program uses fstream header file to read and write from the text file, data.txt.

An array is used to read and write to the file using a loop.

Since the size of array is unknown sizeof() method is used to calculate the size of array.

Since the size of array is unknown data type long is used instead of int.

Program

// Code for writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () 
{
 fstream myfile;
 myfile.open("data.txt", ios::out | ios::in);
 long a[] = {2,3,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,19,18,23, 
    21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,12,16,19,18,11,
    19,18,23,21,13,16,18,19,3,4,5,6,9,4,4,5,6,9,4,3,2,1,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5, 
    6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4,1,12,16,19,18,11,19,18,23,21,13,16,18, 
    19,3,4,5,6,9,4,12,16,19,18,11,19,18,23,21,13,16,18,19,3,4,5,6,9,4};
    
    int size = sizeof(a) / sizeof(a[0]);
    cout << size;    
    
 if (myfile.is_open())
 {
  for(int count = 0; count < size; count ++)
   myfile << a[count] << " " ;
  myfile.close();
 }
 
 else cout << "Unable to open file";
 return 0;
}

 

// Code for reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
 long nums[172];
 int sum;
 
 fstream myfile;
 myfile.open("data.txt", ios::in | ios::out);
 
 if (myfile.is_open())
 {
  for (int i=0; i<172; i++)
  {
   myfile>>nums[i];
   //cout<<nums[i]<< " ";
  }
  myfile.close();
 }

 else cout << "Unable to open file"; 

 sum = 0;
 
 for (int i=0; i<172; i++)
 {
  cout<<nums[i]<<" ";
  sum += nums[i];
 }
 
 int mean = sum / 74;
 cout<<"\n Mean: "<<mean;
 
 return 0;
}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education