dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: questionm

  1. #1
    Join Date
    Mar 2018
    Posts
    36

    questionm

    Hello

    in the following code which a special matrix will be printed out in the output, I have 3 parts:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <limits>
    #include <vector>
    #include <algorithm>
    //===================part 1======================
    constexpr unsigned long long ubound(std::size_t nbits)
    {
    	if (nbits < 2) return 2;
    	else return ubound(nbits - 1) * 2;
    }
    //===================part2==================
    std::vector< std::vector<int> > generate_mtx(std::size_t nbits)
    {
    	nbits %= std::numeric_limits<unsigned long long>::digits;
    
    	std::vector< std::vector<int> > result(nbits);
    
    	// note: col with all zeroes is skipped (start with number = 0 to include it)
    	for (unsigned long long number = 1; number < ubound(nbits); ++number)
    	{
    		auto n = number;
    		for (auto& vec : result)
    		{
    			vec.push_back(n % 2);
    			n /= 2;
    		}
    	}
    
    	// to get the rows in the same order as illustrated in the example  
    	std::reverse(std::begin(result), std::end(result));
    
    	return result;
    }
    //=========================part3====================
    int main()
    {
    	
    	if (size_t nbits = 4)
    	{
    		std::cout << "check matrix (H) is: " << std::endl;
    
    		for (const auto& row : generate_mtx(nbits))
    		{
    			for (int v : row) std::cout << v << ' ';
    			std::cout << '\n';
    		}
    
    		
    	}
    	int pause;
    	std::cin >> pause;

    how I can merge and combine all parts in the 3rd part?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: questionm

    Do you mean like this?

    Code:
    #include <iostream>
    #include <limits>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
    	constexpr size_t nbits = 4 % std::numeric_limits<unsigned long long>::digits;
    
    	std::vector< std::vector<int> > result(nbits);
    
    	// note: col with all zeroes is skipped (start with number = 0 to include it)
    	for (unsigned long long number = 1, n = number, ub = 2 << (nbits - 1); number < ub; n = ++number)
    		for (auto& vec : result)
    			vec.push_back(n % 2), n >>= 1;
    
    	// to get the rows in the same order as illustrated in the example
    	std::reverse(std::begin(result), std::end(result));
    
    	std::cout << "check matrix (H) is: " << std::endl;
    
    	for (const auto& row : result)
    	{
    		for (int v : row) std::cout << v << ' ';
    		std::cout << '\n';
    	}
    }
    Last edited by 2kaud; April 21st, 2018 at 09:20 AM. Reason: Code simplification
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  3. #3
    Join Date
    Mar 2018
    Posts
    36

    Re: questionm

    @2kaud
    Yes, its great and works correctly, but could you please explain that why it never return any value?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: questionm

    main() is a special function such that if no return value is specified via return statement, then by default 0 is assumed to be returned. This only applies to main().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  5. #5
    Join Date
    Mar 2018
    Posts
    36

    Re: questionm

    thank you dear @2kaud, now it's perfect and correct.

  6. #6
    Join Date
    Mar 2018
    Posts
    36

    Re: questionm

    Quote Originally Posted by 2kaud View Post
    main() is a special function such that if no return value is specified via return statement, then by default 0 is assumed to be returned. This only applies to main().
    hello, In my code I used object-oriented programming and inheritance, so I must use the void function for inherite from base class,
    now this code works correctly without inheritence and out of my project, and the error is in this line in (nbits):
    void Ham::check(std::vector< std::vector<int> > result(nbits))
    and when I replace std::size_t nbits instead of (nbits), then everything will be okay like the following line:
    void Ham::check(std::vector< std::vector<int> > result, std::size_t nbits)
    I want to know this action is correct, logically?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: questionm

    Code:
    void Ham::check(std::vector< std::vector<int> > result, std::size_t nbits)
    If you use this, then within check(), make you are sizing result correctly.

    Code:
    result.resize(nbits);
    Also you are passing by value which entails copying of all the elements in result. Any changes made to result are not passed back to the calling function. Have you considered passing by ref which doesn't involve element copying?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  8. #8
    Join Date
    Mar 2018
    Posts
    36

    Re: questionm

    to do inheritance, for each void function which I apply in the derived class, I must define them in the base class and the header of derived class:

    in the file derivedClass.cpp I wrote your code with appending void function and the code is:

    Code:
    constexpr size_t nbits = 4 % std::numeric_limits<unsigned long long>::digits;
    
    void derivedClass::check(std::vector< std::vector<int> > result(nbits))
    {
    
    	// note: col with all zeroes is skipped (start with number = 0 to include it)
    	for (unsigned long long number = 1, n = number, ub = 2 << (nbits - 1); number < ub; n = ++number) {
    		for (auto& vec : result)
    		{
    			vec.push_back(n % 2), n >>= 1;
    		}
    	}
    	// to get the rows in the same order as illustrated in the example
    	std::reverse(std::begin(result), std::end(result));
    
    	std::cout << "check  is: " << std::endl;
    
    	for (const auto& row : result)
    	{
    		for (int v : row)
    		{
    			std::cout << v << ' ';
    		}
    		std::cout << '\n';
    
    	}
    }
    and the error of this code is in these lines:

    1)void derivedclass::check(std::vector< std::vector<int> > result(nbits)) an error occurred on nbits (an error is: variable nbits is not a type name)
    2)for (auto& vec : result) an error occurred on result (this range base 'for' statement requires a suitable begin function and none was found)
    3) std::reverse(std::begin(result), std::end(result)); : error on std::begin and std::end (
    in the derivedclass.h (header file of derived class) I inserted these lines: (no instance of overloaded function "std:: end" and "std::begin" matches the argument list
    4) for (const auto& row : result) on result ( this range base 'for' statement requires a suitable begin function and none was found)

    ====================================================================================
    and in the header file of derivedclass.h the code is:
    Code:
    #include "stdafx.h"
    
    class derivedclass: public baseclass {
    public:
    derivedclass();
    	~derivedclass(void);
    void checkMatrix(std::vector< std::vector<int> > result(nbits));
    the error in:
    void checkMatrix(std::vector< std::vector<int> > &result(nbits)); and the error is on (nbits) (variable nbits is not a type name)
    =================================================================================


    in the baseclass.h:
    Code:
    #include "stdafx.h"
    
    class baseclass {
    protected:
    std::vector< std::vector<int> > result;
    
    public:
    
    virtual void check(std::vector< std::vector<int> > &result(nbits)) = 0;
    again an error exist on line: virtual void check(std::vector< std::vector<int> > &result(nbits)) = 0; on nbits (variable nbits is not a type name)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,210

    Re: questionm

    Code:
    void checkMatrix(std::vector< std::vector<int> > result(nbits));
    No. This is not a valid definition. You are trying to set the size of the vector as part of the definition which isn't allowed. See my post #7. You need

    Code:
    void checkMatrix(std::vector< std::vector<int> > result, size_t nbits);
    etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)