0

我刚开始学习c++。我已经开始编写用于创建汉明码的代码,但我仍处于编写代码的最开始阶段。我测试了到目前为止的内容,并且在询问用户“输入位数:”后我的代码崩溃了。我将不胜感激为什么它会这样做:) (PS。这段代码仍然不完整)

#include <iostream> //standard library
#include <string>
#include <iomanip>
#include <math.h>

void messageIntoHammingCode(int , int* , int*);

int main() {
    int messageBits[20]; //this is an array containing data bits
    int m; //number of data bits
    int r{0}; //number of redundant bits
    
    //Number of bits in the user input
    std::cout<<"Enter the number of bits: ";
    std::cin>>m;
    
    //Calculate Number of redundant bits r using formula 2^r<m+r+1
    while(pow(2,r) < m+r+1){
        r++;
    }
    
    //Length of the output message
    int hammingCode[r+m];
    /*Redundant Bits are added as follow:
    r1 at position 2^0
    r2 at 2^1
    r3 at 2^2, etc.*/
    //Initiliaze redudant bits to -1
    for(int i{0};i<=r+m;i++){
        int j = pow(2,i);
        hammingCode[j] = -1;
    }
    
    //User inputs data bit
    std::cout<<"Enter the Data Bits: ";
    for(int i{1};i<=m;i++){
        std::cin>>messageBits[i];
        //place the bit into the hamming cde
        messageIntoHammingCode(i,hammingCode, messageBits);
    }

    std::cout<<hammingCode;
    
}
void messageIntoHammingCode(int i, int hammingCode[], int messageBits[20]){
    //PLace the given code in the rest of the bits
    if(hammingCode[i]!=-1){
           hammingCode[i] = messageBits[i]; 
    }

}


4

0 回答 0