int *int_array = (int *)malloc(sizeof(int) * 40);Write a loop that will use scanf to read 40 integers from stdin directly into the elements of the integer array.
Solution: The above code allocates a block of memory that can accommodate 40 integers. In other words, it has allocated an array of 40 integers. You now need to pass the address of each array element to scanf. There are two ways to do this. First you can take the address of each element:
for (i = 0; i < 40; i++) scanf("%d", &int_array[i]);Alternatively you can use pointer arithmetic:
for (i = 0; i < 40; i++) scanf("%d", int_array + i);
char word[7]; char *string_ptr; int *double_string_ptr; int i; (1) string_ptr = (char *)malloc(sizeof(char) * 50); (2) double_string_ptr = (char **)malloc(sizeof(char *) * 10); for (i = 0; i < 10; i++) double_string_ptr[i] = string_ptr + (i * 5);Further suppose that you are given the following information (all addresses are in base 10):
address of word = 1000 address of string_ptr = 1008 address of double_string_ptr = 1012 address returned by malloc in statement (1) = 5000 address returned by malloc in statement (2) = 6000 sizeof(char) = 1 sizeof(char *) = 4
Dllist my_list = new_dllist(); Dllist one_node = dll_append(my_list, new_jval_i(1)); Dllist two_node = dll_prepend(my_list, new_jval_i(2)); Dllist three_node = dll_insert_a(two_node, new_jval_i(3)); Dllist four_node = dll_insert_b(two_node, new_jval_i(4)); ||----------------------------------------------------------------------- \/ | my_list --> ------------- ------------- ------------- ------------- ------------- | | flink-----|-->| flink-----|-->| flink-----|-->| flink-----|-->| flink-----|-| |-| blink |<--|-blink |<--|-blink |<--|-blink |<--|-blink | | | val.i = ? | | val.i = 4 | | val.i = 2 | | val.i = 3 | | val.i = 1 | | ------------- ------------- ------------- ------------- ------------- | ^ | | |----------------------------------------------------------------------
You are to write a function that given two sorted lists of integers, L1 and L2, prints the integers that are common to both lists. For example, if L1 contains the integers 1, 3, 6, 10, 12 and L2 contains the integers 2, 3, 10, 12, 15 then your program should print:
3 10 12You should assume that the lists are stored as Dllists and that the integers are stored in Jvals in the Dllists.
void print_common_elements(Dllist L1, Dllist L2) { Dllist list1, list2; // temporary pointers for iterating thru L1 and L2 list1 = dll_first(L1); // make list1 and list2 point to the first elements in list2 = dll_first(L2); // their respective lists int value1, value2; // hold the integer values of the current elements in L1/L2 while ((list1 != dll_nil(L1)) && (list2 != dll_nil(L2))) { value1 = jval_i(dll_val(list1)); value2 = jval_i(dll_val(list2)); if (value1 == value2) { printf("%d\n", value1); list1 = dll_next(list1); list2 = dll_next(list2); } else if (value1 < value2) list1 = dll_next(list1); else list2 = dll_next(list2); } }
Solution 1: The first solution iterates through the elements of the input list and prepends them to the reversed list:
Dllist reverse(Dllist list) { Dllist reversed_list = new_dllist(); Dllist list_ptr; dll_traverse(list_ptr, list) { dll_prepend(reversed_list, dll_val(list_ptr)); } }Solution 2: The second solution iterates through the elements of the input list in reverse order and appends them to the reversed list:
Dllist reverse(Dllist list) { Dllist reversed_list = new_dllist(); Dllist list_ptr; dll_rtraverse(list_ptr, list) { dll_append(reversed_list, dll_val(list_ptr)); } }