0% found this document useful (0 votes)
8 views

Programming_in_C_Series_2_Answers

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming_in_C_Series_2_Answers

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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("text.txt", "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;
}

You might also like