Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Department of Information Systems and Technologies

CTIS 152 – Data Structures and Algorithms


Spring 2020 - 2021
Lab Guide #5 – Week 4 – 1

OBJECTIVE : Nested Structures, Structures as Function Parameters

Instructor : Serpil TIN


Assistants : Ruşen ASAN, Hatice Zehra YILMAZ

1. At the end of the semester in Studio course, each team will submit a project. Each project has team number, subject and
project details (video prepared (Y/N), poster grade and jury grade). The information for all projects is kept in the text file
named “projects.txt”.

Write a C program that reads the information into a dynamically created NESTED structure array and displays the detailed
project information including the overall grade for each team.

(Use dynamic memory allocation for the nested structure array. The first line of the file consists the number of projects)

Write the following functions;


 readFromFile: gets a file pointer, the projects array and the number of projects as parameters, reads the
information for all projects from the file into the nested structure array.
 displayReport: gets the projects array and the number of projects as parameters, displays the detailed
information about the projects.
Overall grade = poster grade (%40) + jury grade (%60).

If the team prepared a video for the project, 10 pts bonus will be given to the team.

Project Name: LG5_Q1


File Name: Q1.cpp
projects.txt

5
1 climateChange Y 85.50 95
2 violenceAgainstwomen Y 90 100
3 animalRights N 80 82.5
4 pediatricDevelopment Y 90 97.5
5 climateChange N 82.50 95

Example Run:

Id Subject Video Poster Jury Bonus Overall


-- ------------------- ----- ------ ------ ----- -------
1 climateChange Y 85.50 95.00 10 101.20
2 violenceAgainstwomen Y 90.00 100.00 10 106.00
3 animalRights N 80.00 82.50 0 81.50
4 pediatricDevelopment Y 90.00 97.50 10 104.50
5 climateChange N 82.50 95.00 0 90.00
2. Volleyball teams’ information are kept in the text file “volleyball.txt”. You will implement a software to keep the teams’
information: name, points, match details consisting the number of matches played, wins and losses. USE NESTED
STRUCTURE ARRAY. Maximum number of teams is 20.

Write the following functions;


 readFromFile: reads the teams’ information from the file into an array of structures and returns the number of
teams.
 displayAll: displays the information of all teams as in the example run.
 findWinner: finds and returns the index of the team which has the maximum points.

Write a main program that reads and displays the team’s information. The program will also display the information of the
winner team.
Project Name: LG5_Q2
File Name: Q2.cpp
Example Run:
Team Name Pts Matches Wins Losses volleyball.txt
-------------------------------------------
sportoto 43 22 15 7 sportoto 43 22 15 7
arhavi 7 22 2 20 arhavi 7 22 2 20
ziraatbankasi 43 22 14 8 ziraatbankasi 43 22 14 8
halkbank 40 22 12 10 halkbank 40 22 12 10
tokat 16 22 6 16 tokat 16 22 6 16
bursa 34 22 12 10 bursa 34 22 12 10
arkasspor 50 22 16 6
arkasspor 50 22 16 6
fenerbahce 58 22 20 2
fenerbahce 58 22 20 2
galatasaray 44 22 15 7
istanbul 26 22 9 13 galatasaray 44 22 15 7
inegol 16 22 6 16 istanbul 26 22 9 13
sorgun 19 22 5 17 inegol 16 22 6 16
sorgun 19 22 5 17
The Winner team:
fenerbahce 58 22 20 2

3. A vending machine will be simulated; the product list of the machine is stored in the file named “products.txt” with the
name, unit price and quantity. The first line of the file consists of the number of products.

Initially the program reads all the products’ information from the file into a dynamically created structure array and displays
them on the screen. While reading the product name you should use %[^0-9].
Then the program gets the product number from the customer and if the product does not run out of in the machine,
customer inserts the money, otherwise gives an error message. After the purchase is finished, the remaining money should
be returned back to the customer. If the inserted money is not enough to purchase the product, program gives an error
message. When the product is run out of, the quantity of the item should be displayed OUT in the menu as in the example
run. When the user enters -1 for the product number, program terminates.

Write the following functions;


 readFromFile : takes a file pointer, a structure array and a size as parameters. It reads the text file into the
structure array and returns the size.
 display : takes a structure array and a size as parameters. It displays the content of the structure array. If the
quantity is 0, it should display the quantity as “OUT”.
Project Name: LG5_Q3
File Name: Q3.cpp
products.txt
10
Mr. Goodbar 1.00 10
M & Ms. 0.60 1
Potato Chips 4.40 2
Chewing Gum 1.20 1
Pretzels 0.45 6
Hershey Kisses 1.25 11
Kracker Jacks 0.80 4
Oreos 0.55 3
Fritos 0.45 1
Milky Way 0.65 10
Example Run:

PRODUCT UNIT PRICE QUANTITY


1. Mr. Goodbar 1.00 10
2. M & Ms. 0.60 1
3. Potato Chips 4.40 2
4. Chewing Gum 1.20 1
5. Pretzels 0.45 6
6. Hershey Kisses 1.25 11
7. Kracker Jacks 0.80 4
8. Oreos 0.55 3
9. Fritos 0.45 1
10. Milky Way 0.65 10

Enter the product number to purchase (to stop -1):4


Insert the money: 1.40
0.20 TL returned back

PRODUCT UNIT PRICE QUANTITY


1. Mr. Goodbar 1.00 10
2. M & Ms. 0.60 1
3. Potato Chips 4.40 2
4. Chewing Gum 1.20 OUT
5. Pretzels 0.45 6
6. Hershey Kisses 1.25 11
7. Kracker Jacks 0.80 4
8. Oreos 0.55 3
9. Fritos 0.45 1
10. Milky Way 0.65 10

Enter product number to purchase (to stop -1):4


THERE IS NO MORE Chewing Gum

PRODUCT UNIT PRICE QUANTITY


1. Mr. Goodbar 1.00 10
2. M & Ms. 0.60 1
3. Potato Chips 4.40 2
4. Chewing Gum 1.20 OUT
5. Pretzels 0.45 6
6. Hershey Kisses 1.25 11
7. Kracker Jacks 0.80 4
8. Oreos 0.55 3
9. Fritos 0.45 1
10. Milky Way 0.65 10

Enter product number to purchase (to stop -1):3


Insert the money: 2.50

Your money is not enough, and returned back

PRODUCT UNIT PRICE QUANTITY


1. Mr. Goodbar 1.00 10
2. M & Ms. 0.60 1
3. Potato Chips 4.40 2
4. Chewing Gum 1.20 OUT
5. Pretzels 0.45 6
6. Hershey Kisses 1.25 11
7. Kracker Jacks 0.80 4
8. Oreos 0.55 3
9. Fritos 0.45 1
10. Milky Way 0.65 10

Enter product number to purchase (to stop -1):6


Insert the money: 1.50
0.25 TL returned back

PRODUCT UNIT PRICE QUANTITY


1. Mr. Goodbar 1.00 10
2. M & Ms. 0.60 1
3. Potato Chips 4.40 2
4. Chewing Gum 1.20 OUT
5. Pretzels 0.45 6
6. Hershey Kisses 1.25 10
7. Kracker Jacks 0.80 4
8. Oreos 0.55 3
9. Fritos 0.45 1
10. Milky Way 0.65 10

Enter product number to purchase (to stop -1):-1

************************
Today 2 products sold
Total payment is: 2.45 TL
Additional Questions
AQ1.
The weekly temperature for 15 cities is kept in the weather.txt file.

Write a C program that reads the number of cities(n) from the user and reads the information for n cities from the text file
into the dynamically created two-dimensional array. The program displays the temperatures on the screen, then finds and
displays the coldest city with temperature and the day of the week like MONDAY, TUESDAY, ... (See the example run).

Write the following functions;


 readFromFile: takes the file pointer, two-dim array (dynamically created) and the number of city (n) as
parameters, reads weekly temperatures for n cities from the file into the array.
 display: takes two-dim array and the number of city(n) as parameters, displays the weekly temperatures on the
screen.
 findColdestDay: takes two-dim array and the number of city(n) as parameters finds and returns indexes of the
coldest city and the day of the week.
Project Name: LG5_AQ1
File Name: AQ1.cpp

Example Run#1: weather.txt

How many city will be displayed in the report? 25 3 13 6 20 4 18 5


How many city will be displayed in the report? 8 1 4 1 8 4 3 5
13 14 6 5 3 7 8
City M T W R F S S 19 8 9 14 8 3 -3
---- --- --- --- --- --- --- --- 8 2 4 6 -7 -1 0
1 3 13 6 20 4 18 5 9 10 5 -2 0 2 4
2 1 4 1 8 4 3 5 7 6 10 0 -1 4 -2
3 13 14 6 5 3 7 8 1 -5 -4 -3 -2 1 -4
4 19 8 9 14 8 3 -3 19 15 10 9 4 2 0
5 8 2 4 6 -7 -1 0 20 19 16 12 11 14 16
6 9 10 5 -2 0 2 4 13 10 14 13 4 -3 -4
7 7 6 10 0 -1 4 -2 6 -8 3 5 6 7 6
8 1 -5 -4 -3 -2 1 -4 19 9 8 10 1 2 8
8 9 10 12 15 17 16
City 5 is the coldest one with the temperature -7 on FRIDAY 17 18 16 8 10 11 12

Example Run#2:

How many city will be displayed in the report? -1

How many city will be displayed in the report? 18


How many city will be displayed in the report? 13

City M T W R F S S
---- --- --- --- --- --- --- ---
1 3 13 6 20 4 18 5
2 1 4 1 8 4 3 5
3 13 14 6 5 3 7 8
4 19 8 9 14 8 3 -3
5 8 2 4 6 -7 -1 0
6 9 10 5 -2 0 2 4
7 7 6 10 0 -1 4 -2
8 1 -5 -4 -3 -2 1 -4
9 19 15 10 9 4 2 0
10 20 19 16 12 11 14 16
11 13 10 14 13 4 -3 -4
12 6 -8 3 5 6 7 6
13 19 9 8 10 1 2 8

City 12 is the coldest one with the temperature -8 on TUESDAY


AQ2.

Create a nested structure applicantsOfII and grades as follows:


typedef struct{
int englishProficiency, jury, graduateExam;
} grades_t;

typedef struct{
int id;
grades_t gr;
double overall;
}applicantsOfII_t;

Write the following functions:


 readFile : takes a set of application information from a text file named applicants.txt until the end of the file is
reached, also returns the size of the structure array (Do not forget to initialize the overall grade to 0 for each student).
 calculate : calculates the overall applicants’ grades’ average and the overall grade of each applicant with the loads
of English proficiency being 30%, jury being 50%, and the graduate exam being 20%)
 display : displays the content of the structure array of applicantsOfII_t type.
 findPassFail : finds and displays the number of the applicants who fail and pass the elimination as well as
displaying the average of all applicants’ grades’. (An applicant passes if overall >= average, otherwise student fails).

Write a C program that reads the entirety of applicants’ information from applicants.txt file into an array of structures, and
displays all the information on the screen as necessary, as shown in the example run below.
Project Name: LG5_AQ2
File Name: AQ2.cpp

applicants.txt

1111 49 65 94
2222 79 58 45
3333 76 88 95

Example Run :
Applicant ID: 1111

Scores:
Applicant English Proficiency: 49
Applicant Jury: 65
Applicant Graduate Examination: 94
Applicant Overall: 66.0

Applicant ID: 2222

Scores:
Applicant English Proficiency: 79
Applicant Jury: 58
Applicant Graduate Examination: 45
Applicant Overall: 61.7

Applicant ID: 3333

Scores:
Applicant English Proficiency: 76
Applicant Jury: 88
Applicant Graduate Examination: 95
Applicant Overall: 85.8

Average is 71.2
Number of the applicants who pass is 1
Number of the applicants who fail is 2

You might also like