Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

[RESOLVED] An error in my code - binary digit matrix implementation by using 2D vector

$
0
0
Hello
I'm doing a code in C++ which must execute a matrix in output contain binary digits, vertically.
the programming environment is visual studio and when I use void in front of my 2D vector, I have one error, unfortunately, and I cannot solve this error
when I use vectors which I commented in the following code without void function everything is okay and if I want to merge them into one vector like following with void function I will have an error in this line:

HTML Code:

for (const auto& row : generate_mtx(nbits))
please help me
my code:

HTML Code:

#include "stdafx.h"
#include <iostream>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;

constexpr unsigned long long ubound(std::size_t nbits)
{
        if (nbits < 2) return 2;
        else return ubound(nbits - 1) * 2;
}

//std::vector< std::vector<int>
> generate_mtx(std::size_t nbits)
void generate_mtx(std::vector< std::vector<int> >& result, 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;
}

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;
        //return 0;
}


Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images