Course: CS-303 Programming Fundamentals
Topics: Problem Analysis, Program Design, Multiple Functions, Arrays and Function-Based Programming
After this lecture, students will be able to:
Suppose a university needs a simple program to manage student records.
The program should:
If we write all instructions inside main(), the program may become difficult to understand.
Instead, we will divide the program into separate functions.
Problem: Develop a Student Management System for five students. Each student has marks in three subjects.
| 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 |
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
Before developing the complete program, we need to understand how arrays can be passed to functions.
#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.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.
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
Now we will combine the concepts into one 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;
}
| 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:
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.
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
Write a C++ program to manage records of five employees using functions.
Tasks:
Requirements:
Write a C++ program to manage five books in a library.
Tasks:
Requirements:
Write a C++ program to manage the results of ten students.
Tasks:
Requirements:
Ask students:
calculateTotal() reused in displayStudents()?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