My numeric analyzer (2)

This is a discussion on My numeric analyzer (2) within the C++ Programming forums, part of the General Programming Boards category; I rewrote the code for it. Please tell me how it is: Numbers.h: Code: #include <fstream> #include <vector> #include <iostream> ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    My numeric analyzer (2)

    I rewrote the code for it. Please tell me how it is:
    Numbers.h:
    Code:
    #include <fstream>
    #include <vector>
    #include <iostream>
    #include <numeric>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    class Numbers{
    public:
    	Numbers(string);	//Definitions are placed here to be inlined
    	__int64 Sum() {return accumulate(_b, _e, 0);}
    	double Ave() {return static_cast<double> (Sum()/numCount);}
    	int Mid() {return (nums[numCount/2]);}
    	void PrintNums() {for each (int val in nums) cout << val << endl;}
    	int Min(){return *min_element(_b, _e);}
    	int Max(){return *max_element(_b, _e);}
    	int Mod();
    	int Count()  {return numCount;}
    	
    private:
    	vector<int>::iterator _b,_e;
    	unsigned int numCount;
    	void ReadFile(string &fileName);
    	string fileName;
    	vector <int> nums;
    
    };
    Numbers.cpp:
    Code:
    #include "Numbers.h"
    
    Numbers::Numbers(string fileName)
    {
    	ReadFile(fileName);
    	//So we won't need to call these functions next times
    	numCount = static_cast <unsigned int> (nums.size()); _b = nums.begin(); _e = nums.end();		
    	sort(_b, _e);	//We need to sort to find the middle (Am I a poet?)	
    }
    
    void Numbers::ReadFile(string &fileName)
    {
    	cout << "Openning \""<< fileName <<"\"." << endl;  
    	ifstream file(fileName.c_str(), ios::in);
    	if(file.is_open())
    	{
    		int buf;
    		cout << "Reading numbers." << endl;
    		while(file >> buf) nums.push_back(buf);
    		
    		if(!file.eof()) cerr << "WARNING: File was not read to end!" << endl;
    		file.close();	
    	}
    	else cerr << "ERROR: File could not be opened!" << endl;
    }
    
    int Numbers::Mod() 
    {
    	unsigned int countN = 0;
    	int mod = 0;
    	unsigned int buff;
    	for each(int val in nums)
    	{
    		buff = static_cast<unsigned int> (count(nums.begin(), nums.end(), val));
    		if(buff > countN){ countN = buff; mod = val;}
    	}
    	return mod;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You seem to have too many casts. For example, std:.vector::size already returns an unsigned type. If you want to be more pedantic, you could make your size member a std::size_t.

    Actually, why duplicate the vector size variable? You can always get the size from the vector (with zero overhead hopefully).

    Code:
    double Ave() {return static_cast<double> (Sum()/numCount);}
    This cast seems to be out of place. If you don't want the average to be truncated, you'll need to cast either member of the division before dividing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    This cast seems to be out of place. If you don't want the average to be truncated, you'll need to cast either member of the division before dividing.
    Oh yes wrote it wrong in one of clean ups
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    OK, I changed them this way:
    Header:
    Code:
    #include <fstream>
    #include <vector>
    #include <iostream>
    #include <numeric>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    class Numbers{
    public:
    	Numbers(string);	//Definitions are placed here to be inlined
    	__int64 Sum() {return accumulate(_b, _e, 0);}
    	double Ave() {return (static_cast<double> (Sum()))/numCount;}
    	int Mid() {return (nums[numCount/2]);}
    	void PrintNums() {for each (int val in nums) cout << val << endl;}
    	int Min(){return *min_element(_b, _e);}
    	int Max(){return *max_element(_b, _e);}
    	int Mod();
    	size_t Count()  {return numCount;}
    	
    private:
    	vector<int>::iterator _b,_e;
    	size_t numCount;
    	void ReadFile(string &fileName);
    	string fileName;
    	vector <int> nums;
    
    };
    cpp:
    Code:
    #include "stdafx.h"
    #include "Numbers.h"
    
    Numbers::Numbers(string fileName)
    {
    	ReadFile(fileName);
    	//So we won't need to call these functions next times
    	numCount = nums.size(); _b = nums.begin(); _e = nums.end();		
    	sort(_b, _e);	//We need to sort to find the middle (Am I a poet?)	
    }
    
    void Numbers::ReadFile(string &fileName)
    {
    	cout << "Openning \""<< fileName <<"\"." << endl;  
    	ifstream file(fileName.c_str(), ios::in);
    	if(file.is_open())
    	{
    		int buf;
    		cout << "Reading numbers." << endl;
    		while(file >> buf) nums.push_back(buf);
    		
    		if(!file.eof()) cerr << "WARNING: File was not read to end!" << endl;
    		file.close();	
    	}
    	else cerr << "ERROR: File could not be opened!" << endl;
    }
    
    int Numbers::Mod() 
    {
    	int mod = 0;
    	__w64 int buff, countN = 0;
    	for each(int val in nums)
    	{
    		buff = count(nums.begin(), nums.end(), val);
    		if(buff > countN){ countN = buff; mod = val;}
    	}
    	return mod;
    }
    And the runner:
    Code:
    #include "Numbers.h"
    
    int main(int argc, char* argv[])
    {
    	string fName;
    	if(argc>1) fName = argv[1];
    	else fName = "nume.txt";
    	Numbers nums(fName);
    	if(nums.Count()){
    		nums.PrintNums();
    		cout << "-------------------------------------------------------------------" << endl;
    		cout << "Count: " << nums.Count() << endl;
    		cout << "Sum: " << nums.Sum() << endl;
    		cout << "Average: " << cout.precision(10) << nums.Ave() << endl;
    		cout << "Mid: " << nums.Mid() << endl;
    		cout << "Mod: " << nums.Mod() << endl;
    		cout << "Max: " << nums.Max() << endl;
    		cout << "Min: " << nums.Min() << endl;
    	}
    	cout << "\nPress a key to exit..." << endl;
    	_getch();
    	return 0;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    What about inlinings?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <fstream>
    #include <vector>
    #include <iostream>
    #include <numeric>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    class Numbers{
    public:
    	Numbers(string);	//Definitions are placed here to be inlined
    	__int64 Sum() {return accumulate(_b, _e, 0);}
    	double Ave() {return (static_cast<double> (Sum()))/numCount;}
    	int Mid() {return (nums[numCount/2]);}
    	void PrintNums() {for each (int val in nums) cout << val << endl;}
    	int Min(){return *min_element(_b, _e);}
    	int Max(){return *max_element(_b, _e);}
    	int Mod();
    	size_t Count()  {return numCount;}
    	
    private:
    	vector<int>::iterator _b,_e;
    	size_t numCount;
    	void ReadFile(string &fileName);
    	string fileName;
    	vector <int> nums;
    
    };
    You should avoid putting a "using namespace" statement anywhere within a header file as it opens up that entire namespace to whatever source file includes that header... intentional or otherwise. The namespace should be explicitly indicated where needed within the header. After that, any source file which includes a header can then put in a "using namespace" statement if necessary. For example:

    In the header file:
    Code:
    #include <fstream>
    #include <vector>
    #include <iostream>
    #include <numeric>
    #include <algorithm>
    #include <string>
    
    class Numbers{
    public:
    	Numbers(std::string);	//Definitions are placed here to be inlined
    	__int64 Sum() {return std::accumulate(_b, _e, 0);}
    	double Ave() {return (static_cast<double> (Sum()))/numCount;}
    	int Mid() {return (nums[numCount/2]);}
    	void PrintNums() {for each (int val in nums) std::cout << val << std::endl;}
    	int Min(){return *std::min_element(_b, _e);}
    	int Max(){return *std::max_element(_b, _e);}
    	int Mod();
    	size_t Count()  {return numCount;}
    	
    private:
    	std::vector<int>::iterator _b,_e;
    	size_t numCount;
    	void ReadFile(std::string &fileName);
    	std::string fileName;
    	std::vector <int> nums;
    };
    And then in the source files:
    Code:
    #include "Numbers.h"
    using namespace std;
    
    ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void PrintNums() {for each (int val in nums) cout << val << endl;}
    I'm pretty sure this is not valid C++. Do you compile your code before posting it?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I'm pretty sure this is not valid C++. Do you compile your code before posting it?
    Of course. You can read about that in MSDN.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Ah, a language extension of MSVC. You might want to specify that next time, or simply dont use such an extension.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    I didn't know that. Are you sure it is not standard? Maybe it is in standard proposal?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    I didn't know that. Are you sure it is not standard? Maybe it is in standard proposal?
    Apparently it is part of C++/CLI. As such, it is not part of standard C++, but is a language extension. The next edition of the C++ Standard will probably include a for each loop, but with a slightly different syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    OK, I used this technique.
    Code:
    int Numbers::Mod() 
    {
    	int mod = 0;
    	__w64 int buff, countN = 0;
    
    	for(vector<int>::const_iterator i = nums.begin(); i < nums.end(); ++i)
    	{
    		buff = count(nums.begin(), nums.end(), *i);
    		if(buff > countN){ countN = buff; mod = *i;}
    	}
    	return mod;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My numeric analyzer
    By siavoshkc in forum C++ Programming
    Replies: 22
    Last Post: 07-15-2007, 09:54 AM
  2. Send a numeric
    By nick048 in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-09-2007, 05:42 PM
  3. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 09:13 AM
  4. only accept numeric data
    By willc0de4food in forum Windows Programming
    Replies: 6
    Last Post: 08-19-2005, 03:38 AM
  5. OpenScript2.0 Spectrum Analyzer
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-23-2004, 09:01 PM

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