0% found this document useful (0 votes)
144 views

Assignment 1: Cs 2255 - Computer Science Ii

The document describes an assignment to write a C program to help a plantation manager. The program must: 1) Declare structs to represent the plantation data with tree locations, types, and other needed information. 2) Write functions to count the number of coffee and tea trees. 3) Calculate the shortest fence length needed by finding the bounding box of all trees. 4) Find the optimal location for a water pump to minimize total tube length based on distances between all trees and pump. 5) Read data from an input file and write results to an output file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Assignment 1: Cs 2255 - Computer Science Ii

The document describes an assignment to write a C program to help a plantation manager. The program must: 1) Declare structs to represent the plantation data with tree locations, types, and other needed information. 2) Write functions to count the number of coffee and tea trees. 3) Calculate the shortest fence length needed by finding the bounding box of all trees. 4) Find the optimal location for a water pump to minimize total tube length based on distances between all trees and pump. 5) Read data from an input file and write results to an output file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT 1

CS 2255 – COMPUTER SCIENCE II

Plantation
There are N trees in a plantation. Each tree is assigned an information card which notes the
following information: location (x, y), type (coffee or tea).

The plantation manager wants to construct a fence around the plantation. For minimum cost, the
fence length needs to be as short as possible.

The manager also wants to build a watering system based on tubes to let water runs from the
pump to each tree. The location of the pump needs to be considered to minimize the total length
of the used tubes.

Write a C program that can help the manager to do the above.

Guidance:
a) First, declare necessary C structs to represent the plantation.

// Declare struct //Declare more if needed. //Declare more if needed.

struct ……………… struct ……………… struct ………………


{ { {

}; }; };

b) Write C functions to count trees of each type.

// Count trees of a specific type. // Use countTrees function…


int countTrees(Plantation p, int type) // Count coffee trees.
{ int countCoffeeTrees(…….…...………………)
{

}
// Count tea trees.
int countTeaTrees(…….….…………………...)
{

} }

1
c) Write C functions to calculate the shortest fence length need to be built.

// Find upper left point.


Upper left (xmin, ymax) Point findUpperLeft(Plantation p)
{

Lower right (xmax, ymin)

}
// Use functions in right column.
// Calculate perimeter of rectangular fence. // Find lower right point.
float calcFenceLength(...……..……………) Point findLowerRight(Plantation p)
{ {

} }

d) Let the program calculate the minimum total length of used tubes.

// Find position of the pump.


// It’s the center of all trees!!!
Point findPump(Plantation p)
{

Pump (xp, yp)

// Use distance function. }


// Calculate total length of tubes. // Calculate distance between two point.
float calculateTotalLength(…………..…….) float distance(Point p, Point q)
{ {

} }

2
e) Let the program input and output through file.

Structure of file NongTrai.in


- First line: N (number of trees).
- The following N lines, line #i (0 <= i <= N - 1): Xi Yi Li
(Xi Yi: location of tree #i, Li : type of tree #i (0-coffee, 1-tea).

Structure of file NongTrai.out:


- Line #1: S0 S1 (S0: coffee tree count, S1: tea tree count).
- Line #2: Cmin (shortest fence length).
- Line #3: Dmin (minimum total length of used tubes).

You might also like