Professional Documents
Culture Documents
Abdul Rafay CS Red 154
Abdul Rafay CS Red 154
Abdul Rafay CS Red 154
Abdul Rafay
Class:
BS-CS(Red)
Roll no:
B23F0389CS154
Subject:
PROGRAMMING
FUNDAMENTALS
Real life examples
i. Array:
Student Grades:
In this program, the grades are stored in an array
`studentGrades`, and the `calculateAverage` function is
used to find the average of these grades. The user is
prompted to input grades for each student, and the
program then calculates and displays the average grade.
Code:
#include <iostream>
Using namespace std;
Int main() {
Int studentGrades[MAX_STUDENTS];
Return 0;
}
```
#include <iostream>
#include <iomanip>
#include <string>
Using namespace std;
Int main() {
// Array to store monthly expenses
Double monthlyExpenses[NUM_MONTHS];
// Display results
Cout << fixed << setprecision(2);
Cout << “\nTotal expenses for the year: $” <<
totalExpenses << endl;
Cout << “Average monthly expense: $” <<
averageExpense << endl;
Return 0;
}
```
I
ii. Functions:
Data Validation:
In a web application a function might be responsible
for validating user input before inserting it to ensure
data integrity
Code:
#include <iostream>
#include <limits>
Using namespace std;
Int main() {
Int userInput;
Return 0;
}
Int main() {
Ifstream inputFile(“input.txt”);
Ofstream outputFile(“encrypted.txt”);
inputFile.close();
outputFile.close();
} else {
Cout << “Unable to open files.” << endl;
}
Return 0;
}
Int main() {
Ifstream inputFile(“example.txt”);
If (inputFile.is_open()) {
String line;
inputFile.close();
} else {
Cout << “Unable to open file.” << endl;
}
Return 0;
}
To-do list:
In this program, file handling is used to read the
existing to-do list from a file, display it to the user,
and allow the user to add a new task. The updated
to-do list is then written back to the file. File
handling is a practical way to persistently store and
retrieve data, making it suitable for applications like
task management systems.
#include <iostream>
#include <fstream>
#include <cstring>
Int main() {
Char tasks[MAX_TASKS][MAX_TASK_LENGTH];
Int taskCount = 0;
If (strcmp(newTask, “exit”) != 0) {
If (taskCount < MAX_TASKS) {
Strcpy(tasks[taskCount], newTask);
++taskCount;
Return 0;
}