0% found this document useful (0 votes)
196 views6 pages

C Programming Exam Answers - GXEST204

The document contains exam questions and answers for the course GXEST204 - Programming in C at St. Thomas Institute for Science and Technology. It includes topics such as formal and actual parameters, structures vs unions, pointer usage, sorting algorithms, file handling, and basic operations like swapping and calculating sum and mean of an array. Each section provides code examples and explanations for various programming concepts.

Uploaded by

BINDUANN THOMAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views6 pages

C Programming Exam Answers - GXEST204

The document contains exam questions and answers for the course GXEST204 - Programming in C at St. Thomas Institute for Science and Technology. It includes topics such as formal and actual parameters, structures vs unions, pointer usage, sorting algorithms, file handling, and basic operations like swapping and calculating sum and mean of an array. Each section provides code examples and explanations for various programming concepts.

Uploaded by

BINDUANN THOMAS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

GXEST204 – Programming in C: Series 2

Exam Answers
St. Thomas Institute for Science and Technology
Department of First Year Engineering
Internal Assessment Test II – April 2025
Semester: 2
Course Code: GXEST204
Course Name: Programming in C (Common to Group A & B)
Maximum Marks: 40
Duration: 2 Hours

PART A – Answer all questions


1. Explain formal parameters and actual parameters. Illustrate
with an example.

 Formal parameters are variables defined in the function


declaration or definition to receive values from the calling
function. Actual parameters are the values passed to the
function at the time of call.

#include <stdio.h>
void display(int a) { // formal parameter
printf("Value: %d\n", a);
}
int main() {
int num = 10;
display(num); // actual parameter
return 0;
}

2. Differentiate between structure and union.

Feature Structure Union

Memory Allocates memory for Shares memory among


all members members

Access All members can be One member at a time


used

Size Sum of all members Size of largest


member

Use Case Use when all data Use when only one
needed data at a time

3. Write a C program to find largest among two numbers using


pointers.

#include <stdio.h>
int main() {
int a, b;
int *p1, *p2;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
p1 = &a;
p2 = &b;
if (*p1 > *p2)
printf("Largest: %d\n", *p1);
else
printf("Largest: %d\n", *p2);
return 0;
}

4. Differentiate between address operator (&) and indirection (*)


operator.

 Address-of operator (&) gives the memory address of a


variable. Indirection operator (*) accesses the value stored
at the address.

Example: int *p = &x; // & gives address, *p gives value

PART B – Answer any one full question from each module

Module III
5(a). Write a C program to implement Bubble sort using functions.

#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
void display(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {5, 2, 9, 1, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
display(arr, n);
return 0;
}

5(b). Explain call by value and call by reference with examples.

Call by Value: A copy of variable is passed, original not


modified.

Call by Reference: Address is passed, changes reflect back.

6. Program using structure Vehicle and sort by price.

#include <stdio.h>
#include <string.h>

struct Vehicle {
char model[20];
int year;
float price;
};

int main() {
int n;
printf("Enter number of vehicles: ");
scanf("%d", &n);
struct Vehicle v[n], temp;
for (int i = 0; i < n; i++) {
printf("Enter model, year and price: ");
scanf("%s %d %f", v[i].model, &v[i].year, &v[i].price);
}

for (int i = 0; i < n-1; i++)


for (int j = i+1; j < n; j++)
if (v[i].price < v[j].price) {
temp = v[i];
v[i] = v[j];
v[j] = temp;
}

printf("Sorted Vehicles by Price:\n");


for (int i = 0; i < n; i++)
printf("%s %d %.2f\n", v[i].model, v[i].year,
v[i].price);

return 0;
}

Module IV
7. C program to read file and count characters, words, and lines
(without inbuilt functions).

#include <stdio.h>

int main() {
FILE *fp = fopen("[Link]", "r");
if (fp == NULL) {
printf("File not found.\n");
return 1;
}

char ch;
int charCount = 0, wordCount = 0, lineCount = 0, inWord = 0;

while (1) {
ch = getc(fp);
if (ch == EOF)
break;

charCount++;
if (ch == '\n')
lineCount++;

if (ch == ' ' || ch == '\t' || ch == '\n') {


inWord = 0;
} else if (inWord == 0) {
inWord = 1;
wordCount++;
}
}

fclose(fp);
printf("Characters: %d\nWords: %d\nLines: %d\n", charCount,
wordCount, lineCount);
return 0;
}

8(a). C program to swap two numbers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}

8(b). C program using pointers to compute Sum and Mean of array.

#include <stdio.h>

int main() {
int n;
printf("Enter size of array: ");
scanf("%d", &n);
float arr[n], sum = 0, mean;
float *p = arr;

printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%f", &p[i]);
sum += p[i];
}

mean = sum / n;
printf("Sum = %.2f\nMean = %.2f\n", sum, mean);
return 0;
}

Common questions

Powered by AI

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).

You might also like