#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <algorithm>
struct sample{
int ID;
float readCNTx, readCNTy;
};
//function protoypes
float GetMedian1(std::vector<sample>& );
int main() {
std::ifstream myfile("/Users/T_HEN1203/Desktop/union1.csv");
if ( myfile.is_open() ) {
std::vector<sample> Gene;
char delimiter;
int id;
float readCNTx, readCNTy;
// Read the file.
myfile.ignore(1000, '\n');
while (myfile >> id >> delimiter >> readCNTx >> delimiter >> readCNTy){
Gene.push_back({id, readCNTx, readCNTy});
}
float med1, med2;
med1 = GetMedian1(Gene);
std::cout << med1 << std::endl;
}
else{
std::cerr<<"ERROR: The file isnt open.\n";
}
return 0;
}
/* function definitions
--------------------------------------------------------------------------------------*/
//functions to get the median
float GetMedian1 (std::vector<sample>& vec) {
std::sort(vec.begin().readCNTx, vec.end().readCNTx);
if (vec.empty())
return 0;
else {
if(vec.size().readCNTx % 2 == 0)
return ( vec.at(vec.size().readCNTx/2 + 1) + vec.at(vec.size().readCNTx/2) ) / 2;
elsereturn vec.at( (vec.size().readCNTx + 1) )/2];
}
}
In the getmedian1 function i tried to access the members of the struct but i get the following errors from code blocks:
/Users/T_HEN1203/Documents/Stats.cpp|41|error: no member named 'readCNTx' in 'std::__1::__wrap_iter<sample *>'; did you mean to use '->' instead of '.'?|
/Users/T_HEN1203/Documents/Stats.cpp|41|error: no member named 'readCNTx' in 'std::__1::__wrap_iter<sample *>'; did you mean to use '->' instead of '.'?|
/Users/T_HEN1203/Documents/Stats.cpp|42|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|48|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm|4050|error: no type named 'value_type' in 'std::__1::iterator_traits<float>'|
/Users/T_HEN1203/Documents/Stats.cpp:44:10: note||in instantiation of function template specialization 'std::__1::sort<float>' requested here|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Users/T_HEN1203/Documents/Stats.cpp|42|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|46|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
/Users/T_HEN1203/Documents/Stats.cpp|48|error: member reference base type 'size_type' (aka 'unsigned long') is not a structure or union|
and whole lot more i don't want to paste for space reasons. Also could you give a reference on operator , i see it a lot but i'm not sure what it does.
std::ifstream myfile("\\Users\\T_HEN1203\\Desktop\\union1.csv");
The number of slashes didn't make a difference but i figured out what i need to do. I had to split up the vector to get the program to run correctly. Thank you for the help!