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 median of the 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
Step 1
  1. Declare the required variables like filename , median and vector.
  2. User enters the filename. Read the file using ifstream;
  3. The Amount of data present in file named input is unknown so use vector to store integer data from file.
  4. Open the file to read, the file is read using while loop till the end of file is not reached.
  5. Read data using getline based on comma separator one by one. convert the data into string then convert it to int value. Now store the int value in the vector.
  6. The process is repeated till the end of the file is not reaches and read data is converted to int then stored in the vector.
  7. close the file now.Display the vector which contains unsorted data.
  8. median is always found on sorted data, first sorted the vector in ascending order.
  9. display the sorted vector. 
  10. find the media now. check if size of the vector is even or odd.
  11. if vector size is even then median is mean of 2 middle elements. size/2 and size/2-1.
  12. else median is the element present at size/2 index
  13. display the median now.

 

Step 2

//read the input txt file store in a vector
//median is found from sorted data 
//sort the vector then find median

#include <iostream>
#include <fstream>
#include <string> 
#include  <cstdlib>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    //reaf txt file using ifstream
    ifstream in_file;
    
 //take user input for filename only txt file
    string filename;
    
    //storing int data read from file
 vector<int> vectorOFInts;
 
 //storing median
 float median = 0.0;
 
 //user enters filename
    cout <<"\nEnter file name: ";
    cin >>filename;
    
    //we are reading text file only right now
    filename = filename+".txt";
    
    // open file to read now
    in_file.open(filename.c_str());
    
    //if file opened successfully
    if(in_file){
       //file opened successfully
       //Read data from file now
       string line;        
     
  //loop will go till end of file is not reached.
        while( getline(in_file,line,',')){
         //convert string to int
            int data = atoi(line.c_str());
            
            //add element in vector
            vectorOFInts.push_back(data);
        }
  
  cout <<"\n close file now ..\n";
        in_file.close();//close the in_file
        cout<<"file is closed\n";
        
        //display the vector elements before sorting     
        cout<<"**** vector before sorting **** \n";
     for (int i = 0; i < vectorOFInts.size(); i++) {
   cout << vectorOFInts.at(i) << ' ';
  }

        //WORK ON THE DATA NOW
     //for finding the median data must be sorted 
  //sort the vector in ascending order
     sort(vectorOFInts.begin(),vectorOFInts.end());
     
     //size of vector
     int totalElements = vectorOFInts.size();
     
  //display the vector elements after sorting     
  cout<<"\n\n**** vector AFTER SORTING ****\n ";
     for (int i = 0; i < vectorOFInts.size(); i++) {
   cout << vectorOFInts.at(i) << ' ';
  }
  
  //index for getting middle index element
  int half = totalElements /2;
  // if size is even
     if(totalElements % 2 == 0){
      median = (vectorOFInts.at( half-1) + vectorOFInts.at( half))/2.0 ;
  }else{//if size is odd
   median = vectorOFInts.at( half) ;
  }
  //display the median
  cout<<"\n\n MEDIAN : "<<median;
        
    }else{
        cout <<"\n UNABLE TO OPEN file ";
        //signify abnormal termination
        return 1;
    }

    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
  • SEE MORE 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