Professional Documents
Culture Documents
COMPUTER PROGRAMMING
LAB 5: STRUCTURE
DATE 3.5.2017
TOTAL MARKS
PART 1 : PRELAB
1. Type the following code, compile, and run it using your IDE.
#include <iostream>
int main()
{
// assign 5 sets of data into structure
student[0].number = 1;
student[0].id = "B0123456";
student[0].name = "Bruce Wayne";
student[0].score = 90.5;
student[1].number = 2;
student[1].id = "B0456789";
student[1].name = "Harley Quinn";
student[1].score = 85.7;
student[2].number = 3;
student[2].id = "B0999666";
student[2].name = "Solomon Grundy";
student[2].score = 76.5;
student[3].number = 4;
student[3].id = "B0755252";
student[3].name = "Selina Kyle";
student[3].score = 93.5;
student[4].number = 5;
student[4].id = "B0369369";
student[4].name = "Lucius Fox";
student[4].score = 78.8;
2
cout << student[3].number << ". \t" << student[3].id << "\t" <<
student[3].name << "\t" << student[3].score << endl;
cout << student[4].number << ". \t" << student[4].id << "\t" <<
student[4].name << "\t" << student[4].score << endl;
2. By using for or while loop, optimize the previous code so that it uses fewer lines
of codes, but achieve the same results.
Code:
#include <iostream>
using namespace std;
int main()
{
int i;
student[100]=student[i];
student[1].number = 2;
student[1].id = "B0456789";
student[1].name = "Harley Quinn";
student[1].score = 85.7;
student[2].number = 3;
student[2].id = "B0999666";
student[2].name = "Solomon Grundy";
student[2].score = 76.5;
student[3].number = 4;
student[3].id = "B0755252";
student[3].name = "Selina Kyle";
student[3].score = 93.5;
student[4].number = 5;
3
student[4].id = "B0369369";
student[4].name = "Lucius Fox";
student[4].score = 78.8;
cout<<student[i].number << ". \t" << student[i].id << "\t" << student[i].name << "\t" <<
student[i].score << endl;
3. Modify your code further so that the new program will output these:
Code:
#include <iostream>
using namespace std;
4
int main()
{
int i,num;
student[100]=student[i];
student[1].number = 2;
student[1].id = "B0456789";
student[1].name = "Harley Quinn";
student[1].score = 85.7;
student[2].number = 3;
student[2].id = "B0999666";
student[2].name = "Solomon Grundy";
student[2].score = 76.5;
student[3].number = 4;
student[3].id = "B0755252";
student[3].name = "Selina Kyle";
student[3].score = 93.5;
student[4].number = 5;
student[4].id = "B0369369";
student[4].name = "Lucius Fox";
student[4].score = 78.8;
5
4. Modify your code to create an empty structure. User will then dynamically key-in
the sets of data and the program will display what he entered on the console screen.
Sample output:
Hint:
cin.ignore(); //Use this code to enter name
getline(cin, student[i].name);
Enter ID 1: B0255255
Enter Name 1: Edward Nygma
Enter Score 1: 65.1
Enter ID 2: B0128128
Enter Name 2: Oswald Cobblepot
Enter Score 2: 40.8
You entered:
Enter ID 1: B0110112
Enter Name 1: Harvey Dent
Enter Score 1: 90.6
Enter ID 2: B0512512
Enter Name 2: Bane Dorrance
Enter Score 2: 30.9
Enter ID 3: B0000250
Enter Name 3: Amadeus Arkham
Enter Score 3: 98.5
Enter ID 4: B0001738
Enter Name 4: Victor Fries
Enter Score 4: 50.3
You entered:
int main()
{
int i,num;
struct student dummy;
for (i=1;i<=num;i++)
{
cout<<endl;
cout<<"You entered:"<<endl<<endl<<endl;
for(i=1;i<=num;i++)
{
cout<<i<< ". \t" << student[i].id << "\t" << student[i].name << "\t" << student[i].score <<
endl;
7
cout << endl << endl;
system("PAUSE");
return 0;
}
PART 2 : LAB
1) REQUIREMENT
Your CGPA
You have to create a program to calculate your current CGPA.
You will ask the user to key in their CPA for every semester. Then calculate their CGPA
CGPA Formula = (GPA1+GPA2+....... +GPAn)/n
Note :
Cumulative Grade Point Average (CGPA) is the cumulative point average obtain for the
semester that have been complete.
Grade Point Average (GPA) is the grade point average obtained in particular semester.
Sample Output:
2) ANALYSIS
8
3) PROGRAM DESIGN
Flow Chart
Start
i=0
i++
i<num of sem
Calculate GGPA
Display CGPA
End
9
Code
// Student Name: Mohamad Rashdan bin Mohd Said Student ID: B071510749
// Lab Title: Data Structure using struct Date: 03/05/2017
#include <iostream>
#include <iomanip>
using namespace std;
struct cgpa
{
char semester[40];
double gpa;
};
int main()
{
y:
for (i=1;i<=num_sem;i++)
{
cout<<"Enter Semester: ";
cin.ignore();
cin.getline(studcgpa[i].semester,30);
cout<<"Enter Your GPA for semester "<<studcgpa[i].semester<<" : ";
cin>>gpa;
cgpa+=gpa;
10
cout<<endl;
}
goto y;
}
.
4) TESTING
Show the input you have inserted and also the program output console.
Test 1
Input Semester have been completed: 2
Enter semester: 1-2015/2016
Enter cgpa for semester 1-2015/2016: 3.3
Enter semester: 2-2015/2016
Enter cgpa for semester 2-2015/2016: 3.4
Output
Test 2
Input Semester have been completed: 2
Enter semester: 1-2016/2017
Enter cgpa for semester 1-2016/2017: 3.6
Enter semester: 2-2016/2017
Enter cgpa for semester 2-2016/2017: 3.7
11
Output
Test 3
Input Semester have been completed: 2
Enter semester: 1-2013/2014
Enter cgpa for semester 1-2013/2014: 3.0
Enter semester: 2-2013/2014
Enter cgpa for semester 2-2013/2014: 3.26
Output
1) REQUIREMENT
Movie Schedule 1
Write program to display the movie schedule for 5 days.
Your schedule should have
- Day
- Title
- Directors
- Time
2) ANALYSIS
Input Process Output
Select the day Declare variables Display the movies
Define struct movie Day,
day[i],name[i],director[i] and
Movie, Director, Time
time[i]
12
3) PROGRAM DESIGN
Flow Chart
Start
Declare variables
Define struct movie Day, Movie, Director, Time
i=0
i++
i<5
FALSE
i<5 Display invalid input
TRUE
End
13
Code
#include <iostream>
#include <iomanip>
using namespace std;
struct movie
{
int day;
char *title;
char *director;
char *time;
};
int main()
{
y:
int i;
struct movie MOVIE[5]=
{
{1,"Rambo 7","\tUzi","9.30am"},
{2,"Transformer","Afiq","10.30am"},
{3,"Stuart Little","Nadia","8.30am"},
{4,"Fast & Furious","Audi","6.30am"},
{5,"Die Hard 5","Leong","4.30am"},
};
for(i=0;i<5;i++)
{
cout<<MOVIE[i].day<<".\t"<<MOVIE[i].title<<"\t\t"<<MOVIE[i].director<<"\t\t"
14
<<MOVIE[i].time<<endl;
}
cout<<endl;
if ((i>6)&&(i<1))
{
cout<<"Invalid Input!"<<endl;
}
else
{
cout << "DAY \tMOVIE \t\tDIRECTOR\tTime "<< endl;
cout <<
"============================================================="
<<endl;
cout<<MOVIE[i-1].day<<".\t"<<MOVIE[i-1].title<<"\t\t"
<<MOVIE[i-1].director<<"\t\t"<<MOVIE[i-1].time<<endl;
}
system("pause");
system ("cls");
goto y;
return 0;
}
15
4) TESTING
Show the input you have inserted and also the program output console.
Test 1
Input i=2
Output
Test 2
Input i=5
Output
Test 3
Input i=1
16
Output
5) POSSIBLE IMPROVEMENTS
Describe how you could improve your program if you had more time. What
improvements would make the program faster, more general, simpler, or easier to
understand?
Understanding coding language
Involve in a programmer forum
Ask better programmer about coding like programmer forum on social media
More practice with using others coding and try to find an error
6) COMMENTS
Evaluate the lab. What did you learn from it? Was it too hard? Too easy? Suggest
improvements if you can.
This lab session teach us how write a programming code using Dev C++. With an
error appear when running that programming will teach us how to solve the coding
problem and understanding it. For a beginner programmer it is quite difficult to solve
the coding problem and we had to try more practice or do pre lab. The improvement
for this problem is gave to us more a simple coding practice with an error and we
need to find that error. For a beginner programmer, simple program is good enough to
try and improve the coding skill with it.
17