Professional Documents
Culture Documents
Structures in Advanced Programming
Structures in Advanced Programming
STRUCTURE
Astructureisagroupofelementswhichareofdifferenttype.Eachoftheelementsisidentified
byitsownidentifierandtheyarecalledmember.
A structure can be thought of as an object without any member functions. The important
propertyofstructuresisthatthedatainastructurecanbeacollectionofdataitemsofdiversetypes.
SourceCode:
Output:
RMDA
Page1
AdvancedProgrammingLanguages:Structure
Astructurevariablecanholdvaluesjustlikeanyothervariablecan.Astructurevalueisacollectionof
smallervaluescalledmembervalues.
SourceCode:
Output:
#include<iostream>
usingnamespacestd;
structStudent{
intid;
charname[30];
doublegrade;
};
voidnewLine();
intmain()
{
Studentstud;
cout<<"Enterstudentrecords:\n";
cout<<"ID:";
cin>>stud.id;
newLine();
cout<<"Name:";
cin.getline(stud.name,29);
cout<<"Grades:";
cin>>stud.grade;
cout<<"\n\n";
cout<<"Displaystudentrecords\n";
cout<<"ID:"<<stud.id<<endl;
cout<<"Name:"<<stud.name<<endl;
cout<<"Grades:"<<stud.grade<<endl;
system("pause>0");
return0;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
RMDA
Page2
AdvancedProgrammingLanguages:Structure
ArraysofStructure
SourceCode:
#include<iostream>
#include<iomanip>
usingnamespacestd;
structitem{
intid;
charname[50];
doubleprice;
};
voidnewLine();
intmain()
{
itemitm[5];
inti;
cout<<"ENTER5ITEMS:\n";
for(i=0;i<5;i++)
cout<<"\nITEM"<<(i+1)<<endl;
cout<<"ID:";
cin>>itm[i].id;
newLine();
cout<<"Name:";
cin.getline(itm[i].name,49);
cout<<"Price:";
cin>>itm[i].price;
cout<<setw(10)<<"Item#"
<<setw(16)<<"ItemCode"
<<setw(30)<<"Name"
<<setw(10)<<"Price";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
for(i=0;i<5;i++)
cout<<endl;
cout<<setw(10)<<i+1
<<setw(16)<<itm[i].id
<<setw(30)<<itm[i].name
<<setw(10)<<itm[i].price;
system("pause>0");
return0;
}
RMDA
Page3
AdvancedProgrammingLanguages:Structure
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Output:
StructuresandFunctions
SourceCode:
#include<iostream>
usingnamespacestd;
structItemRec
{
intid;
charname[50];
doubleprice;
}item;
voidinputItem();
voiddisplayItem();
voidnewLine();
intmain()
{
cout<<"EnterItemInformation\n";
inputItem();
displayItem();
system("pause>0");
RMDA
Output:
Page4
AdvancedProgrammingLanguages:Structure
return0;
}
voidinputItem()
{
cout<<"ID:";
cin>>item.id;
newLine();
cout<<"Name:";
cin.getline(item.name,49);
cout<<"Price:";
cin>>item.price;
}
voiddisplayItem()
{
cout<<"\n\nItemInformation:\n";
cout<<"ID:"<<item.id<<endl;
cout<<"Name:"<<item.name<<endl;
cout<<"Price:"<<item.price<<endl;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
InitializationofStructure
SourceCode:
#include<iostream>
usingnamespacestd;
structemployeeRecord
{
intid;
charname[50];
doublesalary;
};
intmain()
{
employeeRecordemp={101,"JuanDelaCruz",15000.0};
cout<<"EMPLOYEERECORD\n";
cout<<"ID:"<<emp.id<<endl;
cout<<"Name:"<<emp.name<<endl;
cout<<"Salary:"<<emp.salary<<endl;
cout<<"\n\nEnternewsalary:";
cin>>emp.salary;
RMDA
Page5
AdvancedProgrammingLanguages:Structure
cout<<endl;
cout<<"EMPLOYEENEWRECORD\n";
cout<<"ID:"<<emp.id<<endl;
cout<<"Name:"<<emp.name<<endl;
cout<<"Salary:"<<emp.salary<<endl;
system("pause>0");
return0;
Output:
StructuresandPointers
SourceCode:
#include<iostream>
usingnamespacestd;
structEmployeeRec{
intid;
charname[50];
charaddress[100];
charcontactNo[15];
doublesalary;
};
voidnewLine();
intmain()
{
EmployeeRec*emp;
emp=newEmployeeRec;
cout<<"Enteremployee'srecord:\n";
cout<<"ID:";
cin>>emp>id;
newLine();
cout<<"Name:";
cin.getline(emp>name,49);
cout<<"Address:";
cin.getline(emp>address,99);
cout<<"ContactNumber:";
RMDA
Page6
AdvancedProgrammingLanguages:Structure
cin.getline(emp>contactNo,14);
cout<<"Salary:";
cin>>emp>salary;
cout<<endl;
cout<<"EmployeeInformation\n";
cout<<"ID:"<<emp>id<<endl;
cout<<"Name:"<<emp>name<<endl;
cout<<"Address:"<<emp>address<<endl;
cout<<"ContactNumber:"<<emp>contactNo<<endl;
cout<<"Salary:"<<emp>salary;
system("pause>0");
return0;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Output:
PassingStructurestoFunctions
SourceCode:
#include<iostream>
#include<string>
#include<iomanip>
usingnamespacestd;
structstudRecord{
intid;
charname[50];
doublegrades;
};
voidnewLine();
voidshowStudRecord(studRecordstud);
RMDA
Output:
Case1:
Page7
AdvancedProgrammingLanguages:Structure
intmain()
{
studRecords;
cout<<"EnterStudentRecord:\n";
cout<<"ID:";
cin>>s.id;
newLine();
cout<<"Name:";
cin.getline(s.name,49);
cout<<"Grades:";
cin>>s.grades;
cout<<endl;
showStudRecord(s);
system("pause>0");
return0;
}
voidshowStudRecord(studRecordstud)
{
cout<<"StudentRecord\n";
cout<<"ID:"<<stud.id<<endl;
cout<<"Name:"<<stud.name<<endl;
cout<<"Grades:"<<stud.name<<endl;
if(stud.grades<75)
cout<<"Remarks:FAILED";
else
cout<<"Remarks:PASSED";
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Case2:
SourceCode:
#include<iostream>
usingnamespacestd;
//CertificateofDepositAccountStructure
structCDAccount{
doublebalance;
doubleinterestRate;
intterm;
};
CDAccountdoubleInterest(CDAccountoldAccount);
voidaccountBalance(CDAccount&_account);
RMDA
Page8
AdvancedProgrammingLanguages:Structure
intmain()
{
CDAccountaccount;
cout<<"Enteraccountbalance:PhP";
cin>>account.balance;
cout<<"Enteraccountinterest:";
cin>>account.interestRate;
cout<<"Enterthenumberofmonthsuntilmaturity:";
cin>>account.term;
accountBalance(account);
cout<<"\nOldAccount\n"
<<"WhenyourCDmaturesin\n"
<<account.term<<"months,\n"
<<"itwillhaveabalanceofPhP"
<<account.balance<<endl;
CDAccountaccountNew;
accountNew=doubleInterest(account);
accountBalance(accountNew);
cout<<"\nNewAccount\n"
<<"WhenyourCDmaturesin"
<<accountNew.term<<"months,\n"
<<"itwillhaveabalanceofPhP"
<<accountNew.balance<<endl;
system("pause>0");
return0;
}
voidaccountBalance(CDAccount&_account)
{
doublerateFraction,interest;
rateFraction=_account.interestRate/100.0;
interest=_account.balance*(rateFraction*(_account.term/12.0));
_account.balance=_account.balance+interest;
}
CDAccountdoubleInterest(CDAccountoldAccount)
{
CDAccounttemp;
temp=oldAccount;
temp.interestRate=2*oldAccount.interestRate;
returntemp;
}
RMDA
Page9
AdvancedProgrammingLanguages:Structure
Output:
StructureDeclaredinaStructure
Declaration1
SourceCode
Declaration2
#include<iostream>
#include<string>
usingnamespacestd;
structStudent_Details{
charstudentName[50];
intage;
struct{
chartitle[50];
doubleprice;
}Book_Bought;
};
voidnewLine();
intmain()
{
Student_Detailssd;
cout<<"ENTERTHEFOLLOWINGINFORMATION:\n";
cout<<"Name:";
cin.getline(sd.studentName,49);
cout<<"Age:";
RMDA
Page10
AdvancedProgrammingLanguages:Structure
cin>>sd.age;
newLine();
cout<<"BookTitle:";
cin.getline(sd.Book_Bought.title,49);
cout<<"BookPrice:";
cin>>sd.Book_Bought.price;
cout<<"\n\n";
cout<<"INFORMATIONDETAILS:\n";
cout<<sd.studentName<<endl;
cout<<sd.age<<endl;
cout<<sd.Book_Bought.title<<endl;
cout<<sd.Book_Bought.price<<"Pesos"<<endl;
system("pause>0");
return0;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Output:
SourceCode:
#include<iostream>
usingnamespacestd;
structStudentInfo{
intid;
struct{
charlastName[30];
charfirstName[30];
}name;
struct{
intday;
intmonth;
intyear;
}birthDay;
charaddress[50];
charcontactNo[15];
};
RMDA
Page11
AdvancedProgrammingLanguages:Structure
voidnewLine();
intmain()
{
StudentInfostud;
cout<<"Enterstudentinformation\n";
cout<<"ID:";
cin>>stud.id;
newLine();
cout<<"FirstName:";
cin.getline(stud.name.firstName,29);
cout<<"LastName:";
cin.getline(stud.name.lastName,29);
cout<<"Address:";
cin.getline(stud.address,49);
cout<<"DateofBirth:\n";
cout<<"Month:";
cin>>stud.birthDay.month;
cout<<"Day:";
cin>>stud.birthDay.day;
cout<<"Year:";
cin>>stud.birthDay.year;
cout<<"ContactNumber:";
cin>>stud.contactNo;
cout<<"\nDisplayStudentInformation\n";
cout<<"ID:"<<stud.id<<endl;
cout<<"Name:"<<stud.name.lastName<<","
<<stud.name.firstName<<endl;
cout<<"Address:"<<stud.address<<endl;
cout<<"BirthDay:"
<<stud.birthDay.month<<"/"
<<stud.birthDay.day<<"/"
<<stud.birthDay.year<<endl;
cout<<"ContactNumber:"<<stud.contactNo<<endl;
system("pause>0");
return0;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
RMDA
Page12
AdvancedProgrammingLanguages:Structure
Output:
Structure:DynamicMemoryAllocation
SourceCode:
#include<iostream>
usingnamespacestd;
structItem
{
intcode;
charname[50];
doubleprice;
};
voiddisplayItem(Item*_itm);
voidnewLine();
intmain()
{
Item*itm;
itm=newItem;
cout<<"EnterItemInformation:\n";
cout<<"Code:";
cin>>itm>code;
newLine();
cout<<"Name:";
cin.getline(itm>name,49);
cout<<"Price:";
cin>>itm>price;
displayItem(itm);
system("pause>0");
return0;
}
voiddisplayItem(Item*_itm)
{
cout<<"\n\nDisplayItemInformation:\n";
RMDA
Page13
AdvancedProgrammingLanguages:Structure
cout<<"Code:"<<_itm>code<<endl;
cout<<"Name:"<<_itm>name<<endl;
cout<<"Price:"<<_itm>price<<endl;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Output:
SourceCode:
#include<iostream>
#include<iomanip>
usingnamespacestd;
structSubject{
charcode[10];
chardescription[50];
floatunits;
};
voidnewLine();
intmain()
{
intnoOfSubject;
cout<<"Howmanysubjectsdoyouwanttoenter?";
cin>>noOfSubject;
newLine();
Subject*subj=newSubject[noOfSubject];
for(inti=0;i<noOfSubject;i++)
cout<<"Entersubject"<<i+1<<endl;
cout<<"Code:";
cin.getline(subj>code,9);
cout<<"Description:";
cin.getline(subj>description,49);
cout<<"Unit(s):";
cin>>subj>units;
RMDA
Page14
AdvancedProgrammingLanguages:Structure
newLine();
subj++;
subj=noOfSubject;
cout<<"\n\nListofSubjects\n";
cout<<setw(10)<<"Code"
<<setw(40)<<"Description"
<<setw(10)<<"Units";
cout<<endl;
for(inti=0;i<noOfSubject;i++)
cout<<setw(10)<<subj>code
<<setw(40)<<subj>description
<<setw(10)<<subj>units<<endl;
subj++;
system("pause>0");
return0;
}
voidnewLine()
{
chars;
do{cin.get(s);}while(s!='\n');
}
Output:
RMDA
Page15