Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Store information in a structure

In this example, we will store information in a structure

#include <iostream>

using namespace std;

struct student{
    string name;
    string rollno;
    string subject[3];
    int marks[3];
    float cgpa;
};

void display(struct student s){
    cout<<"Student Information"<<endl;
    cout<<"Name of student : "<<s.name<<endl;
    cout<<"Student Roll Number : "<<s.rollno<<endl;
    int len_S = sizeof(s.subject)/sizeof(s.subject[0]);
    for(int i = 0; i < len_S; i++){
        cout<<"Subject "<<i+1<<" : "<<s.subject[i]<<endl;
    }
    int len_M = sizeof(s.marks)/sizeof(s.marks[0]);
    for(int i = 0; i < len_M; i++){
        cout<<"Subject "<<i+1<<" Marks is :"<<s.marks[i]<<endl;
    }

    cout<<"Student CGPA : "<<s.cgpa<<endl;
}
int main(){
    struct student s1 = {"Daniyal","2023",{"OOPS","DSA","Math"},{24,15,36},8.9};

    display(s1);
    return 0;
}

Output:

Student Information
Name of student : Daniyal
Student Roll Number : 2023
Subject 1 : OOPS
Subject 2 : DSA
Subject 3 : Math
Subject 1 Marks is : 24
Subject 2 Marks is : 15
Subject 3 Marks is : 36
Student CGPA : 8.9

Explanation:

Step1 : Structure Definition: student

struct student {
    string name;
    string rollno;
    string subject[3];
    int marks[3];
    float cgpa;
};

A struct named student is declared with the following members:

  • name: A string to store the student’s name.
  • rollno: A string to store the student’s roll number.
  • subject[3]: An array of strings to store the names of up to three subjects.
  • marks[3]: An array of integers to store the marks obtained in the three subjects.
  • cgpa: A float to store the student’s CGPA (Cumulative Grade Point Average).

Step2 : Function: display()

void display(struct student s) {
    cout << "Student Information" << endl;
    cout << "Name of student : " << s.name << endl;
    cout << "Student Roll Number : " << s.rollno << endl;

    int len_S = sizeof(s.subject) / sizeof(s.subject[0]);
    for(int i = 0; i < len_S; i++) {
        cout << "Subject " << i + 1 << " : " << s.subject[i] << endl;
    }

    int len_M = sizeof(s.marks) / sizeof(s.marks[0]);
    for(int i = 0; i < len_M; i++) {
        cout << "Subject " << i + 1 << " Marks is : " << s.marks[i] << endl;
    }

    cout << "Student CGPA : " << s.cgpa << endl;
}

Function Breakdown:

  • Input: The function takes a student struct s as an argument, which contains the details of a student.
  • Display Information: The function prints various details:
    • The student’s name and roll number.
  • Calculate Lengths of Arrays:
    • To display the subjects and their corresponding marks, the lengths of the subject and marks arrays are calculated using sizeof:
      • int len_S = sizeof(s.subject) / sizeof(s.subject[0]);: This gives the number of subjects.
      • int len_M = sizeof(s.marks) / sizeof(s.marks[0]);: This gives the number of marks.
  • Display Subjects and Marks:
    • Two for loops iterate over the subjects and marks arrays, printing each subject and its corresponding marks.
  • Display CGPA: Finally, the student’s CGPA is printed.

Step3: Main Function

int main() {
    struct student s1 = {"Daniyal", "2023", {"OOPS", "DSA", "Math"}, {24, 15, 36}, 8.9};
    
    display(s1);
    return 0;
}

In the main() function:

  • A student variable s1 is initialized with:
    • name: "waqar"
    • rollno: "2023"
    • subject: An array of three subjects: {"oops", "dsa", "math"}
    • marks: An array of marks: {24, 15, 36}
    • cgpa: 8.9
  • The display(s1) function is called to print the details of s1.

    Leave a Reply

    Your email address will not be published.

    Need Help?