Answer Scheme - Lab Structure

EKT120: COMPUTER PROGRAMMING
LAB MODULE v1.0
LAB 11: STRUKTUR
Puan Salina Mohd Asi
Pusat Pengajian Kejuruteraan Komputer Dan Perhubungan
Kolej Universiti Kejuruteraan Utara Malaysia
Objective:
1. Introduction to Structure
2. Definition and declaration of structure
3. Array of structure
4. How to access member of structure
5. Structure within a structure
1. Introduction
Structure is a collection of related data items called components (or members) that are not
necessarily of the same data type. Commonly used to define records to be stored in files
Usually the collections of related data item are characteristics of an object
 Object
Characteristics
 Book
Price, number of pages, year published
 Car
Price, year, model, colour
 Student
Name, matric_no, semester
2. Definition and declaration of a structure
Before a structure can be use, we need to define the structure
 Syntax
struct <StructureTypeName> {
structure member declaration list
};
 E.g.
struct book {
float price;
int numpages;
int year;
};
Note: struct is the keyword to define the structure.
There are two ways to declare a structure,
1. Example: consider the above structure definition.
struct <structure name> <variable_name>;
struct book buku_latihan;
2. or it can straight away after the definition
struct book {
float price;
int numpages;
int year;
} buku_latihan;
3. Arrays of Structure
We can make structure to be in array, it is call array of structure.
struct employeeType
{
char firstName[20];
char lastName[20];
int personID;
char deptID[10];
double yearlySalary;
double monthlySalary
double yearToDatePaid;
double monthlyBonus;
};
struct employeeType employees[50]; //We declare 50 element of array type structure of
employeeType
4. How to access member of structure
To access member of structure we use the “.” (dot) operator. Refer to the above buku_latihan for
the below example.
i. To assign the numpages member of buku_latihan with 120,
buku_latihan.numpages = 120; or
scanf(“%d”,&bukulatihan.numpages);
ii. To assign the price of buku_latihan with RM 1.00
buku_latihan.price = 1.00; or
scanf (“%d”,&buku_latihan.price);
iii. To print the member of the structure
printf(“Bil mukasurat buku latihan adalah %d”, bukulatihan.numpages);
printf(“Harga buku latihan adalah %f”,bukulatihan.price);
5. Structure within a structure
struct nameType
{ char first[30];
char middle[30];
char last[20];
};
struct addressType
{ char address1[40];
char address2[40];
char city[30];
char state;
char zip;
};
struct dateType
{ char month[2];
char day[2];
char year[4];
};
struct contactType
{ char phone[12];
char cellphone[12];
char fax[12];
char pager[12];
char email[50];
};
struct employeeType
{ nameType name;
char emplID[12];
addressType address;
dateType hiredate;
dateType quitdate;
contactType contact;
char deptID[10];
double salary;
};
Refer the above example the struc employeeType contain structure of addressType, dateType,
To access,
 newEmployee.salary = 45678.00;
 newEmployee.name.first = "Mary";
 newEmployee.name.middle = "Beth";
 newEmployee.name.last = "Simmons";
Question1:
Define a structure according to the below information,
a. Structure name - studentType
b. Members
i. name type of char, have 50 character
ii. nokp type of intrger
iii. test1 type of float
iv. test2 type of float
v. total type of float
struct studentType {
char name[50];
float test1;
float test2;
float total;
}
Declare the below variables,
a. aminah type of studentType.
struct studentType aminah;
b. pelajar is an array of studentType, the no of elements is 5.
struct studentType pelajar[5];
Question 2:
Below is a simple program using structure, look at the definition and declaration of structure and
how to access the information of the structure. Write, save, compile and understand the below
program.
#include <stdio.h>
struct book {
float price;
int numpages;
int year;
};
//definition of book structure
int main()
{
struct book my_book = {25.50,690,2005}; //declaration and initialize my_book
struct book her_book;
printf("Enter book price : ");
scanf("%f", &her_book.price); //To access price of her_book
printf("Enter number of pages : ");
scanf("%d", &her_book.numpages); //To access numpages of her_book
printf("Enter year published : ");
scanf("%d", &her_book.year); //to access year of her_book
printf("My book :\n");
//to access member of my_book
printf("%.2f\t%d\t%d\n", my_book.price, my_book.numpages, my_book.year);
printf("Her book :\n");
printf("%.2f\t%d\t%d\n", her_book.price, her_book.numpages, her_book.year);
if(my_book.year > her_book.year)
printf("My book is the latest publication\n");
else
printf("Her book is the latest publication\n");
return 0;
}
Write the output of the program.
Question 3
The below program is to demonstrate how structures use as parameters, and to pass structure
variable by value. Type, save, compile and understand the program below.
#include <stdio.h>
struct book {
float price;
int numpages;
int year;
};
struct book read(); //declare a prototype of function read.
void cetak(struct book, struct book);
void compare(int my, int );
//the return type of above function is a structure, another way to return more than one value
void print(struct book, struct book); //receiving two structures
void compare(int, int);
int main()
{
struct book my_book = {25.50,690,2005}; //declaration of my_book
struct book she_book; //declaration of she_book
she_book=read(); //call the function read, the return value will be received by she_book
cetak(my_book , she_book);
compare(my_book.year, she_book.year);
//passing the value of member of my_book and she_book i.e year
return 0;
}
struct book read()
{
struct book her_book;
printf("Enter book price : ");
scanf("%f", &her_book.price);
printf("Enter number of pages : ");
scanf("%d", &her_book.numpages);
printf("Enter year published : ");
scanf("%d", &her_book.year);
return(her_book);
}
void cetak(struct book my_book, struct book her_book)
{
printf("My book :\n");
printf("%.2f\t%d\t%d\n", my_book.price, my_book.numpages, my_book.year);
printf("Her book :\n");
printf("%.2f\t%d\t%d\n", her_book.price, her_book.numpages, her_book.year);
}
void compare(int my_year, int she_year)
{
if(my_year > she_year)
printf("My book is the latest publication\n");
else
printf("Her book is the latest publication\n");
}
Write down the output of the program
Book’s Title
Price (RM)
Quantity
Applications Programming in C++
Programming With C++
Digital Image Processing
Image Processing The Fundamentals
Introduction To MATLAB 6 For Engineers
79.10
49.50
70.50
125.60
59.95
40
20
30
10
50
Question 4
Problem Solving question.
Write a program to calculate the total price for purchasing book.
The Input data are: Book’s title, price & quantity.
The program must be able to calculate the total price for each book title.
If quantity books sold is greater than 40 then get discount 10%. Display book’s title, price,
quantity, total price for each title, discounted amount for each title and sum of amount payable to
the supplier.
Requirement:
1. Define and declare struct bookType with name(char), price(float), qty(int) and
total(float) as members.
2. Declare array discount to store calculated discount amount.
3. Declare array after_discount to store calculated total amount after have been discount.
4. Declare an array of structure bookType named book
Enter book name : bukuc
Enter book price : RM 79.10
Enter quantity ordered : 40
…….
Book's Title Price
bukuc
79.10
bukuD
49.50
bukuS
70.50
bukuW
125.60
bukuR
59.95
Quantity
40
20
30
10
50
Total
3164.00
990.00
2115.00
1256.00
2997.50
Total amount payable : RM 10222.75
Complete the below program:
#include <stdio.h>
#include <stdlib.h>
#define num_book 5
struct bookType{
char name[50];
float price;
int qty;
double total;
Discount
0.00
0.00
0.00
0.00
299.75
After discount
3164.00
990.00
2115.00
1256.00
2697.75
};
struct bookType inputbook( ); //function prototype.
int main()
{
struct bookType book[num_book];
float discount[num_book] ={0.0};
float afdiscount[num_book] = {0.0};
float all = 0.00;
int i;
//To get the input
for(i=0;i<num_book;i++)
{
book[i] = inputbook ();
if(book[i].qty > 40)
discount[i]= 0.10 * book[i].price * book[i].qty;
book[i].total = book[i].price * book[i].qty;
after_discount[i]=book[i].total - discount[i];
all = all + after_discount[i];
}
//print all records of book ordered
printf("Book's Title\tPrice\tQuantity\tTotal\tDiscount\tAfter discount\n");
for(i=0;i<num_book;i++)
printf("%s\t\t%.2f\t%d\t\t%.2lf\t%.2f\t%.2f\n", book[i].name,
book[i].price,book[i].qty, book[i].total, discount[i],after_discount[i]);
printf("\nTotal amount payable : RM %.2f\n", all);
return 0;
}
struct bookType inputbook ( )
{
struct bookType buku;
printf("Enter book name : ");
scanf("%s", buku.name);
printf("\nEnter book price : RM ");
scanf("%f", &buku.price);
printf("\nEnter quantity ordered : ");
scanf("%d", &buku.qty);
return (buku);
}