Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /*
  6. * Function to count the total number of characters in all the command-line input arguments to main (excluding the program name)
  7. */
  8.  
  9. unsigned int count_chars_in_args(int argc, char *argv[])
  10. {
  11. unsigned int i;
  12. unsigned int count = 0;
  13. for(i = 1; i < argc; i++) {
  14. count += strlen(argv[i]);
  15. }
  16. return count;
  17. }
  18.  
  19. /*
  20. * Function to convert endian representation. Input is converted from bigendian to little endian, and vice-versa.
  21. */
  22. unsigned int endian_convert(unsigned int num)
  23. {
  24. unsigned int res;
  25. unsigned char *cn = (unsigned char *)&num;
  26. unsigned char *cres = (unsigned char *)&res;
  27. /* Simply reverse the byte order */
  28. cres[0] = cn[3];
  29. cres[1] = cn[2];
  30. cres[2] = cn[1];
  31. cres[3] = cn[0];
  32.  
  33. return res;
  34. }
  35.  
  36. /* Function to reverse a string by creating a new string */
  37. char *reverse_new_str(char *str)
  38. {
  39. if(str == NULL) return str;
  40.  
  41. /* Allocate 1 extra byte for the null character */
  42. char *res = (char *) malloc (strlen(str) + 1);
  43.  
  44. int i, j;
  45.  
  46. /* Copy in reverse order */
  47. for(i = strlen(str) - 1, j = 0; i >= 0; i--, j++) {
  48. res[j] = str[i];
  49. }
  50.  
  51. /* Finally set the null byte */
  52. res[j] = '\0';
  53.  
  54. return res;
  55. }
  56.  
  57. /* Allocate a 2-D array where row[i] contains col[i] number of columns */
  58. void *my_2d_alloc(int rows, int *col)
  59. {
  60. /* First allocate rows */
  61. int **ret = (int **) malloc (rows * sizeof(int *));
  62. int i;
  63.  
  64. /* For each row, number of columns is given by col[i]. Allocate those many ints */
  65. for (i = 0; i < rows; i++) {
  66. ret[i] = (int *) malloc (sizeof(int) * col[i]);
  67. }
  68. return ret;
  69. }
  70.  
  71. /* Function to replace repeated consecutive characters by a single instance of the character. E.g., aaaabbbcabba becomes abcaba */
  72. char *deduplicate_in_place(char *str)
  73. {
  74. unsigned int i, j;
  75. i = j = 0;
  76. char prev = '\0';
  77. while(str[i] != '\0') {
  78. /* If the current char is not the same as previous char, retain it, else skip it */
  79. if(str[i] != prev) {
  80. str[j++] = str[i];
  81. prev = str[i];
  82. }
  83. i++;
  84. }
  85. str[j] = '\0';
  86. return str;
  87. }
  88.  
  89. /* Function to compute the cumulative sum of all characters in a string */
  90. unsigned int str_weight(char *str)
  91. {
  92. unsigned int weight = 0;
  93. unsigned int i = 0;
  94. while(str[i] != '\0') {
  95. /* Treat each char as an integer and accumulate its ascii value */
  96. weight += str[i];
  97. i++;
  98. }
  99. return weight;
  100. }
  101.  
  102. /* Given two integer arrays in1, in2, and an array of characters op, all of equal length (len), return an array of same length where ret[i] = in1[i] oper[i] in2[i]. op is either '+', '-', '*' or '/'. Guaranteed no overflow. No divide by zero. */
  103. int *array_op(int *in1, char *op, int *in2, unsigned int len)
  104. {
  105. int *ret = (int *) malloc (len * sizeof(int));
  106. unsigned int i = 0;
  107. for(i = 0; i < len; i++) {
  108. switch(op[i]) {
  109. case '+': ret[i] = in1[i] + in2[i]; break;
  110. case '-': ret[i] = in1[i] - in2[i]; break;
  111. case '*': ret[i] = in1[i] * in2[i]; break;
  112. case '/': ret[i] = in1[i] / in2[i]; break;
  113. default : printf("unsupported\n"); break;
  114. }
  115. }
  116. return ret;
  117. }
  118.  
  119. /* Given a number and a bit pattern of length len <= 8, find the index in num where that pattern appears, -1 otherwise.*/
  120. /* MASK(2) is 000...0011, MASK(3) is 000...0111, and so on. */
  121. #define MASK(len) ((1UL << (len)) - 1)
  122. int bit_hunt(unsigned long num, unsigned char pattern, unsigned int len)
  123. {
  124. unsigned int i;
  125. /* We store cast it to long so we don't lose data when we shift */
  126. unsigned long temp = (unsigned long) pattern;
  127.  
  128. /* Transform 0000....0111 to 1110....0000.
  129. * Now, when we & it with a num, we extract the most significant len number of bits from the num */
  130. unsigned long mask = MASK(len) << (64 - len);
  131.  
  132. for(i = 0; i < 64; i++) {
  133. /* Check if the len number of most significant bits are the same as pattern, if so, we have found a match.
  134. * If not, we shift the number left to push the most significant bit out and compare the pattern with len number of
  135. * bits starting from the next location.
  136. */
  137. if( ((num << i) & mask) == (temp << (64-len)) ) {
  138. return i;
  139. }
  140. }
  141.  
  142. /* If we don't find it, return -1 */
  143. return -1;
  144. }
  145.  
  146.  
  147. /* Given a number return 1 if it is a fibonacci number, else 0 */
  148. unsigned int is_fibo(unsigned long num)
  149. {
  150. unsigned long t0 = 0, t1 = 1, fibo;
  151. while(t1 >= t0) { /* Overflow will violate this condition, which is when we know the number is not fibonacci number */
  152. fibo = t0 + t1;
  153. if(fibo == num) return 1;
  154. t0 = t1;
  155. t1 = fibo;
  156. }
  157.  
  158. return 0;
  159. }
  160.  
  161. /* Write a function that accepts a number 'x' and a number 'terms' and returns the sin of the number 'x' upto 'terms' number of terms in the infinite series for sin(x). You will be provided the infinite series. */
  162. double my_sin1(double x, unsigned int terms);
  163.  
  164. /* Write a function that accepts a number 'x' and a number 'delta' and returns the sin of the number 'x' accurate within 'delta' using the infinite series for sin(x). You will be provided the infinite series. You will also be allowed to use sin function from the math library to compute accuracy. */
  165. double my_sin2(double x, unsigned int terms);
  166.  
  167. /* Given a string and indices i1 and i2, perform an in-place swap of characters at i1 and i2 in the string */
  168. void char_swap(char *str, int i1, int i2)
  169. {
  170. char t = str[i1];
  171. str[i1] = str[i2];
  172. str[i2] = t;
  173. }
  174.  
  175. /* Declare a structure that contains an integer i, float f and char c. Write a function struct_array_alloc that accepts an integer n and allocates memory for an array of n structures using malloc (or any other malloc family of functions) */
  176. struct s {
  177. char c;
  178. int i;
  179. float f;
  180. };
  181.  
  182. struct s *struct_array_alloc(unsigned int n) {
  183. struct s *ret = (struct s*) malloc (n * sizeof(struct s));
  184. return ret;
  185. }
  186.  
  187. /* Write a function paritize that accepts and returns an unsigned long number where the return number is the same as the input number except that the most significant bit of the return number is set to 1 if the number of 1s in the remaining bits is even, and 0 otherwise */
  188. unsigned long paritize(unsigned long num)
  189. {
  190. unsigned long ret = num;
  191. unsigned int count = 0;
  192. unsigned int i = 0;
  193.  
  194. /* First, count the number of 1's in the lower 63 bits. */
  195. for(i = 0; i < 8 * sizeof(num) - 1; i++) {
  196. if(num & (1UL << i)) count ++;
  197. }
  198.  
  199. /* If the count is even, set the 64th bit to 1, if not set it to 0 */
  200. if(count % 2 == 0) {
  201. ret |= (1UL << (8*sizeof(num) - 1));
  202. } else {
  203. ret &= ~(1UL << (8*sizeof(num) - 1));
  204. }
  205. return ret;
  206. }
  207.  
  208. void print_in_binary(unsigned long n)
  209. {
  210. int i;
  211. for(i = 63; i >= 0; i--) {
  212. if( (1UL << i) & n ) {
  213. printf("1 ");
  214. } else {
  215. printf("0 ");
  216. }
  217. }
  218. printf("\n");
  219. }
  220.  
  221.  
  222. int main(int argc, char *argv[]) {
  223. printf("count_chars_in_args: %u\n", count_chars_in_args(argc, argv));
  224. printf("endian_convert: %x -> %x\n", 0xdeadbeef, endian_convert(0xdeadbeef));
  225.  
  226. char *res = reverse_new_str("Helloooo Worrrrlddd, how are you?");
  227. printf("reverse_new_str: %s\n", res);
  228. printf("deduplicate_in_place: %s\n", deduplicate_in_place(res));
  229. printf("str_weight: %u\n", str_weight(res));
  230.  
  231. int in1[] = {1, 2, 3, 4};
  232. int in2[] = {5, 7, 2, 1};
  233. char op[] = {'+', '-', '*', '/'};
  234. int *ret = array_op(in1, op, in2, sizeof(in1)/sizeof(in1[0]));
  235. int i;
  236. for(i = 0; i < sizeof(in1)/sizeof(in1[0]); i++) {
  237. printf("array_op[%d]: %d\n", i, ret[i]);
  238. }
  239. free(ret);
  240.  
  241. printf("bit_hunt: %x:%x:%u -> %d\n",0x10070ab,0x15, 5, bit_hunt(0x10070ab, 0x15, 5));
  242. printf("bit_hunt: %x:%x:%u -> %d\n",0xFFFFFFFF,0x5, 3, bit_hunt(0xFFFFFFFF, 0x5, 3));
  243. printf("bit_hunt: %x:%x:%u -> %d\n",0,0x5, 3, bit_hunt(0, 0x5, 3));
  244. printf("bit_hunt: %x:%x:%u -> %d\n",0xB,0x5, 3, bit_hunt(0xB, 0x5, 3));
  245. printf("is_fibo: 9: %u, 8: %u\n", is_fibo(9), is_fibo(8));
  246. char_swap(res, 4, 0);
  247. printf("char_swap: res, 4, 0: %s\n", res);
  248. printf("paritize: %lx -> %lx, %lx -> %lx\n", 0x2, paritize(0x2), 0x3, paritize(0x3));
  249.  
  250. return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement