Closed AndrewRoe34 closed 2 years ago
Currently working on my own solution set and saw this mistake with yours. Answer should actually be this:
The index should be the first slot for array C as line 8 shows.
You can verify with this snippet of code:
#include <stdio.h> #include <stdlib.h> /** * Adds the two binary values together * * @param A char array A * @param B char array B * @param n length of A (same as B) * @return pointer to char array C (has length n + 1) */ char *addBits(char *A, char *B, int n) { char r = 0; char *C = (char *) malloc(sizeof(char) * (n + 1)); for(; (n - 1) >= 0; n--) { char s = A[n - 1] + B[n - 1] + r; C[n] = s % 2; r = s > 1 ? 1 : 0; } C[n] = r; return C; } /** * Starting point of application */ int main() { char A[] = {1, 0, 1, 1}; char B[] = {1, 1, 0, 1}; char *C = addBits(A, B, sizeof(A) / sizeof(char)); //should be 11000 for(int i = 0; i < 5; i++) printf("%d", C[i]); free(C); return 0; }
Currently working on my own solution set and saw this mistake with yours. Answer should actually be this:
The index should be the first slot for array C as line 8 shows.
You can verify with this snippet of code: