Hi im trying to take a .dat file with just a list of integers separated by lines. The program has to allow for whether or not the list is an even amount of numbers or not. im a bit stuck here is what i have so far.
#include <iostream>
#include <fstream>
using namespace std;
int main( ){
ifstream input;
int med_val[100];
int value, median;
int count = 1;
input.open( "asgn.dat" );
if ( input.fail( ) ){
cout << "Input file opening failed. \n";
exit(1);
}
while ( !input.eof( ) ){
input >> value;
count++;
for ( int i = 0; i < count; i++ ){
input >> med_val[i];
}
}
if ( ( count % 2 ) == 0 ){
count = count / 2;
median = ( ( med_val[ count + 1 ] + med_val[ count ] ) / 2 );
}
else {
count = ( ( count - 1 ) / 2 );
median = med_val[ count ];
}
cout << median<< endl;
return 0;
}
Any help would be appreciated thanks in advance.