Answer to Question #134971 in C++ for Qasim

Question #134971
Write a c++ program to create a structure to specify data on students roll number, name, year of joining. write a function to print names of all students who joined in particular year and print data of students whose roll number is given.
Hint (Use Structure Array and search data from structure array for particular year& rollno)
1
Expert's answer
2020-09-25T08:58:36-0400
#include<iostream>

using namespace std;

// Struct student contain roll number, name, year of joining
struct student {
    int roll_number{}, year_of_joining{};
    string name;
};

// Function to print the names of all students who joined in a given year
void print_by_year(student students[], int n, int year) {
    cout << "Student(s) joined in " << year << " year:\n";
    for (int i = 0; i < n; i++)
        if (students[i].year_of_joining == year)
            cout << students[i].roll_number << " " << students[i].name << "\n";
    cout << endl;
}

// Function to print the details of student of given roll number
void print_by_roll_number(student students[], int n, int roll) {
    cout << "Student(s) with " << roll << " roll number:\n";
    for (int i = 0; i < n; i++)
        if (students[i].roll_number == roll) {
            cout << students[i].name << " " << students[i].year_of_joining << endl;
        }
}

int main() {
    student students[5];

    students[0].name = "Jacob Williams";
    students[0].roll_number = 1;
    students[0].year_of_joining = 2018;

    students[1].name = "Michael Davis";
    students[1].roll_number = 2;
    students[1].year_of_joining = 2018;

    students[2].name = "Andrew Robinson";
    students[2].roll_number = 3;
    students[2].year_of_joining = 2019;

    students[3].name = "Emma Davis";
    students[3].roll_number = 4;
    students[3].year_of_joining = 2020;

    students[4].name = "Ethan Wright";
    students[4].roll_number = 5;
    students[4].year_of_joining = 2018;

    print_by_year(students, 5, 2018);

    print_by_roll_number(students, 5, 4);

}


Output:







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Qasim
26.09.20, 16:16

thank you so much very helpful :D

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS