1.For two variables a and b created as follows unsigned int a=3, b=5; The value of b>>a is 1 True or False 2.For variables a created as follows char *a="abcdefgh"; the value of a[3] is 'c'. True or...

1 answer below »
C exam regarding memory allocation, pointers, signed/unsigned data values, sorting algorithms. Exam proctor is Respondus Lockdown Browser. I have a practice exam to see if it's possible.


1.For two variables a and b created as follows unsigned int a=3, b=5; The value of b>>a is 1 True or False 2.For variables a created as follows char *a="abcdefgh"; the value of a[3] is 'c'. True or False 3. For two variables a and b created as follows unsigned int a=3, b=5; the value of a|b us 1, True or False 4. In a C program with the main function defined as int main(int argc, char argv[][]), the argv parameter is a static two-dimensional array of characters. True or False 5. Assuming that i is an unsigned integer having a value greater than 8 and smaller than 32. Then (i>>1)+(i<3) is="" equal="" to="" 3.5*i="" true="" or="" false="" 6.="" statement="" int="" *="" a,b;="" is="" to="" define="" two="" pointers="" a="" and="" b.="" true="" or="" false="" 7.="" for="" a="" pointer="" p="" and="" an="" array="" a="" defined="" as="" follows:="" int="" a[]="{3," 13,="" 113,="" 1113},="" *p="&(a[2]);" expression="" p-a="" is="" equal="" to="" 2="" true="" or="" false="" 8.="" char="" s[]="hello world!" ;="" strlen(s)="" and="" sizeof(s)="" return="" the="" same="" value.="" true="" or="" false="" 9.="" when="" a="" program="" prod="" is="" run="" with="" command="" line="" ./prog="" -n="" 12="" -c="" dev="" -o="" out.txt,="" argv[0][0]="" is="" ________="" argv[1][1]="" is="" ________="" argv[2][2]="" is="" ________="" 10.="" for="" floating="" point="" variable="" f,="" write="" c="" statements="" to="" extract="" bit="" 12="" to="" bit="" 15="" and="" save="" the="" value="" corresponding="" to="" these="" bits="" to="" an="" unisgned="" integer="" variable="" i.="" f="" is="" 32="" bits="" long.="" bit="" 0="" is="" the="" least="" significant="" bit(lsb),="" and="" bit="" 21="" is="" the="" most="" significant="" bit(msb).="" 11.="" assume="" the="" address="" of="" a="" variable="" a="" is="" 4434.="" based="" on="" the="" statements="" below,="" determing="" the="" values="" of="" the="" following="" expressions:="" int="" a="12;" int="" *b="&a;" omt="" **c="&b" value="" of="" *b="" is:="" value="" of="" *c="" is:="" value="" of="" **c="" is:="" 12.="" what="" is="" printed="" out="" by="" the="" code="" below?="" #include=""> main() { int a=0; int *b=&a; *b=a+2; printf("%d",a) } 13. Finish the function declared as follows. The function returns a pointer to the MAXIMUM value in an array of double values. You can assume that a is not NULL, and size is not equal to 0. double* maximum(double* a, int size): { 14. Given char str[] = "a?b?,,c?,,??d#,,"; Answer the following questions. a) What would strtok(str, "?"); return? b) continuing part a, fill in the blank such that strtok() will return a string "b". strtok(______); c) continuing parts a and b, what would strtok(str+2, "?"); return? 15. For an unsigned integer variable num, write C statements to finish the following operations. Each unsigned integer is 32 bits long. Bit 0 is the least significant bit and bit 21 is the most significant bit 1. set even bits (i.e. bit 0, bit 2, bit 4,...) to 1. __________ 2. clear odd bits (i.e. bit 1, bit 3, bit 5,...) ___________ 3. flip the 8 bits in the most significant byte. ___________ 16. Write a C function that can find the longest continuous sequence of bit 1s (binary number 1) from an unsigned integer. For example, if the integer is 4891183, the function returns 4 because 4891183 is 0000 0000 0100 1010 1010 0010 0010 1111 in binary, and the longest sequence of binary number 1s is from bit 0 to bit 3. Use bitwise operations to chec the bits in the integer. DO NOT translate the integer into a string of character 1's and 0's. Use the following declaration of the function. int binary_sequence(unsigned int num); 17. Write a C function to sort an array of unsigned long integers (64 bits each) in ascending order. The function is declared as follows void radixsort(unsigned long *a, int size); 18. Write a C program to print out the paths in PATH environment variable, one path on each line. For example, if PATH contains the following directories (seperated by colon signs) $echo $PATH /home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin Your program needs to print /home/ubuntu/bin /home/ubuntu/.local/bin /usr/local/sbin Implement your program using strtok(). You can get the PATH environment variable by searching envp or use the function getenv() 19. Write a C program that sorts its command line arguments in ascending order determined by strcmp() linked-list-and-state-space-search-ff1l5t3o.pptx Structures and pointers 6/25/2021 1 Structures can organize data in different types Declared using struct with member types and names included in braces. struct variables can be declared with the struct. 6/25/2021 Lect 23P. 2 struct transaction { int id; float amount; char name[20]; char addr[30]; }; struct transaction t, *pt; struct transaction { int id; float amount; char name[20]; char addr[30]; } t, *pt; typedef struct { int id; float amount; char name[20]; char addr[30]; } transaction; transaction t,*pt; Prof. Ding, Xiaoning. Spring 2021. Protected content. 6/25/2021 Prof. Ding, Xiaoning. Spring 2021. Protected content. 2 Instructor: Often multiple variables must be passed from one function to another, and often these variables have different data types. Thus it is useful to have a container to store various types of variables in. Structs allow the programmer to do just that. Accessing struct members using . or -> A member in a struct variable can be access using . A member in a struct pointed by a pointer can be access using -> or by dereferencing the pointer first and then using . 6/25/2021 3 pt=&t; printf("id: %d, name: %s, addr: %s", t.id, pt->name, (*pt).addr); Pointer members in a struct Some members need to have their memory dynamically allocated or location dynamically determined. 6/25/2021 4 Extra pointers can be added to structs to support data structures, e.g., linked list, stack, queue, tree, graph, … struct transaction{ int id; float amount; char name[20]; char *addr; } t1, t2; t1.addr=(char *)malloc(30); t2.addr=another_str; struct transaction{ int id; float amount; char name[20]; char *addr; struct transaction *next; } t1, t2; t1.next = &t2; Linked Lists A linked list is a sequence of connected nodes. Each node contains at least Some data A pointer to the next node in the list The head pointer points to the first node The last node points to NULL The tail pointer (optional) points to the last node. 6/25/2021 5 '\0' node * a; a->next = b; z->next = NULL; b->next = c; node * head; node * tail; … Why linked list? Often the maximum size of the list cannot be estimated. Static arrays have fixed sizes. Extending dynamic arrays (malloc-ed mem space) may need to copy data from the old and smaller space to a larger new space. Usually there are updates in the middle of the list, e.g., insertion, deletion, re-arranging, etc. Overhead is high with arrays. Inserting a new element in the front or deleting the first element requires shifting all the elements in the array On average, half of the lists needs to be moved for insertion/deletion. Compared to an array, linked list uses only as much space as is needed (requires extra-space for pointers) 6/25/2021 6 Declare node type --- self-referential struct Create nodes --- allocate memory on-demand, initialize members Link nodes to the list --- find a location on the list (the previous and/or the next node) and update pointers in these nodes and the new node. Keep the head pointer updated --- If the address is lost, the whole list may be lost. Ensure the next pointer of the last node to NULL. If there is a tail pointer, keep it updated Building a linked list 7 6/25/2021 '\0' … node * a; node * b; a->next = b; z->next = NULL; b->next = c; node * head; node * tail; 6/25/2021 8 #include #include struct node{ int id; char name[20]; struct node *next; }; int main(){ struct node *head=NULL, *tail=NULL, *pnode; while(1){ pnode=(struct node *)malloc(sizeof(struct node)); printf("id:"); scanf("%d", &(pnode->id)); if(pnode->id<0) break;="" printf("name:");="" scanf("%s",="" pnode-="">name); pnode->next=NULL; /*ensure next pointer of last node is NULL */ if(head==NULL) head=pnode; if(tail!=NULL) tail->next=pnode; tail=pnode; } Declare node type self_referential struct Create and initialize a new node Link the new node to the end head/tail pointers always point to the first/last node. Traverse a linked list Start from the head pointer. Proceed following the next pointers of nodes. Stop when the last node is reached. Last node: next pointer is NULL, or pointed by the tail pointer. 6/25/2021 9 pnode=head; while(pnode!=NULL) { printf("id: %d\t name:%s\n", pnode->id, pnode->name); pnode=pnode->next; } } Adding a node to a list 6/25/2021 10 node * head; node * b; b->next = head; head = b; Order of the operations is important head = b; b->next = ? '\0' ? adding to the front Adding a node to a list adding to the front 6/25/2021 11 node * head; node * b; b->next = head; head = b; Order of the operations is important head = b; b->next = ? '\0' ? Inserting into the middle node * a; node * b; '\0' b->next = a->next; a->next = b; Consider a->next as a "head pointer" to the rest of the list. Deleting a node 12 6/25/2021 node * a; a->next; a->next->next; p = a->next; a->next = a->next->next; … free(p); p Deleting the first node 13 6/25/2021 node * a; a->next; a->next->next; p = a->next; a->next = a->next->next; … free(p); p p = head; head = head->next; … free(p); Deleting the last node ? Inserting/deleting a node: what to notice? Check whether the node is to be added to (deleted from) the front, the middle, or the end of the list. Different operations for different locations. Avoid losing the pointer pointing to a node It becomes ``inaccessible'' if there are no pointers points to it. Keep the head pointer and the tail poiner (if you have one) updated. 6/25/2021 14 Other types of linked list Double linked list 6/25/2021 15 '\0' a->next = b; z->next = NULL; b->next = c; node * head; node * a; node * tail; '\0' node * b; node * z; b->prev = a; a->prev = NULL; … Circular linked list … node * a; node * b; a->next = b; z->next = a; b->next = c; node * head; node * tail; struct node{ int id; char name[20];
Answered 1 days AfterJul 08, 2021

Answer To: 1.For two variables a and b created as follows unsigned int a=3, b=5; The value of b>>a is 1 True or...

Pulkit answered on Jul 10 2021
160 Votes
test had been completed
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here