LAB 12 Structures: EKT 120 - Computer Programming Laboratory Module
LAB 12 Structures: EKT 120 - Computer Programming Laboratory Module
LAB 12
STRUCTURES
1. OBJECTIVES:
2. INTRODUCTION:
Structures are collections 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.
Example:
struct book
{
char title[20];
float price;
int numpages;
int year;
};
There are two ways to declare a structure. For example, given a variable name fiction_book, it can
be declared as:
2
EKT 120 – Computer Programming Laboratory Module
We can also make structures to be in arrays, it is called arrays of structures. For example,
struct employeeType
{
char firstName[20];
char lastName[20];
int personID;
char deptID[10];
double monthlySalary
double monthlyBonus;
};
To access members of structures, we use the “.” (dot) operator. Refer to the previous fiction_book
for the following examples.
The struct employeeType contains structures of nameType, addressType, dateType and also
contactType.
3
EKT 120 – Computer Programming Laboratory Module
newEmployee.salary = 45678.00;
newEmployee.name.first = "Mary";
newEmployee.name.middle = "Beth";
newEmployee.name.last = "Simmons";
#include <stdio.h>
int main()
{ struct book my_book = {25.50,690,2005}; //declare and initialize my_book
struct book her_book; //declare her_book
printf("\nEnter her book price : ");
scanf("%f", &her_book.price); //to access price member of her_book
printf("\nEnter number of pages of her book : ");
scanf("%d", &her_book.numpages); //to access numpages member of her_book
printf("\nEnter year published of her book : ");
scanf("%d", &her_book.year); //to access year member of her_book
My book :
25.50 690 2005
Her book :
30.00 150 2008
4
EKT 120 – Computer Programming Laboratory Module
The following program shows the use of structures with functions. The structures variables are
passed by value.
#include <stdio.h>
struct book
{
float price;
int numpages;
int year;
};
struct book read(); //the return type of the function is a structure; another
//way to return more than one value from a function.
void print(struct book, struct book); //function receives two structures as parameters
int main()
{ struct book my_book = {25.50,690,2005};
struct book she_book;
she_book = read(); //call function read, the return value will be received
//by she_book
print(my_book, she_book);
My book :
25.50 690 2005
Her book :
30.00 150 2008
3. TASKS:
3.1 a. List two major differences between a variable that is an array and a variable that is of a
structured data type. Which would you use to store the catalog description of a course? To
store the names of students in the course?
3.2 a. Define a structure type sSquareColumnT to represent a square timber column. Components
should include the type of wood (a string of less than 20 characters), the length and width of the
column in inches, and the maximum compressive strength of the wood in whole pounds per
square inch. Here is a variable sq_col that it should be possible to declare using your data type.
sq_col
6
EKT 120 – Computer Programming Laboratory Module
(b) Define a structure type inventory containing character array acPartName[30], integer
iPartNumber, floating point fPrice, integer iStock, and integer iReorder.
(c) Define a struct called address, containing character arrays acStreetAddress[25], acCity[20],
acState[3] and acZipcode[6].
(d) Define a structure type cat_num_t to represent a catalog number consisting of a two- or three-
character category code followed by an integer. Then define a structure type cat_entry_t to
represent a catalog entry. Each entry has a catalog number, a description that is a string of up
to 19 characters, a wholesale price, and a retail price.
(e) Define two output functions for the data types you defined in Question 3.2.d: print_cat_num
and print_cat_entry. Output of a catalog entry should resemble the following example.
Number: EXC-412
Description: 2400 BAUD MODEM
Wholesale price: $79.86
Retail price: $119.99
7
EKT 120 – Computer Programming Laboratory Module
(f) Define an interactive input function for catalog numbers. Name the function fnScanCatNum.
The function should prompt the user as follows.
Function fnScanCatNum should take a single structured output argument through which it
stores the catalog number entered. The function should return 1 for successful input, 0 for
input error, and EOF if endfile is encountered before any data is obtained.
#include<stdio.h>
struct tag{
int i;
char acName[32];
} s1={10, "ABCD"};
int main() {
struct tag s2;
s2.i = 2*s1.i;
strcpy(s2.acName, s1.acName);
strcat(s2.acName, "1234");
printf("s2.i = %d\n", s2.i);
printf("s2.acName= %s\n", s2.acName);
return 0;
}
8
EKT 120 – Computer Programming Laboratory Module
b. Give the output of the following program:
#include <stdio.h>
struct tag {
char *p;
char acName[32];
} s = {"abcd", "ABCD"};
int main() {
printf("s.p = %s\n", s.p);
printf("s.p[1] = %c\n", s.p[1]);
printf("s.acName = %s\n", s.acName);
printf("s.acName[0] = %c\n", s.acName[0]);
printf("s.acName[1] = %c\n", s.acName[1]);
printf("*s.acName = %c\n", *s.acName);
return 0;
}
#include<stdio.h>
struct tag
{
int i;
int j;
} s1;
int main()
{
struct tag s;
s = funct(1);
printf("s.i = %d\n", s1.i);
printf("s.j = %d\n", s1.j);
s = funct(2);
printf("s.i = %d\n", s1.i);
printf("s.j = %d\n", s1.j);
return 0;
}
9
EKT 120 – Computer Programming Laboratory Module
#include <stdio.h>
struct data {
int a;
float b[10];
} DATA;
int main() {
DATA d;
struct data *p;
d.a = 10;
d.b[2] = 10;
p = &d;
p->a = d.a;
p->b[1]= 2*d.b[2];
printf("p->a= %d\n", p->a);
printf("p->b[1]= %f\n", p->b[1]);
return 0;
}
3.3 Define a structure type to represent a common fraction. Write a program that gets a fraction and
displays both the fraction and the fraction reduced to lowest terms using the following code
fragment:
frac = get_fraction();
print_fraction(frac);
printf(“=”);
print_fraction(reduce_fraction(frac);
Sample output 1:
Input fraction (num/den) : 8/6
8/6 = 4/3
Sample output 2:
Input fraction (num/den) : 2/6
2/6 = 1/3
10
EKT 120 – Computer Programming Laboratory Module
11
EKT 120 – Computer Programming Laboratory Module
3.4 Numeric addresses for computers on the international network internet are composed of four
parts, separated by periods, of the form
xx.yy.zz.mm
where xx, yy, zz and mm are positive integers. Locally, computers are usually known by a
nickname as well. You are designing a program to process a list of Internet addresses, identifying
all pairs of computer from the same locality. Create a structure type called address_t with
components for the four integers of an internet address and a fifth component in which to store an
associated nickname of 10 characters. Your program should read a list of up to 100 addresses
and nicknames terminated by a sentinel address of all zeros and a sentinel nickname.
Sample data
111.22.3.44 platte
555.66.7.88 wabash
111.22.5.66 green
0.0.0.0 none
The program should display a list of message identifying each pair of computers from the same
locality – that is, each pair of computers with matching values in the first two components of the
address. In the message, the computers should be identified by their nicknames.
Example Message
Machines platte and green are on the same local network.
Follow the message by a display of the full list of addresses and nicknames. Include in your
program a fnScanAddress function, a fnPrintAddress function, and a fnLocalAddress function.
Function fnLocalAddress should take two address structures as input parameters and return 1 (for
true) if the addresses are on the same local network, and 0 (for false) otherwise.
12
EKT 120 – Computer Programming Laboratory Module
13
EKT 120 – Computer Programming Laboratory Module
3.5 Table 12.1 shows a list of best seller books in a book store. Write a program to calculate the total
price for all the books sold. If the quantity sold is greater than or equal to 40 copies, a discount of 10%
will be deducted from the book’s price. Display book’s title, price, quantity, total price, discounted
amount and price after discount for each book, and finally calculate the total of amount payable to the
book’s supplier.
Table 12.1
Requirement:
(a) Define and declare struct bookType with title (char), price (float), qty (int) and total
(float) as members.
(b) Declare an array named discount to store calculated discount amount.
(c) Declare an array named after_discount to store calculated total amount after
discount.
(d) Declare an array of structure bookType named book to store all the information for
five (5) books.
Sample Output:
14
EKT 120 – Computer Programming Laboratory Module
15