954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having trouble calculating the median from a data file

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.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

The program compiles but it gives me weird output like -858993460 with a the data file containing 1 - 10.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

Just what is this section supposed to be doing? Walk through it by hand and analyze what it is REALLy doing.

while ( !input.eof( ) )
{
	input >> value;
	count++;
	for ( int i = 0; i < count; i++ )
        {
	      input >> med_val[i];
	 }
}


The state of your array, when this has run ( with 1...10 as the data file) is
9 10 7 and the rest of the array is uninitialized. Variable count has no real relationship to the data.
Val

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

well what i want it to do is store the values of the file into an array and add to the counter. ive been trying different ways of doing this so things like this while loop might not make perfect sense. can you explain how to initializ the rest of the array so it can match the count.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

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

well what i want it to do is store the values of the file into an array and add to the counter. ive been trying different ways of doing this so things like this while loop might not make perfect sense. can you explain how to initializ the rest of the array so it can match the count.


What you wrote above and the code you posted do not match at all. Your description is very lacking because it's simplistic, confusing, and too much info is missing. The code you wrote requires much more complexity that the above description, too.

Start again and state clearly from beginning to end what you are trying to accomplish. Post additional details that might be necessary, like the input file. And what your expected output should be with the posted input as well as what the output currently is.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

OK i am taking a file with data stored like
1
2
3
4
5
then i have to find the median of the numbers in the file. The only condition i have to allow for is that if the amount of numbers in the file are an even number than the median is the average of two numbers.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

so what i tried to accomplish in the code was to open the file, read all the values into an array and then count how many numbers there were.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

Before you can solve the median value problem, you need to correctly read the data file into your array. See my earlier post. Fix that.

As far as median values go, are you guaranteed that the data file will be in sequential order?
Val

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

> while ( !input.eof( ) )
Well on the iteration before this becomes true, value is going to get a garbage value (most likely), which is going to completely screw up your attempt at a calculation.

Never use eof() to control a loop, that's not what it's for.
while ( input >> value )
is the way to go.

Checking for success in your other file reading functions as well would be a good idea.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

thanks so far and yes you are gauranteed that the file is in order.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

so far this is what I have i commented out the part that calculates the median but i still cant get the for loop to pass the right integer values into the array.

#include <iostream>
#include <fstream>
using namespace std;

int main( ){
	ifstream input;
	int lineValue[100];
	int value, median;
	int count = 1;


	input.open( "asgn.dat" );

	if ( input.fail( ) ){
		cout << "Input file opening failed. \n";

		exit(1);
	}
    

	while ( input >> value ){
		count++;
	}

	for ( int i = 0; i < count; i++ ){
		input >> lineValue[i];
	}

	/*if ( ( count % 2 ) == 0 ){
		count = count / 2;

		median = ( ( lineValue[ count + 1 ] + lineValue[ count ] ) / 2 );
	}
	else {
		count = ( ( count - 1 ) / 2 );
		
		median = lineValue[ count ];
	}*/
    
	cout << lineValue[1]<< endl;
	cout << count<< endl;

	return 0;
}
mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

> while ( input >> value )
This loops to the end of the file, then stops.

> input >> lineValue[i];
You're already at the end of the file, so this does nothing.

while ( count < 100 && input >> lineValue[count] ) {
  count++;
}


Now you can do your stats.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Thanks a lot that was really dumb of me, but im new to this stuff so u know problems come up a lot.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

here is my final code that functions correctly.

#include <iostream>
#include <fstream>
using namespace std;

int main( ){
	ifstream input;
	double lineValue[100];
	double median;
	int count = 1;


	input.open( "asgn.dat" ); 

	if ( input.fail( ) ){
		cout << "Input file opening failed. \n"; 

		exit(1); 
	}
    

    while ( count < 100 && input >> lineValue[count] ) { 
		count++; 
    }
    
	count = count - 1; 

	if ((count % 2) == 0 ){ 
		count = count / 2;  
        
		median = (( lineValue[ count + 1 ] + lineValue[ count ] ) / 2 ); 
	}
	else {
		count = ( ( count - 1 ) / 2 ) + 1; 
		
		median = lineValue[ count ];
	}
	
	cout << "The median is "<< median<< "."<< endl;

	return 0;
}
mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

If you start count at 0, you won't have to subtract 1 at the end of the loop.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I tried that but then it wasnt reading the right numbers from the file.

mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You