You are on page 1of 17

FAKULTI TEKNOLOGI KEJURUTERAAN

UNIVERSITI TEKNIKAL MALAYSIA MELAKA

COMPUTER PROGRAMMING

BETR 1343 SEMESTER 2 SESSION 2016/2017

LAB 5: STRUCTURE

DATE 3.5.2017

NAME OF GROUP MOHAMAD RASHDAN BIN MOHD SAID B071510749


MEMBERS & MATRIX
NUMBER

NAME OF INSTRUCTORS EN. MOHD HANIF BIN CHE HASAN

EN. RAMLAN BIN LATIP

EXAMINERS COMMENTS VERIFICATION STAMP

TOTAL MARKS

Document Number Issue Number


N/A 29.05.2015
Revision Number Total Pages
4.0 (A. I. A. Rahman)
LAB REPORT FORMAT FOR BETR 1343 COMPUTER PROGRAMMING

PART 1 : PRELAB
1. Type the following code, compile, and run it using your IDE.
#include <iostream>

using namespace std;

// declare the data structure


struct STUD
{
// declare 4 structure members
int number;
string id;
string name;
float score;
};

STUD student[100]; // we can have up until 100 sets of these data

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;

// display the 5 sets of data


cout << "INDEX\tID\t\tNAME\t\tSCORE" << endl;
cout << "=============================================" << endl;
cout << student[0].number << ". \t" << student[0].id << "\t" <<
student[0].name << "\t" << student[0].score << endl;
cout << student[1].number << ". \t" << student[1].id << "\t" <<
student[1].name << "\t" << student[1].score << endl;
cout << student[2].number << ". \t" << student[2].id << "\t" <<
student[2].name << "\t" << student[2].score << endl;

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;

cout << endl << endl;


return 0;
}

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;

// declare the data structure


struct student
{
// declare 4 structure members
int number;
char *id;
char *name;
float score;
}

student[100]; // we can have up until 100 sets of these data

int main()
{
int i;
student[100]=student[i];

struct student dummy;


// 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;
3
student[4].id = "B0369369";
student[4].name = "Lucius Fox";
student[4].score = 78.8;

// display the 5 sets of data


cout << "INDEX\tID\t\tNAME\t\tSCORE" << endl;
cout << "=============================================" << endl;
for(i=0; i<5; i++)
{

cout<<student[i].number << ". \t" << student[i].id << "\t" << student[i].name << "\t" <<
student[i].score << endl;

cout << endl << endl;


system ("pause");
return 0;
}

3. Modify your code further so that the new program will output these:

Which data number do you wish to see? 3

INDEX ID NAME SCORE


======================================================
3 B0999666 Solomon Grundy 76.5

Which data number do you wish to see? 5

INDEX ID NAME SCORE


======================================================
5 B0369369 Lucius Fox 78.8

Code:

#include <iostream>
using namespace std;

// declare the data structure


struct student
{
// declare 4 structure members
int number;
char *id;
char *name;
float score;
}

student[100]; // we can have up until 100 sets of these data

4
int main()
{
int i,num;
student[100]=student[i];

struct student dummy;


// 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;

cout<<"Which data number do you wish to see? ";


cin>>num;
cout<<endl<<endl;

// display the 5 sets of data


cout << "INDEX\tID\t\tNAME\t\tSCORE" << endl;
cout << "=============================================" << endl;

cout<<student[num].number << ". \t" << student[num].id << "\t" <<


student[num].name << "\t" << student[num].score << endl;

cout << endl << endl;


system ("pause");
return 0;
}

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);

How many sets of data do you wish to enter? 2

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:

INDEX ID NAME SCORE


================================================
1 B0255255 Edward Nygma 65.1
2 B0128128 Oswald Cobblepot 40.8

How many sets of data do you wish to enter? 4

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:

INDEX ID NAME SCORE


================================================
1 B0110112 Harvey Dent 90.6
2 B0512512 Bane Dorrance 30.9
3 B0000250 Amadeus Arkham 98.5
4 B0001738 Victor Fries 50.3
6
Code:
#include <iostream>
using namespace std;

// declare the data structure


struct student
{
char id[15];
char name[50];
float score;
}
student[100]; // we can have up until 100 sets of these data

int main()
{
int i,num;
struct student dummy;

cout<<"How many sets of data do you wish to enter? ";


cin>>num;
cout<<endl;

for (i=1;i<=num;i++)
{

cout<<"Enter ID "<<i<<" : ";


cin>>student[i].id;
cout<<"Enter Name "<<i<<" : ";
cin.ignore();
cin.getline(student[i].name,30);
cout<<"Enter Score "<<i<<" : ";
cin>>student[i].score;
cout<<endl;

cout<<endl;
cout<<"You entered:"<<endl<<endl<<endl;

cout << "INDEX\tID\t\tNAME\t\tSCORE" << endl;


cout << "=============================================" << 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

Section 1: Program Design 1

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

where n is your total semester.

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

Input Process Output


Insert number of Calculate cgpa Display cgpa
semester,semester and
cgpa

8
3) PROGRAM DESIGN

Flow Chart

Start

Insert number of semester

i=0
i++
i<num of sem

Insert GPA of semester

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:

struct cgpa studcgpa[100];


double gpa,cgpa=0;
int i,num_sem;
cout<<"How many semester you have been completed? ";
cin>>num_sem;
cout<<endl;

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;
}

cout<<"Your CGPA : "<<fixed<<setprecision(2)<<cgpa/num_sem<<endl;


system("pause");
system ("cls");

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

Section 2: Program Design 2

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

Display the movies day,name,director and time

Insert i = the day of movie

FALSE
i<5 Display invalid input

TRUE

Display the movies day[i],name[i],director[i] and time[i]

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"},
};

cout << "DAY \tMOVIE\t\t\tDIRECTOR\tTime "<< endl;


cout << "=========================================================" << endl;

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;

cout<<"Please Select Day"<<endl;


cout<<"[1] Monday \n[2] Tuesday \n[3] Wednesday \n[4] Thursday \n[5] Friday \n";
cin>>i;
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

You might also like