Starting from:
$30

$24

Homework 6 Solution

Files to submit: perimeter.c

Time it took Matthew to Complete: 20 mins




All programs must compile without warnings when using the -Wall and -Werror options
Submit only the files requested

Do NOT submit folders or compressed files such as .zip, .rar, .tar, .targz, etc
Your program must match the output exactly to receive credit.

Make sure that all prompts and output match mine exactly.
Easiest way to do this is to copy and paste them
All input will be valid unless stated otherwise
Print all real numbers to two decimal places unless otherwise stated
The examples provided in the prompts do not represent all possible input you can receive.
All inputs in the examples in the prompt are underlined

You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program
If you have questions please post them on Piazza
 
Write a program called perimeter.c that calculates the perimeter of a polygon.



The points of the polygon will be stored in a file and this file will be passed on the command line arguments
The file itself will be a binary file containing integers



The first integer in the file is the number of points contained in the file
The remaining integers are the points, with the first integer being the x coordinate and the second integer being the y coordinate.
There is an edge between each adjacent point and between the first point and the last point
Each file contains at least 3 points
The perimeter of a polygon is the sum of the lengths of all of its edges
Use a double to store your perimeter and report the perimeter to the nearest 2 decimal points.
As an aside the example tests do not form actual polygons but assume that they do.



Example. Assume that there is a file called example.txt. It will store the following information but in binary form. This example is just to give you a visualization of the data.

3

422
-981
647
./perimeter example.txt The perimeter is 3648.30




Files to submit: read_lines.c, read_lines.h

Time it took Matthew to Complete: 15 mins




All programs must compile without warnings when using the -Wall and -Werror options
Submit only the files requested

Do NOT submit folders or compressed files such as .zip, .rar, .tar, .targz, etc
Your program must match the output exactly to receive credit.

Make sure that all prompts and output match mine exactly.
Easiest way to do this is to copy and paste them
All input will be valid unless stated otherwise
Print all real numbers to two decimal places unless otherwise stated
The examples provided in the prompts do not represent all possible input you can receive.
All inputs in the examples in the prompt are underlined

You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program
If you have questions please post them on Piazza



Restrictions




No global variables are allowed
Your main function may only declare variables, call other functions, and assign variables values.
For this assignment you will be writing only a single function: read_lines.




Specifications




This function has the following definition:

void read_lines(FILE* fp, char*** lines, int* num_lines).



This function should read all of the lines contained within fp and

Set each row of lines to contain one line of the file.
Set num_lines to be equal to the number of lines that were in the file
If the file is empty lines should be set to NULL and num_lines to 0.



You only need to submit read_lines.c and read_lines.h
read_lines.h must contain at least the definition for read_lines but it is perfectly ok if there are more



Example




1. ./read_lines.out Makefile

read_lines.out: read_lines.o main.o
gcc -g -Wall -Werror -o read_lines.out read_lines.o main.o
 
main.o: main.c read_lines.h
gcc -g -Wall -Werror -c -o main.o main.c
 
read_lines.o: read_lines.c read_lines.c
gcc -g -Wall -Werror -c -o read_lines.o read_lines.c
 
clean:
rm -f *.out *.o



Files to submit: tail.c

Time it took Matthew to Complete: 20 mins




All programs must compile without warnings when using the -Wall and -Werror options
Submit only the files requested

Do NOT submit folders or compressed files such as .zip, .rar, .tar, .targz, etc
Your program must match the output exactly to receive credit.

Make sure that all prompts and output match mine exactly.
Easiest way to do this is to copy and paste them
All input will be valid unless stated otherwise
Print all real numbers to two decimal places unless otherwise stated
The examples provided in the prompts do not represent all possible input you can receive.
All inputs in the examples in the prompt are underlined

You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program
If you have questions please post them on Piazza



Restrictions




No global variables are allowed
Your main function may only declare variables, call other functions, and assign variables values.
Write a program called tail.c that prints out the last N lines of a given file




Arguments to your program will be passed on the command line in the following order

Name of the file
N
N will always be at least 1
If N is greater than the number of lines in the file, all of the lines in the file should be displayed
You may assume that no line is longer than 100 characters

For an additional challenge try to solve the problem where there is no limit on the length of a line

My solution to dealing with any length lines involved using the functions ftell and fseek.
There is no limit to the number of lines in a file
Some lines may only contain the newline character, these still count as a line
My editor automatically added 1 newline character to the end of most of the test files but does not display them. Your editor may or may not display them but do be aware that they are there.



Example. Assume the file meme.txt contains the following

It

is

over 9000!!!




./tail.out meme.txt 1 over 9000!!!



./tail.out meme.txt 2 is



over 9000!!!

./tail.out meme.txt 10 It



is

over 9000!!!

More products