C Programming Exam Answers - GXEST204
C Programming Exam Answers - GXEST204
A programmer would prefer 'call by reference' when they need to modify the actual input parameters of the function or when needing to return multiple results. This method provides access to the variable directly, allowing changes to affect the original variables. Conversely, 'call by value' is suitable when the function does not need to modify the original variables as only copies are passed. 'Call by reference' thus allows for more efficient memory use and manipulation of data directly .
The Bubble sort algorithm is implemented using functions in C by iteratively passing through the array, comparing adjacent elements, and swapping them if they are in the incorrect order. This process is repeated for all elements, gradually placing the largest elements at the end. The Bubble sort is inefficient for larger datasets because it requires multiple passes through the data and has a best-case time complexity of O(n^2).
The C program reads each character from a text file using a `while` loop that continues until the end of the file (EOF). It counts characters unconditionally, increments the line counter on newline `\n`, and identifies word boundaries using spaces, tabs, or newlines. When non-boundary characters are found after a boundary, the word count increases. This process handles characters, words, and lines in a single pass .
In C programming, the address-of operator `&` retrieves the memory address of a variable, which is fundamental in pointer usage. The indirection operator `*` accesses or modifies the value stored at the memory address that a pointer references. Thus, they complement each other: `&` is used to get a pointer to a variable, while `*` is used to access the variable from its pointer .
Formal parameters are variables defined in the function declaration or definition intended to receive values from the calling function. Actual parameters are the values provided to the function at the time of the call. For example, in the C code snippet: `void display(int a)` is using `a` as a formal parameter while the call `display(num);` uses `num` as an actual parameter .
Using a structure is more beneficial than a union when dealing with complex data entities that require simultaneous storage and access to multiple elements. This is particularly pertinent when records of fixed-form entries or when consistency in data handling is essential, such as employee records or student databases, where every field needs to be maintained and accessed independently .
Using pointers enables accessing and comparing the values stored in certain memory locations without directly manipulating the variables themselves. In the provided C program snippet, two pointers `p1` and `p2` are used to point to integers `a` and `b`. The program compares the values of the integers through pointers and prints the largest by using dereferencing `*p1` and `*p2` .
In C programming, a structure allocates separate memory space for each of its members, allowing simultaneous use of all members. Meanwhile, a union shares the same memory space among all its members, only permitting one member's storage at a time, effectively requiring the programmer to use only one member at any time due to its memory-sharing nature .
To compute the sum and mean of an array using pointers in C, one initializes a pointer to point to the array, iterates through the elements using pointer arithmetic, and accumulates a sum. Finally, dividing this sum by the number of elements results in the mean. This approach is advantageous because it avoids indexing, thus directly accessing memory locations, which can enhance efficiency and improve clarity by abstracting array index operations .
Pointers enhance memory efficiency in swapping operations by allowing direct access and modification of the actual variables in memory rather than making copies. This minimizes the overhead of copying data and supports memory conservation, as the operations only require temporary pointers and a minimal amount of additional storage (just one temporary variable).