Every student's record must consist of Age, ID Number and Mark.
a) Declare a struct named PELAJAR which consists of the above component.
b) In the main function, declare a student array which consists of 20 students.
c) Create a function to read in the data for 20 students by using the student array and maximum number of students as parameters.
d) Create a function to display the data for all students by using the student array and maximum number of students as parameters.
e) Create a function to find a student with the highest mark.
f) Complete the program by creating the main function:
(i) Initialize the student array to dummy values.
(ii) Create appropriate function calls to call functions in c), d) and e).
My answer:
#include <iostream>
#include <string>
using namespace std;
const int size = 20; int i;
struct PELAJAR
{
int Age;
string ID_Number;
float Mark;
}students[size];
void read(PELAJAR students[size],int size)
{
for (i=0;i<size;i++)
{
cout << (i+1) << ": " << endl;
cout << "\tAge: ";
cin >> students[i].Age;
cout << "\tID Number: ";
cin >> students[i].ID_Number;
cout << "\tMark: ";
cin >> students[i].Mark;
cout <<endl;
}//for
}//Read
void display(PELAJAR students[size],int size)
{
cout << endl;
cout << "No\tAge\tID Number\tMark" << endl;
for (i=0;i<size;i++)
{
cout << (i+1) << "\t" << students[i].Age << "\t" << students[i].ID_Number
<< "\t" << students[i].Mark << endl;
}
}//display
void highest (void)
{
float highest = students[0].Mark;
int high=0;
for (i=1;i<size;i++)
{
if (students[i].Mark>highest)
{
highest=students[i].Mark;
high=i;
}
}
cout << endl;
cout << "Student with highest mark is student " << (high+1)
<< " ID Number: " << students[high].ID_Number
<< " with Mark: " << students[high].Mark;
cout << endl;
}
int main()
{
for (i=0;i<size;i++)
{
students[i].Age=0;
students[i].ID_Number="0";
students[i].Mark=0.0;
}
read(students,size);
display (students,size);
highest();
cin.ignore();
cin.get();
return 0;
}
No comments:
Post a Comment