Design a class Student. Include data members rollno, name, city and age. Write member functions:
i) To accept information of ‘n’ students
ii) To display information of ‘n’ students
iii) To search details of a student using rollno
(use array of objects)
Iv. Calculate Average age.
#include <iostream>
using namespace std;
class Student
{
string rollno;
string name;
string city;
int age;
};
void read_info(Student *students, int n) {
for (int i = 0; i < n; i++) {
cin >> students[i].rollno
>> students[i].name
>> students[i].city
>> students[i].age >> endl;
}
}
void display_info(Student *students, int n) {
for (int i = 0; i < n; i++) {
cout << "Rollno: " << students[i].rollno << "\n"
<< "Name: " << students[i].name << "\n"
<< "City: " << students[i].city << "\n"
<< "Age: " << students[i].age < "\n";
}
}
Student *find(Student *students, int n, string rollno) {
int idx = -1;
for (int i = 0; i < n; i++) {
if (students[i].rollno == rollno) idx = i;
}
if (idx != -1) return &students[i].
else return nullptr;
}
double avg_age(Student *students, int i) {
double total = 0;
for (int i = 0; i < n; i++)
total += students[i].age;
return total / n;
}
Comments
Leave a comment