It doesn't return a correct result...

This is a discussion on It doesn't return a correct result... within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm doing the first exercise on struct. I can't figure out why it doesn't return a correct result for ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    It doesn't return a correct result...

    Hi,

    I'm doing the first exercise on struct. I can't figure out why it doesn't return a correct result for median. It always rounds it up... Say if I input 1 2 3 4, it returns the median == 3, not 2.5, even though it uses type double. Could you please help? Also, could you please help suggest a bit of improvement on the program? I'm learning about struct/class and this exercises asks me to practice using struct...

    Code:
    // Exercise 8.12
    #include "std_lib_facilities.h"
    /*
    	Write a function that finds the smallest & the largest element of vector
    	argument and also compute the mean and the median. Do not use global variables.
    	Either return a struct containing results or pass them back through reference
    	arguments. Which one do you prefer, and why?
    
    */
    
    struct stats {
    	vector<double> v;
    	double smallest, largest, median;
    	double fmean(vector<double>& v);
    	double fmedian(vector<double>& v);
    	double fsmallest(vector<double>& v);
    	double flargest(vector<double>& v);
    };
    
    int main()
    try {
    	stats mystats;
    	vector<double> v;
    	double x;
    	cout << "Enter some elements for a vector: \n";
    	while (cin >> x)
    		v.push_back(x);
    	cout << "Here are some summary statistics: \n";
    	cout << "Smallest: " << mystats.fsmallest(v) << '\n';
    	cout << "Largest: " << mystats.flargest(v) << '\n';
    	cout << "Mean: " << mystats.fmean(v) << '\n';
    	cout << "Median: " << mystats.fmedian(v) << '\n';
    	
    }
    catch (runtime_error e) {
    	cout << e.what();
    }
    
    double stats::fsmallest(vector<double>& v)
    {
    	smallest = v[0];
    	for (unsigned int i = 0; i < v.size(); ++i) {
    		if (v[i] < smallest)
    			smallest = v[i];
    	}
    	return smallest;
    }
    
    double stats::flargest(vector<double>& v)
    {
    	largest = v[0];
    	for (unsigned int i = 0; i < v.size(); ++i) {
    		if (v[i] > largest)
    			largest = v[i];
    	}
    	return largest;
    }
    
    double stats::fmean(vector<double>& v)
    {
    	double sum = 0;
    	for (unsigned int i = 0; i < v.size(); ++i) {
    		sum += v[i];
    	}
    	return sum / v.size();
    }
    
    double stats:: fmedian(vector<double>& v)
    {
    	sort(v.begin(), v.end());
    	median = 0;
    	for (unsigned int i = 0; i < v.size(); ++i) {
    		if (v.size()%2==0)
    			median = (v[v.size()/2] + v[v.size()/2-1])/2;
    		median = v[v.size()/2];	
    	}
    	return median;
    }

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    Code:
    if (v.size()%2==0)
      median = (v[v.size()/2] + v[v.size()/2-1])/2;
    median = v[v.size()/2];
    Perhaps you intended to place an else in there somewhere?

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by ctrl_freak View Post
    Perhaps you intended to place an else in there somewhere?
    Thanks a lot, ctrl_freak. I should have put else just before median... Thanks for helping

  4. #4
    Registered User
    Join Date
    Jan 2010
    Location
    china,wuhan
    Posts
    1
    I want to ask what "std_lib_facilities.h" is ? I can't find it in head files. I use the ubuntu OS.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by okgays View Post
    I want to ask what "std_lib_facilities.h" is ? I can't find it in head files. I use the ubuntu OS.
    It's a header file used in the new book by Bjarne Stroustrup. I'm learning C++ from this book, so I copy it & use it in every program.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    >I want to ask what "std_lib_facilities.h" is ?
    http://www.stroustrup.com/Programmin...b_facilities.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21