In: Computer Science
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines:
1. Create an Array of 5 Structures of Student Records
2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student and update its Marks of any particular subject.
Here we just want to create a program with array of structure and array within structure.
And here search value is the students id number.
#include <cstring>
#include <iostream>
#include <ostream>
using namespace std;
//create the structure
struct student
{
string name;
string id;
int phone_number;
int mark[3];
};
int main(){
struct student stud[5];
int i,search ,choice, subnum;
for(i=0; i<5; i++){ //taking values from user
cout << "\nStudent " << i + 1 << endl;
cout << "Enter roll no" << endl;
cin >> stud[i].id;
cout << "Enter name" << endl;
cin >> stud[i].name;
cout << "Enter phone number" << endl;
cin >> stud[i].phone_number;
for(int j=0;j<3;j++)
{
cout << "Enter Marks " << j+1 << " : ";
cin >> stud[i].mark[j];
}
}
for(i=0;i<5;i++){ //printing values
cout << "\nStudent " << i + 1 << endl;
cout << "Roll no : " << stud[i].id << endl;
cout << "Name : " << stud[i].name << endl;
cout << "Phone no : " << stud[i].phone_number << endl;
for(int j=0;j<3;j++)
{
cout << "mark of sub"<<j+1<<" : "<< endl;
cout << stud[i].mark[j]<<endl;
}
}
//ask for a search value
cout<<"Enter phone number(for searching) "<<endl;
cin>>search;
for(i=0;i<5;i++)
{ //conpare the search value with orginal value
if(stud[i].phone_number==search)
{
cout << "\nResult was" << i + 1 << endl;
cout << "Roll no : " << stud[i].id << endl;
cout << "Name : " << stud[i].name << endl;
cout << "Phone no : " << stud[i].phone_number << endl;
for(int j=0;j<3;j++)
{
cout << "mark of sub"<<j+1<<" : "<< endl;
cout << stud[i].mark[j]<<endl;
}
//ask for manipulation
cout<<"do you want to edit the marks?press 1 for yes,2 for no!"<<endl;
cin >>choice;
if(choice ==1){
cout<<"enter the subject number" <<endl;
cin>>subnum;
cout << "Enter new Mark"<< " : ";
cin >> stud[i].mark[subnum-1];
}
}}
for(i=0;i<5;i++){ //printing final values
cout << "\nEditted results " << i + 1 << endl;
cout << "Roll no : " << stud[i].id << endl;
cout << "Name : " << stud[i].name << endl;
cout << "Phone no : " << stud[i].phone_number << endl;
for(int j=0;j<3;j++)
{
cout << "mark of sub"<<j+1<<" : "<< endl;
cout << stud[i].mark[j]<<endl;
}
}
return 0;
}
This is the code.. Also including screen shots.
Unfortunately my device didn't accept input more than 2 lines so take only 2 students.
Output :
Please refer this screen shot for actual code incase of any errors.