Programming_in_C_Series_2_Answers
Programming_in_C_Series_2_Answers
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
#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;
}
Use Case Use when all data Use when only one
needed data at a time
#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;
}
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;
}
#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);
}
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++;
fclose(fp);
printf("Characters: %d\nWords: %d\nLines: %d\n", charCount,
wordCount, lineCount);
return 0;
}
#include <stdio.h>
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
#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;
}