CSS 2101 DATA
STRUCTURES
TUTORIALS
Tutorial-1
1. Using pointers, traverse a static array and count how many
elements are even and how many are odd.(Use case: Processing
sensor values or survey data.)
2. Write a function to reverse the elements of a static array in-place
using pointers.(Use case: Undoing operations or reversing user-
entered data.)
3. Given marks of 5 students in 3 subjects (use 2D array), use
pointers to:
• Calculate total marks of each student
• Find average marks of each subject
( Use case: Academic report generation)(Note: Not to use structures)
Solution-1
Tutorial 1 solutions
#include <stdio.h>
int main() {
int data[] = {12, 7, 5, 64, 22, 91, 33, 18}; // Example sensor/survey
data
int evenCount = 0, oddCount = 0;
int *ptr = data; // Pointer to the array
int size = sizeof(data) / sizeof(data[0]);
for (int i = 0; i < size; i++) {
if (*(ptr + i) % 2 == 0)
evenCount++;
else
oddCount++; }
printf("Total even numbers: %d\n", evenCount);
printf("Total odd numbers: %d\n", oddCount);
return 0;
}
Solution-2
Tutorial 1 solutions
#include <stdio.h> int main() {
void reverseArray(int *arr, int size) { int data[] = {10, 20, 30, 40,
int *start = arr; 50, 60};
int *end = arr + size - 1; int size = sizeof(data) /
while (start < end) { sizeof(data[0]);
// Swap the values pointed by start and end
int temp = *start; printf("Original array: ");
*start = *end; for (int i = 0; i < size; i++) {
*end = temp; printf("%d ", data[i]);
// Move the pointers }
start++; reverseArray(data, size);
end--; printf("\nReversed array: ");
} for (int i = 0; i < size; i++) {
} printf("%d ", data[i]);
}
return 0;
Solution-3 Tutorial 1 solutions
// Calculate average marks for each subject
#include <stdio.h> for (int j = 0; j < SUBJECTS; j++) { int sum =
0;
#define STUDENTS 5 for (int i = 0; i < STUDENTS; i++) {
#define SUBJECTS 3 sum += *(ptr + i * SUBJECTS + j); }
float average = (float)sum / STUDENTS;
void calculateTotalsAndAverages(int printf("Average marks in Subject %d: %.2f\n", j
marks[STUDENTS][SUBJECTS]) { + 1, average);
int *ptr = &marks[0][0]; }
}
// Calculate total marks for each int main() {
student int marks[STUDENTS][SUBJECTS] = {
for (int i = 0; i < STUDENTS; i++) { {85, 90, 78},
int total = 0; {76, 88, 91},
for (int j = 0; j < SUBJECTS; j++) {90, 92, 85},
{ {70, 75, 80},
total += *(ptr + i * SUBJECTS {88, 86, 84}
+ j); };
} calculateTotalsAndAverages(marks);
printf("Total marks of Student return 0;
%d: %d\n", i + 1, total); }
}
Tutorial-2
1. Solve Tower of Hanoi for n disks using Recursion and print the moves.(Use
Case: Recursive problem-solving patterns, puzzle-solving algorithms.)
2. Dynamically allocates memory using calloc() for an array of integers to
store attendance status of n students (where n is entered by the user).
• 1 → Present
• 0 → Absent
• Initially, all values should be set to zero (absent).
• Then, accept a list of roll numbers of students who are present and update their status
in the array.
• Finally, display the list of present and absent students based on their roll numbers.
3. Given a dynamically allocated 2D array, write functions to:
• Print a specific row.
• Print a specific column.
• Replace all elements of a selected row with a given value.
(Use Case: Spreadsheet row/column operations.)
Tutorial - 3
Define a structure for student records with:
• Roll Number (int), Name (string), Marks in 3 Subjects (array of 3 integers),Total Marks (int
calculated),Average Marks (float — calculated)
• Use Arrays of structures (for student lists)
• Pointers to structures (for traversal and manipulation)
• Recursive functions (for searching and sorting operations)
1. Write a recursive Bubble Sort function to sort student records in ascending order of Roll
Numbers. (Use pointer arithmetic to swap structures.)
2. After sorting the student list by Roll Number, write a recursive function to search for a
student by Roll Number using Binary Search.
3. Implement Recursive Insertion Sort to sort students by their Average Marks
(descending).
4. Write a recursive function that counts how many students passed in all subjects
(assuming pass mark = 40).