Is there any simple way to add a prefix to each element of an array in C? -
I am solving the gray code problem. I created my argument using recursive call. Everything looks fine but I do not know how to prefix '0' or '1' for each element of an array. For example) prefix 0: {00, 01, 11, 10} -> {000, 001, 011, 010} or prefix 1: {00, 01, 11, 10} -> {100, 101, 111, 110 }
contains # lt; Stdio.h & gt; #to & lt include, string.h & gt; # Include & lt; Math.h> Int grayCode (int list [], int bit) {int listLen = strlen (list); // Calculation of array length received INR revlist [] = {0}; Int newList [] = {0}; If (listLen & lt; pow (2, bit)) // If the received array length is less than 2 ^ bits, then proceed down. {For (int i = 0; i & lt; listLen; i ++) // Create a reverse order array {revList [i] = list [listLen - i]; } For (int i = 0; i & lt; listLen; i ++) // prefix 0 for each element of an array obtained, I know it does not work. How do i do it {List [ii] = "0" + list [i]; } (Int i = 0; i & lt; listLen; i ++) // prefix 1 in each element of a reverse array, I know it does not work. How do i do it {RevList [i] = "1" + Modified [i]; } For (int i = 0; i & lt; listLen; i ++) // new array {newList [i] = list [i] has inserted all elements of the array obtained; } (Int i = 0; i 1) {grayCode (initList, bit);} return 0;}
No, you can not add the 0
prefix to the integer.
An
(037)
8 < / Sub> == (31) 10
If you really want to save the numbers you already know, then 0
s, you have to store the strings as strings.
You can use a temporary string to store intermediates.
The algorithm will be:
four letters [10] = "01"; // 001 to convert four floors [10]; Strcpy (temp, "0"); // temp = "0", str = "01" strcat (temp, str); // temp = "001", str = "01" strcpy (str, temporary); // temp = "001", str = "001"
Comments
Post a Comment