SlideShare a Scribd company logo
// c++ code for a Median of Integer Stream from Text File program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#include
double streamMedian(vector v, int size)
{
double* s = new double[size];
for (int i = 0; i < size; ++i)
{
s[i] = v[i];
}
for (int i = size - 1; i > 0; --i)
{
for (int j = 0; j < i; ++j)
{
if (s[j] > s[j+1])
{
double t = s[j];
s[j] = s[j+1];
s[j+1] = t;
}
}
}
double median = 0.0;
if ((size % 2) == 0)
{
median = (s[size/2] + s[(size/2) - 1])/2.0;
}
else
{
median = s[size/2];
}
return median;
}
int main()
{
int number;
double median;
std::vector v;
int count = 1;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
while ( myfile >> number )
{
cout << "After reading " << count << " element of stream - ";
v.push_back(number);
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << " ";
}
cout << "-> ";
sort(v.begin(), v.end());
cout << "median - " << streamMedian(v,v.size()) << endl;
}
myfile.close();
}
return 0;
}
/*
input.txt
5
15
1
3
output:
After reading 1 element of stream - 5 -> median - 5
After reading 1 element of stream - 5 15 -> median - 10
After reading 1 element of stream - 5 15 1 -> median - 5
After reading 1 element of stream - 1 5 15 3 -> median - 4
*/
Solution
// c++ code for a Median of Integer Stream from Text File program
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#include
double streamMedian(vector v, int size)
{
double* s = new double[size];
for (int i = 0; i < size; ++i)
{
s[i] = v[i];
}
for (int i = size - 1; i > 0; --i)
{
for (int j = 0; j < i; ++j)
{
if (s[j] > s[j+1])
{
double t = s[j];
s[j] = s[j+1];
s[j+1] = t;
}
}
}
double median = 0.0;
if ((size % 2) == 0)
{
median = (s[size/2] + s[(size/2) - 1])/2.0;
}
else
{
median = s[size/2];
}
return median;
}
int main()
{
int number;
double median;
std::vector v;
int count = 1;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
while ( myfile >> number )
{
cout << "After reading " << count << " element of stream - ";
v.push_back(number);
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << " ";
}
cout << "-> ";
sort(v.begin(), v.end());
cout << "median - " << streamMedian(v,v.size()) << endl;
}
myfile.close();
}
return 0;
}
/*
input.txt
5
15
1
3
output:
After reading 1 element of stream - 5 -> median - 5
After reading 1 element of stream - 5 15 -> median - 10
After reading 1 element of stream - 5 15 1 -> median - 5
After reading 1 element of stream - 1 5 15 3 -> median - 4
*/

More Related Content

PDF
Language is C++ -- Using Dynamic Memory 1 -- One statistic of interest.pdf
PDF
Educational Objectives After successfully completing this assignmen.pdf
PPT
Vectors Intro.ppt
PDF
This C++ program keeps giving me the wrong median. #include ios.pdf
PDF
C++ practical
PPTX
Moving Average Filter in C
DOCX
Programación
PDF
Update include ltiostreamgt include ltfstreamgt .pdf
Language is C++ -- Using Dynamic Memory 1 -- One statistic of interest.pdf
Educational Objectives After successfully completing this assignmen.pdf
Vectors Intro.ppt
This C++ program keeps giving me the wrong median. #include ios.pdf
C++ practical
Moving Average Filter in C
Programación
Update include ltiostreamgt include ltfstreamgt .pdf

Similar to c++ code for a Median of Integer Stream from Text File program#.pdf (10)

DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
PDF
LAB (100-) The Numbers Module Your task for this lab is to complete.pdf
PDF
C++ Deviation from Average (Recursion) Instructions Step 1- Write a.pdf
PPTX
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
PPTX
Intro To C++ - Class 11 - Converting between types, formatting floating point...
PPTX
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
PDF
C++ ProgrammingYou are to develop a program to read Baseball playe.pdf
PPT
Operator Overloading
PDF
AI Lab menu for ptu students and easy to use and best quality and help for 6t...
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
LAB (100-) The Numbers Module Your task for this lab is to complete.pdf
C++ Deviation from Average (Recursion) Instructions Step 1- Write a.pdf
Intro To C++ - Cass 11 - Converting between types, formatting floating point,...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
FSTREAM,ASSERT LIBRARY & CTYPE LIBRARY.
C++ ProgrammingYou are to develop a program to read Baseball playe.pdf
Operator Overloading
AI Lab menu for ptu students and easy to use and best quality and help for 6t...
Ad

More from aquastore223 (20)

PDF
1. How new modern materials prompted changes in architecture in the .pdf
PDF
1) cobalt iodide has an intense color, thus this colorless salt cann.pdf
PDF
the hybridisation is sp2 since the electron cloud.pdf
PDF
Sodium hydroxide, also known as lye and caustic s.pdf
PDF
Hydrocrbon is the organic compound which containing only the atoms .pdf
PDF
No. In order for a substance to conduct electrici.pdf
PDF
HCHO OS for H is +1, O is -2 there are two Hs o.pdf
PDF
The molecular orbital (MO) theory is a way of loo.pdf
PDF
The electrons reach or give off a certain amount of energy and diffe.pdf
PDF
Weak London disperision and dipole- dipoleModerate Hydrogen bond.pdf
PDF
var min =0; var max=10; var tab = {}; var name; var score;.pdf
PDF
Unfortunately several cancers are not predictable with simple tests .pdf
PDF
The van t Hoff factor i (named after J. H. van t Hoff) is a meas.pdf
PDF
Polar Bonds and Molecular Shape A polar molecule.pdf
PDF
The good functioning of an economy depends on the proper functioning.pdf
PDF
The answer is Yes, it is a reduction-oxidation reaction2 HNO2 + 2.pdf
PDF
Stegosaurus dinosaur belonged to the late jurassic period i.e Kimmer.pdf
PDF
The answer is d- the hydrogen bonds in iceThe high heat of fusion.pdf
PDF
SolutionOption (a) will qualify as a database as dictionary is a .pdf
PDF
Tests of Controls in an Audit of Internal Control. The objective of .pdf
1. How new modern materials prompted changes in architecture in the .pdf
1) cobalt iodide has an intense color, thus this colorless salt cann.pdf
the hybridisation is sp2 since the electron cloud.pdf
Sodium hydroxide, also known as lye and caustic s.pdf
Hydrocrbon is the organic compound which containing only the atoms .pdf
No. In order for a substance to conduct electrici.pdf
HCHO OS for H is +1, O is -2 there are two Hs o.pdf
The molecular orbital (MO) theory is a way of loo.pdf
The electrons reach or give off a certain amount of energy and diffe.pdf
Weak London disperision and dipole- dipoleModerate Hydrogen bond.pdf
var min =0; var max=10; var tab = {}; var name; var score;.pdf
Unfortunately several cancers are not predictable with simple tests .pdf
The van t Hoff factor i (named after J. H. van t Hoff) is a meas.pdf
Polar Bonds and Molecular Shape A polar molecule.pdf
The good functioning of an economy depends on the proper functioning.pdf
The answer is Yes, it is a reduction-oxidation reaction2 HNO2 + 2.pdf
Stegosaurus dinosaur belonged to the late jurassic period i.e Kimmer.pdf
The answer is d- the hydrogen bonds in iceThe high heat of fusion.pdf
SolutionOption (a) will qualify as a database as dictionary is a .pdf
Tests of Controls in an Audit of Internal Control. The objective of .pdf
Ad

Recently uploaded (20)

PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
International_Financial_Reporting_Standa.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
Trump Administration's workforce development strategy
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Unit 4 Computer Architecture Multicore Processor.pptx
Cambridge-Practice-Tests-for-IELTS-12.docx
International_Financial_Reporting_Standa.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Complications of Minimal Access-Surgery.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Computer Architecture Input Output Memory.pptx
What if we spent less time fighting change, and more time building what’s rig...
TNA_Presentation-1-Final(SAVE)) (1).pptx
Virtual and Augmented Reality in Current Scenario
Trump Administration's workforce development strategy
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Weekly quiz Compilation Jan -July 25.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf

c++ code for a Median of Integer Stream from Text File program#.pdf

  • 1. // c++ code for a Median of Integer Stream from Text File program #include #include #include #include #include #include #include #include #include #include using namespace std; #include double streamMedian(vector v, int size) { double* s = new double[size]; for (int i = 0; i < size; ++i) { s[i] = v[i]; } for (int i = size - 1; i > 0; --i) { for (int j = 0; j < i; ++j) { if (s[j] > s[j+1]) { double t = s[j]; s[j] = s[j+1]; s[j+1] = t; } } } double median = 0.0; if ((size % 2) == 0)
  • 2. { median = (s[size/2] + s[(size/2) - 1])/2.0; } else { median = s[size/2]; } return median; } int main() { int number; double median; std::vector v; int count = 1; ifstream myfile ("input.txt"); if (myfile.is_open()) { while ( myfile >> number ) { cout << "After reading " << count << " element of stream - "; v.push_back(number); for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << "-> "; sort(v.begin(), v.end()); cout << "median - " << streamMedian(v,v.size()) << endl; } myfile.close(); } return 0; }
  • 3. /* input.txt 5 15 1 3 output: After reading 1 element of stream - 5 -> median - 5 After reading 1 element of stream - 5 15 -> median - 10 After reading 1 element of stream - 5 15 1 -> median - 5 After reading 1 element of stream - 1 5 15 3 -> median - 4 */ Solution // c++ code for a Median of Integer Stream from Text File program #include #include #include #include #include #include #include #include #include #include using namespace std; #include double streamMedian(vector v, int size) { double* s = new double[size]; for (int i = 0; i < size; ++i)
  • 4. { s[i] = v[i]; } for (int i = size - 1; i > 0; --i) { for (int j = 0; j < i; ++j) { if (s[j] > s[j+1]) { double t = s[j]; s[j] = s[j+1]; s[j+1] = t; } } } double median = 0.0; if ((size % 2) == 0) { median = (s[size/2] + s[(size/2) - 1])/2.0; } else { median = s[size/2]; } return median; } int main() { int number; double median; std::vector v; int count = 1; ifstream myfile ("input.txt"); if (myfile.is_open()) {
  • 5. while ( myfile >> number ) { cout << "After reading " << count << " element of stream - "; v.push_back(number); for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << "-> "; sort(v.begin(), v.end()); cout << "median - " << streamMedian(v,v.size()) << endl; } myfile.close(); } return 0; } /* input.txt 5 15 1 3 output: After reading 1 element of stream - 5 -> median - 5 After reading 1 element of stream - 5 15 -> median - 10 After reading 1 element of stream - 5 15 1 -> median - 5 After reading 1 element of stream - 1 5 15 3 -> median - 4 */