You are on page 1of 9

Faculty of Computer Science and Information

Technology

BIC10404:
Struktur Data/
Data Structure

LAB 4, SEMESTER 2, SESSION 2018/2019

Lecturer: MISS NORHAMREEZA BINTI ABDUL HALIM

NAME SITI NUR AMIERAH BINTI MOHD SO’AD


MATRIC NO. AI180078
SECTION 8
QUESTION 1
Instruction: Answer ALL the questions.

1. Given the following diagram:

start
Alia Burn Muthu Chun
NULL
1530.20 6970.50 4905.00 3280.40

Figure 1: Linked List


a) Write a structure to define a record containing:
i) character array of size 100 which comprises name of employee
- char name[100];
ii) a floating point of salary
- float salary;
iii) a pointer for the next node
- struct myRecord*next;

b) Write code segmentations for the following operation:


i) display all the records
curr= start;
printf("\nLIST OF ALL RECORDS");
for(i=1; i<=5; i++)
{
while(curr->next!=NULL)
{
printf("\n\nName= %s ", curr->data1);
printf("\nSalary= RM%.2f ", curr->data2);
curr=curr->next;
}
ii) insert at the end of the linked list
for(i=1; i<=5; i++)
{
printf("\nEnter name: ");
scanf("%s", &name);
printf("\nEnter salary(RM): ");
scanf("%f", &salary);
newPtr=malloc(sizeof(myRecord));
if(newPtr!=NULL)
{
strcpy(newPtr->data1, name);
newPtr->data2=salary;
newPtr->next=NULL;
}
if(start==NULL)
start=newPtr;
else
{
newPtr->next=start;
start=newPtr;
}
}
iii) delete the first node of the linked list
curr=start;
start =start->next;
free (curr);

2. Write a program that allow user to input 5 records into a linked list. Each record contains
students’ name and test marks. This program also contains of:
a) display all the records in the linked list

void printRecord (record * head){

record * curr = head;

for(int i = 0; i< 5; i++) {

puts(curr->name);

printf(“%.2f”, curr->marks);

curr = curr->next;

}
b) display all the students’ name whose test mark is less than 50.
void printFail(record * head) {

record * curr = head;

puts(“Students who scored less than 50:);

for(int i = 0; i< 5; i++) {

if (curr->marks < 50)

puts(curr->name);

curr = curr->next;

c) write function for the following task:


(i) A function called minMarks to display the student’s name with minimum
mark

void minMarks (record * head) {

float min = head->marks;

char stdName[70] = head->name;

record * curr = head;

for(int i = 0; i< 5; i++){

if(curr->marks < min) {

min = curr->marks;

stdName = curr->name;

curr = curr->next;

printf(“The student who scored the minimum mark: %s”, stdName);

}
(ii) A function called maxMarks to display the student’s name with maximum
mark

void maxMarks (record * head) {

float max = head->marks;

char stdName[70] = head->name;

record * curr = head;

for(int i = 0; i< 5; i++){

if(curr->marks > max) {

max = curr->marks;

stdName = curr->name;

curr = curr->next;

printf(“The student who scored the maximum mark: %s”, stdName);

(iii) A function called avgMarks to calculate and display the average

void avgMarks (record * head) {

record * curr = head;

float avg = 0;

for(int i = 0; i< 5; i++){

avg += curr->marks

curr = curr->next;

avg /= 5;

printf(“Average mark: %.2f”, avg);

}
3. Table 1 shows classification of blood pressure based on the systolic blood pressure (SBP)
and diastolic blood pressure (DBP) level and recommendations for follow up defined by
The Seventh Report of the Joint National Committee on Prevention, Detection,
Evaluation and Treatment of High Blood Pressure.
Table 1: Classification of blood pressure and recommendations for follow up based
on initial blood pressure measurements for adults.

SBP DBP Blood Pressure


(mm Hg) (mm Hg) Category

less than 120 and less than 80 Normal

120 – 139 or 80 – 89 Prehypertension

High Blood Pressure


140 – 159 or 90 – 99 (Hypertension)
Stage 1
High Blood Pressure
160 or higher or 100 or higher (Hypertension)
Stage 2

higher than higher than


or Hypertensive Crisis
180 110

Based on Table 1, write a program that allows the user to input records into a linked list.
The total of the records are determined by the user. Each record contains name of patient,
age, SBP, DBP and blood pressure category. The blood pressure category is determined
by the SBP and DBP. In this program display:
a) all the patients’ information
b) the total and all the patients’ name whose in the Hypertensive Crisis category.
c) the total and all the patients’ name whose in the Normal category.
CODING
OUTPUT

You might also like