You are on page 1of 3

Department of Information Systems and Technologies

CTIS 152 – Data Structures and Algorithms


Spring 2020 - 2021
Lab Guide #4 – Week 3 – 2

OBJECTIVE : Structures and Pointer Operations

Instructor : Serpil TIN


Assistants : Ruşen ASAN, Hatice Zehra YILMAZ

The %s operator is one used for reading strings of characters in to character arrays using the scanf function.

1. Create the structure car_t with the following fields.


typedef struct{
char brand[30];
int modelYear;
double motorPower;
double km;
double price;
} car_t;

a) Initialize the data with the following values (Brand: Mercedes, ModelYear: 2017, MotorPower: 2.0, Km: 134.00, Price: 384.50),
and then display as shown in the example run.
Example Run:
The Car information is:
Brand : Mercedes
Model Year : 2017
Motor Power : 2.0
Kilometer : 134.00
Price : 384.50 TL
Project Name: LG4_Q1a
File Name: Q1a.cpp

b) Get the structure data from the user, and then display as shown in the example run.
Example Run:
Enter the information of the car:
Brand : Audi
Model Year : 2016
Motor Power : 1.8
Kilometer : 105
Price : 495

The Car information is:


Brand : Audi
Model Year : 2016
Motor Power : 1.80
Kilometer : 105.00
Price : 495.00 TL
Project Name: LG4_Q1b
File Name: Q1b.cpp

c) Use Pointer notation for part a.


Example Run:
The Car information is:
Brand : Mercedes
Model Year : 2017
Motor Power : 2.0
Kilometer : 134.00
Price : 384.50 TL
Project Name: LG4_Q1c
File Name: Q1c.cpp
d) Rewrite the program so that it will read the information of the car from the file “cars.txt” file, store them into an array
of structure, and display the number of cars and the information of them.
( Use dynamic memory allocation for the array. The first line of the file consists the number of cars. )
Example Run: cars.txt
There are 3 cars. 3
BMW 2003 2.0 195.00 148.50
The Car Information: Ford 2014 1.8 78.90 112.50
********************* Peugeot 2007 1.6 180.00 96.50
Brand : BMW
Model Year : 2003
Motor Power : 2.0
Kilometer : 195.00
Price : 148.50 TL
***********************
Brand : Ford
Model Year : 2014
Motor Power : 1.8
Kilometer : 78.90
Price : 112.50 TL
***********************
Brand : Peugeot
Model Year : 2007
Motor Power : 1.6
Kilometer : 180.00
Price : 96.50 TL
***********************
Project Name: LG4_Q1d
File Name: Q1d.cpp

2. Amazon offers special discount for some products. Discounted products’ information (product name, price, discount rate)
is kept in the “amazon.txt” file.

Write a C program that will read all the products’s information from the file into a dynamically created structure array,
display the product information including the discounted price and the saved amount. The program will also display the
number of products.

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

Example Run: amazon.txt

There are 6 products in the market 6


e-reader 119.99 17
PRODUCT NAME PRICE DISCOUNT DISC. PRICE SAVED AMOUNT WifiCamera 39.99 33
************ ********* ******** *********** ************ SolarLight 16.99 67
e-reader 119.99 $ %17 99.59 $ 20.40 $ LedBulb 25.99 62
WifiCamera 39.99 $ %33 26.79 $ 13.20 $ CarOrginazer 39.99 74
SolarLight 16.99 $ %67 5.61 $ 11.38 $ FlashLight 19.99 40
LedBulb 25.99 $ %62 9.88 $ 16.11 $
CarOrginazer 39.99 $ %74 10.40 $ 29.59 $
FlashLight 19.99 $ %40 11.99 $ 8.00 $

Project Name: LG4_Q2


File Name: Q2.cpp
Additional Questions
AQ1. Write a C program that reads the information about several students from the text file named as “gpa.txt” into a
structure array. (Use dynamic memory allocation for the array. The first line of the file consists the number of
students.)
StuID Gpa1 Gpa2 Your output in scholarship.txt file should be like
gpa.txt: StuID Gpa1 Gpa2 Yearly GPA Scholarship
5 ***** **** **** ********** ***********
1111 3.50 3.70 1111 3.50 3.70 3.60 100
1112 3.10 3.50 1112 3.10 3.50 3.30 50
1118 2.98 3.20 1118 2.98 3.20 3.09 50
1205 2.40 2.75 1205 2.40 2.75 2.58 0
1315 1.98 2.80 1315 1.98 2.80 2.39 0

Gpa 1 : Fall semester GPA of the student,


Gpa 2 : Spring semester GPA of the student.
The program should calculate the yearly average gpa of each student . The formula is given below:
yearlyGpa = (Gpa1 + Gpa2) / 2
University will give scholarship to successful students. The rules to have a scholarship are as follows:
 If a student’s yearly GPA is above 3.00 (included), the student will be able to take %50 scholarship
 If a student’s yearly GPA is above 3.50 (included), the student will be able to take %100 scholarship
 Write the student ID, Gpa1, Gpa2, yearly GPA, and the scholarship percent for each student to the text file with the
name scholarship.txt.
Project Name: LG4_AQ1
File Name: AQ1.cpp

AQ2. A media company keeps the track of radios which serve from the internet. The company needs a program that stores the
information of the radios: Frequency, name of the radio, number of hits of the most popular program for each. Write the
following functions;
 read gets the radio information from a file into an array of structures.
 display displays all radio information as in the example run.
 findMaxHits returns the index of the radio which has the maximum number of hits.
 findRadio finds the radio information with the given frequency and returns the index of this radio.
Write a main program that takes the number of inputs (n) from the user and read the information for n radios from the
file “radio.txt” into the array of structure which you created dynamically. Maximum size is 20 for inputs. Do NOT forget
to make data validation. If you don’t use dynamic memory allocation, you will lose points.
The program will also do the followings;
 Display the list of all radios,
 Find and display the radio which has the maximum hits.
 Ask for a frequency and display the radio information which has this frequency.
Please examine the given example runs, and test your programs.
Hints:
 Use %s operator while reading and displaying the radio name.
 Include stdlib.h for malloc()
Example Run#1
How many radio information will you read: 30 Sorry! There is no radio on the given frequency
Error!!! Please enter a number 1-20 :(

How many radio information will you read: 10 Example Run#2


Frequency Name #ofHits How many radio information will you read: 3
********* ************ ******* Frequency Name #ofHits
95.3 Slowturk 85909 ********* ************ *******
89.8 Showradyo 51613 95.3 Slowturk 85909
92.0 Kralfm 41091 89.8 Showradyo 51613
89.2 Alemfm 98210 92.0 Kralfm 41091
90.8 Superfm 36975
90.0 Radyoviva 31097 The radio <Slowturk> has the maximum hits
106.9 Powerturk 27187 Frequency: 95.3
104.0 Radyod 22374 Hits : 85909
100.6 Joyfm 19829
94.1 Polisradyosu 17654 Enter a radio frequency to search: 89.8

The radio <Alemfm> has the maximum hits Radio <Showradyo> is on the 89.8 frequency
Frequency: 89.2
Hits : 98210 Project Name: LG4_AQ2
Enter a radio frequency to search: 100.0 File Name: AQ2.cpp

You might also like