Las arrays se utilizan para almacenar conjuntos de datos de tipos de datos similares en ubicaciones de memoria contiguas. A diferencia de las arrays, las estructuras son tipos de datos definidos por el usuario que se utilizan para almacenar grupos de elementos de tipos de datos no similares. Aquí, vamos a compilar un programa en C++ que almacenará la información de los estudiantes en una Estructura.
La siguiente es la información que almacenaremos en la estructura:
- Nombre del estudiante (string)
- Número de Lista del Estudiante (String)
- Sujetos inscritos (array de strings)
- Marcas en cada materia (Array of Int)
- CGPA (flotante)
Ejemplos:
Yash Gupta S20200010234 [DSA, OOPS ,DBMS, CCN] [89,78,86,90] 8.918
para un estudiante
C++
// C++ program to demonstrate // a structre for student details #include <bits/stdc++.h> using namespace std; // Structure Of students struct student { string name; // Student Name string rollno; // Student Roll Number vector<string> subjects; // Subjects Enrolled(Array) vector<int> marks; // Marks in each subject(Array) float cgpa; // Student's CGPA }; // Function to print a vector(for more "Different ways to // print elements of vector" at GFG) template <typename S> void printv(const vector<S>& v) { cout << "[ "; // Iterating over all elements of vector for (auto elem : v) { cout << elem << " "; } cout << "]"; cout << endl; } // Function to print a Student void printStudent(student* s) { cout << "Student Details:" << endl; cout << endl; cout << "Name: " << s->name << endl; cout << "Roll Number: " << s->rollno << endl; cout << "Subjects: "; printv(s->subjects); cout << "Marks: "; printv(s->marks); cout << "CGPA " << s->cgpa << endl; } // Driver Code int main() { // New Student student s; // Declaring all the information of a student s.name = "GeeksforGeeks"; s.rollno = "S20200010234"; s.subjects = { "DSA", "OOPS", "DBMS", "CCN" }; s.marks = { 89, 78, 86, 90 }; s.cgpa = 8.918; // Function call to print a Student printStudent(&s); return 0; }
Student Details: Name: GeeksforGeeks Roll Number: S20200010234 Subjects: [ DSA OOPS DBMS CCN ] Marks: [ 89 78 86 90 ] CGPA 8.918
Para un estudiante (array)
Estos dos detalles también se agregarán con el detalle anterior para mostrar el uso de la estructura con múltiples parámetros.
GFG S20200010164 [DSA, OOPS ,DBMS, CCN] [89,80,89,80] 8.45
gfg Roll Number: S20200010169 Subjects: [ DSA OOPS DBMS CCN ] Marks: [ 99 0 99 90 ] CGPA 9.47
C++
// C++ program to demonstrate a // structre for multiple student details #include <bits/stdc++.h> using namespace std; // Structure Of students struct student { string name; string rollno; // Subjects Enrolled(Array) vector<string> subjects; // Marks in each subject(Array) vector<int> marks; // Student's CGPA float cgpa; }; // Function to print a vector // (for more "Different ways to // print elements of vector" at GFG) template <typename S> void printv(const vector<S>& v) { cout << "[ "; // Iterating over all elements of vector for (auto elem : v) { cout << elem << " "; } cout << "]"; cout << endl; } // Function to print a Student void printStudent(student* s) { cout << "Student Details:" << endl; cout << endl; cout << "Name: " << s->name << endl; cout << "Roll Number: " << s->rollno << endl; cout << "Subjects: "; printv(s->subjects); cout << "Marks: "; printv(s->marks); cout << "CGPA " << s->cgpa << endl; } // Driver Code int main() { // Array of Students student arrayofstudnets[10]; // Student 1 arrayofstudnets[0].name = "GeeksforGeeks"; arrayofstudnets[0].rollno = "S20200010234"; arrayofstudnets[0].subjects = { "DSA", "OOPS", "DBMS", "CCN" }; arrayofstudnets[0].marks = { 89, 78, 86, 90 }; arrayofstudnets[0].cgpa = 8.918; // Student 2 arrayofstudnets[1].name = "GFG"; arrayofstudnets[1].rollno = "S20200010164"; arrayofstudnets[1].subjects = { "DSA", "OOPS", "DBMS", "CCN" }; arrayofstudnets[1].marks = { 89, 80, 89, 80 }; arrayofstudnets[1].cgpa = 8.45; // Student 3 arrayofstudnets[2].name = "gfg"; arrayofstudnets[2].rollno = "S20200010169"; arrayofstudnets[2].subjects = { "DSA", "OOPS", "DBMS", "CCN" }; arrayofstudnets[2].marks = { 99, 00, 99, 90 }; arrayofstudnets[2].cgpa = 9.47; // Loop to print all students for (int i = 0; i < 3; i++) { // Function call printStudent(&arrayofstudnets[i]); } return 0; }
Student Details: Name: GeeksforGeeks Roll Number: S20200010234 Subjects: [ DSA OOPS DBMS CCN ] Marks: [ 89 78 86 90 ] CGPA 8.918 Student Details: Name: GFG Roll Number: S20200010164 Subjects: [ DSA OOPS DBMS CCN ] Marks: [ 89 80 89 80 ] CGPA 8.45 Student Details: Name: gfg Roll Number: S20200010169 Subjects: [ DSA OOPS DBMS CCN ] Marks: [ 99 0 99 90 ] CGPA 9.47
Publicación traducida automáticamente
Artículo escrito por yashguptaaa333 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA