Learn with Yasir

Share Your Feedback

Lecture 6: Function-Based Problem Solving – Student Management System


Lecture 6: Function-Based Problem Solving – Student Management System

Course: CS-303 Programming Fundamentals
Topics: Problem Analysis, Program Design, Multiple Functions, Arrays and Function-Based Programming

1. Learning Outcomes

After this lecture, students will be able to:

  1. Analyze a real-life programming problem.
  2. Divide a problem into smaller tasks.
  3. Design functions for each task.
  4. Pass arrays to functions.
  5. Combine functions, arrays and loops in one program.
  6. Develop a simple Student Management System.

2. Introduction: Real-Life Problem

Suppose a university needs a simple program to manage student records.

The program should:

  • Store student roll numbers.
  • Store student names.
  • Store marks.
  • Calculate total and percentage.
  • Assign grades.
  • Display student results.
  • Search for a student by roll number.

If we write all instructions inside main(), the program may become difficult to understand.

Instead, we will divide the program into separate functions.

3. Problem Analysis

Problem: Develop a Student Management System for five students. Each student has marks in three subjects.

Required Operations

  1. Input student information.
  2. Calculate total marks.
  3. Calculate percentage.
  4. Determine grade.
  5. Display student results.
  6. Search for a student.

Identify the Functions

Function Purpose
inputStudents() Input student records
calculateTotal() Calculate total marks
calculatePercentage() Calculate percentage
calculateGrade() Determine grade
displayStudents() Display all records
searchStudent() Search by roll number

4. Program Design

Before writing code, we should understand how the program will work.

main()

Controls the program

inputStudents()

Accept student information

calculateTotal()

calculatePercentage()

calculateGrade()

searchStudent()

displayStudents()

Display student results

5. Understanding Arrays in Functions

Before developing the complete program, we need to understand how arrays can be passed to functions.

Example: Passing an Array to a Function

#include <iostream>
using namespace std;

void displayMarks(int marks[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << marks[i] << endl;
    }
}

int main()
{
    int marks[5] = {70, 80, 65, 90, 75};

    displayMarks(marks, 5);

    return 0;
}

Output:

70
80
65
90
75

Explanation:

  • marks[] receives the array.
  • size receives the number of elements.
  • The loop displays all array elements.
  • The array is passed to the function using its name.

Important: When an array is passed to a function in this way, the function can modify the original array elements. The size is not automatically passed, so we provide it separately.

6. Practical Example: Student Marks Management

Let’s first create a simple program that stores marks of five students and calculates their totals.

#include <iostream>
using namespace std;

void inputMarks(int marks[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << "Enter marks of student "
             << i + 1 << ": ";

        cin >> marks[i];
    }
}

int calculateTotal(int marks[], int size)
{
    int total = 0;

    for (int i = 0; i < size; i++)
    {
        total = total + marks[i];
    }

    return total;
}

void displayMarks(int marks[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << marks[i] << endl;
    }
}

int main()
{
    int marks[5];

    inputMarks(marks, 5);

    int total = calculateTotal(marks, 5);

    cout << "Total Marks: " << total << endl;

    displayMarks(marks, 5);

    return 0;
}

Sample Output:

Enter marks of student 1: 70
Enter marks of student 2: 80
Enter marks of student 3: 65
Enter marks of student 4: 90
Enter marks of student 5: 75

Total Marks: 380
70
80
65
90
75

7. Complete Mini Project: Student Management System

Now we will combine the concepts into one program.

Project Requirements

  • Store records of five students.
  • Store roll number, name and three subject marks.
  • Calculate total marks.
  • Calculate percentage.
  • Assign grades.
  • Display all student records.
  • Search for a student using roll number.

Complete C++ Program

#include <iostream>
#include <string>
using namespace std;

const int SIZE = 5;

// Function prototypes
void inputStudents(int roll[], string names[],
                   int marks[][3]);

int calculateTotal(int marks[]);

double calculatePercentage(int total);

char calculateGrade(double percentage);

void displayStudents(int roll[], string names[],
                     int marks[][3]);

void searchStudent(int roll[], string names[]);

// Input student records
void inputStudents(int roll[], string names[],
                   int marks[][3])
{
    for (int i = 0; i < SIZE; i++)
    {
        cout << "\nStudent " << i + 1 << endl;

        cout << "Enter Roll Number: ";
        cin >> roll[i];

        cout << "Enter Name: ";
        cin >> names[i];

        for (int j = 0; j < 3; j++)
        {
            cout << "Enter marks of subject "
                 << j + 1 << ": ";

            cin >> marks[i][j];
        }
    }
}

// Calculate total marks
int calculateTotal(int marks[])
{
    int total = 0;

    for (int i = 0; i < 3; i++)
    {
        total += marks[i];
    }

    return total;
}

// Calculate percentage
double calculatePercentage(int total)
{
    return (total / 300.0) * 100;
}

// Calculate grade
char calculateGrade(double percentage)
{
    if (percentage >= 80)
        return 'A';

    else if (percentage >= 70)
        return 'B';

    else if (percentage >= 60)
        return 'C';

    else if (percentage >= 50)
        return 'D';

    else
        return 'F';
}

// Display all student records
void displayStudents(int roll[], string names[],
                     int marks[][3])
{
    cout << "\n===== STUDENT RESULTS =====\n";

    for (int i = 0; i < SIZE; i++)
    {
        int total = calculateTotal(marks[i]);

        double percentage = calculatePercentage(total);

        char grade = calculateGrade(percentage);

        cout << "\nRoll Number: " << roll[i];

        cout << "\nName: " << names[i];

        cout << "\nTotal Marks: " << total;

        cout << "\nPercentage: " << percentage << "%";

        cout << "\nGrade: " << grade << endl;
    }
}

// Search student by roll number
void searchStudent(int roll[], string names[])
{
    int searchRoll;
    bool found = false;

    cout << "\nEnter Roll Number to Search: ";
    cin >> searchRoll;

    for (int i = 0; i < SIZE; i++)
    {
        if (roll[i] == searchRoll)
        {
            cout << "Student Found!" << endl;

            cout << "Name: " << names[i] << endl;

            found = true;

            break;
        }
    }

    if (!found)
    {
        cout << "Student Not Found!" << endl;
    }
}

// Main function
int main()
{
    int roll[SIZE];

    string names[SIZE];

    int marks[SIZE][3];

    inputStudents(roll, names, marks);

    displayStudents(roll, names, marks);

    searchStudent(roll, names);

    return 0;
}

Understanding the Program

Function Input Output / Action
inputStudents() Arrays Stores student records
calculateTotal() Marks array Returns total
calculatePercentage() Total marks Returns percentage
calculateGrade() Percentage Returns grade
displayStudents() Student arrays Displays results
searchStudent() Roll and name arrays Displays matching student

Important programming concepts used:

  • One-dimensional arrays
  • Two-dimensional arrays
  • Functions and prototypes
  • Function parameters
  • Return values
  • Loops
  • Conditional statements
  • Array passing
  • Function reusability

Note: This is a basic classroom project. It assumes valid input, unique roll numbers and marks between 0 and 100. The cin >> names[i] statement accepts single-word names.

8. Dry Run: Calculate Student Total

Suppose a student has the following marks:

Subject Marks
Programming 80
Mathematics 75
English 85

Function call:

calculateTotal(marks[0]);

Dry run:

Iteration marks[i] Total
1 80 80
2 75 155
3 85 240

Percentage:

\[ \frac{240}{300}\times100=80\% \]

Grade: A

9. Lab Exercises

Problem Statement 1: Employee Management System

Write a C++ program to manage records of five employees using functions.

Tasks:

  1. Input employee ID, name and basic salary.
  2. Create a function to calculate allowance.
  3. Create a function to calculate net salary.
  4. Create a function to display employee information.
  5. Search for an employee using employee ID.

Requirements:

  • Use arrays to store employee records.
  • Use separate functions for each operation.
  • Use parameters and return values.

Problem Statement 2: Library Book Management

Write a C++ program to manage five books in a library.

Tasks:

  1. Input book ID, title and price.
  2. Create a function to display all books.
  3. Create a function to search for a book using its ID.
  4. Create a function to calculate the total price of all books.

Requirements:

  • Use arrays.
  • Use functions for input, display and searching.
  • Use loops to process records.

Problem Statement 3: Result Management System

Write a C++ program to manage the results of ten students.

Tasks:

  1. Input student roll numbers and marks in five subjects.
  2. Calculate total marks using a function.
  3. Calculate percentage using a function.
  4. Assign grades using a function.
  5. Display students who have passed.
  6. Display the student with the highest percentage.

Requirements:

  • Use a two-dimensional array for marks.
  • Use separate functions.
  • Use loops and conditional statements.

10. Classroom Discussion

Ask students:

  1. Why do we divide a program into functions?
  2. Can one function call another function?
  3. Can we pass an array to a function?
  4. Why do we pass the array size separately?
  5. What is the difference between a function that displays a result and one that returns a result?
  6. Why is calculateTotal() reused in displayStudents()?
  7. What changes would be needed to manage 100 students instead of five?

11. Quick Quiz

Knowledge Check

0 / 5 answered

1. What is the main purpose of modular programming?

A. To write all instructions in main()

B. To divide a program into smaller functions

C. To avoid using variables

D. To remove loops

2. How do we pass an array to a function?

A. Using its array name

B. Using cout

C. Using return only

D. Using a loop name

3. What does calculateTotal() return in our project?

A. Student name

B. Roll number

C. Total marks

D. Grade

4. Why is SIZE declared as a constant?

A. To change it every time

B. To keep the array size fixed

C. To make it a string

D. To stop the program

5. Which function searches for a student by roll number?

A. inputStudents()

B. calculateGrade()

C. searchStudent()

D. calculateTotal()

Submit Ans