Week 11 Class activities

advertisement
http://www.comp.nus.edu.sg/~cs1010/
Week 11
Class Activities
© NUS
CS1010 (AY2015/6 Semester 2)
Week 11: Structures
 Unit #19: Sections 1 - 4 (40 minutes)
 Exercise #1: Perimeter (10 min.)
 Unit #19: Sections 5 - 6 (20 minutes)
 Exercise #2: Points (25 minutes)
 Unit #19: Sections 7 - 9 (20 minutes)
 Exercise #3: Health Screen (25 minutes)
Week11 - 2
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 3
Week 11 Programs
 Download the programs from this web page

http://www.comp.nus.edu.sg/~cs1010/lect/prog/2015s2/Week11_for_students
 The files are:




Week11_Perimeter.c
Week11_Points_Complete.c
Week11_Points.c
Week11_Health_Screen.c
 You may also copy the above files directly into your sunfire
account using the following UNIX command, where xxx is
the name of one of the above files:
cp ~cs1010/lect/prog/week11_for_students/xxx .
© NUS
CS1010 (AY2015/6 Semester 2)
Unit #19: Sections 1 – 4
1. Organizing Data
2. Structure Types
3. Structure Variables
4. Assigning Structures
Week11 - 4
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 5
Exercise #1: Perimeter (1/2)
 Write a program Week11_Perimeter.c to do the
following:
1. Define a structure type rectangle_t with 2 integer members:
side1 and side2, which are the lengths of its 2 sides.
2. Declare a variable of type rectangle_t and read values into its
members.
3. Compute the minimum perimeter if we fold the rectangle into
halves once, either along the x-axis or the y-axis.
 A skeleton program is given
 Do not use any additional variables besides the two given
variables.
 You may write the code in the main() function. You may
modularise the program later.
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 6
Exercise #1: Perimeter (2/2)
#include <stdio.h>
Week11_Perimeter.c
typedef struct {
int side1, side2;
} rectangle_t;
int main(void) {
rectangle_t rect;
int perimeter;
printf("Enter lengths: ");
scanf("%d %d", &rect.side1, &rect.side2);
if (rect.side1 > rect.side2)
perimeter = rect.side1 + 2*rect.side2;
else
perimeter = rect.side2 + 2*rect.side1;
printf("Perimeter = %d\n", perimeter);
return 0;
}
© NUS
CS1010 (AY2015/6 Semester 2)
Unit #19: Sections 5 – 6
5. Passing Structures to Functions
6. Array of Structures
Week11 - 7
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 8
Exercise #2: Points (1/5)
 Write a program Week11_Points.c that includes
1. a structure type point_t whose members are the x- and ycoordinates of a point. The coordinates are integers.
2. a function read_points() to read the number of points and points’
data into an array of points, and return the number of points
read. Each point is represented by its x- and y-coordinates.
 You may assume that the input data contain at least 1 point and at
most 10 points.
 An example of input data of 5 points is as shown here.
5
34
-1 4
5 -2
-6 -2
03
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 9
Exercise #2: Points (2/5)
Week11_Points.c
#include <stdio.h>
#define MAX_POINTS 10
typedef struct {
int x, y; // x- and y-coordinates of a point
} point_t;
// Function prototypes omitted for brevity
int main(void) {
point_t points[MAX_POINTS];
int size; // number of points
size = read_points(points);
…
return 0;
}
© NUS
CS1010 (AY2015/6 Semester 2)
Exercise #2: Points (3/5)
// Read input data
// Return the number of points read
int read_points(point_t pts[]) {
int size, i;
printf("Enter number of points: ");
scanf("%d", &size);
printf("Enter data for %d points:\n", size);
for (i=0; i<size; i++)
scanf("%d %d", &points[i].x, &points[i].y);
return size;
}
Week11_Points.c
Week11 - 10
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 11
Exercise #2: Points (4/5)
 After reading the points, imagine that you draw the smallest circle
with centre at the origin (0, 0) that encloses all the given points.
Complete the function float circle_area() to return the area (of type
float) of this smallest circle.
 You may assume that π is 3.14159.
 For the example input data, the area is 125.66.
 Hint: It may be useful to add a function for computing the square of
distance of a point from the origin.
5
34
-1 4
5 -2
-6 -2
03
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 12
Exercise #2: Points (5/5)
// Compute the area of the smallest circle that
// encloses all the points.
float circle_area(point_t pts[], int size) {
Week11_Points.c
int i, max_dist, dist;
max_dist = dist_sq(points[0]);
for (i=1; i<size; i++) {
dist = dist_sq(points[i]);
if (dist > max_dist)
max_dist = dist;
}
return PI * max_dist;
}
// Square of distance of a point from the origin
int dist_sq(point_t pt) {
return (pt.x * pt.x) + (pt.y * pt.y);
}
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 13
Unit #19: Sections 7 – 9
7. Passing Address of Structures to
Functions
8. The Arrow Operator (->)
9. Returning Structure from Functions
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 14
Exercise #3: Health Screening (1/2)
 Write a program Week11_Health_Screen.c to read in a
list of health screen readings
 Each input line represents a reading consisting of 2 numbers: a
float value indicating the health score, and an int value indicating
the number of people with that score.
 You may assume that there are at most 50 readings.
 The input should end with the reading 0 0, or when 50 readings
have been read.
 As the readings are gathered from various clinics, there
might be duplicate scores in the input. You are to
determine how many unique scores there are.
 A skeleton program Week11_Health_Screen.c is given.
 This exercise is mounted on CodeCrunch.
© NUS
CS1010 (AY2015/6 Semester 2)
Week11 - 15
Exercise #3: Health Screening (2/2)
 A sample run is shown below
Enter score and frequency (end with 0 0):
5.2135 3
3.123 4
2.9 3
0.87 2
2.9 2
8.123 6
3.123 2
7.6 3
2.9 4
0.111 5
0 0
Number of unique readings = 7
 Possible extension: Which is the score that has the
highest combined frequency? (Do this on your own.)
© NUS
CS1010 (AY2015/6 Semester 2)
Things-To-Do


PE2 this Wednesday!

30 Mar 2016

See CS1010 “PE” website for details
Next week’s lecture:

Searching and Sorting
Week11 - 16
© NUS
CS1010 (AY2015/6 Semester 2)
End of File
Week11 - 17
Download