Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please answer it with C (Only C). Here is Testing code, Please try with this testing code. #include #include #include a1_question3.h void checkAndPrint( float expected,

image text in transcribedimage text in transcribedPlease answer it with C (Only C).

Here is Testing code, Please try with this testing code.

#include

#include

#include "a1_question3.h"

void checkAndPrint(float expected, float result) {

if (fabs(expected-result)

printf("%f is essentially the same as %f, PASS ", expected, result);

} else {

printf("%f is essentially not the same as %f, FAIL ", expected, result);

}

}

int main() {

int matrix1[4][2] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};

checkAndPrint(2.0, calculateAreaShoelace(4, matrix1, 1)); //should return 2.0 (area of a triangle with the second point removed)

checkAndPrint(4.0, calculateAreaShoelace(4, matrix1, 4)); //should return 4.0 (area of a square as 4 is an invalid index)

checkAndPrint(2.0, calculateAreaShoelace(4, matrix1, 0)); //should return 2.0 (area of a different triangle with the first point removed)

return 0;

}

In mathematics there are formulas for calculating the area of a polygon formed by any number of points. One popular formula is called "the Shoelace Formula": S2=S1=y1x2+y2x3+y3x4+y4x1x1y2+x2y3+x3y4+x4y1Area=0.5S1S2 Write a function that takes in the number of rows (number of columns is fixed at 2) of a 2D array of ints, the array itself, an integer as the index, and calculates the area of the polygon formed by the points represented in the 2D array with the row indicated by the index removed. Use this function header: float calculateAreaShoelace(unsigned int rows, int points[][2], int index) Given the 422D array matrix1: {{1,1},{1,1},{1,1},{1,1}}, calculateAreaShoelace (4, matrix1, 1) returns 2.0 (area of a triangle with the second point removed) calculateAreaShoelace (4, matrix1, 4) returns 4.0 (area of a square as 4 is an invalid index) calculateAreaShoelace (4, matrix 1,0 ) returns 2.0 (area of a different triangle with the first point removed) Note that the area can be zero if the points are all in a straight line or there are not enough points to form a polygon. You can assume that the row argument is correct for the array, and the points in the 2D array are arranged in a clockwise or anticlockwise manner (required for the Shoelace Formula to work). You can also assume that the number of points will not be more than 128 . The index can be invalid (negative or larger than row-1) and if that is the case it means no row is being removed. Do not use dynamic arrays and the keyword continue in your answer (if you use any of those you get 0 for this question). Your answer must first calculate S1, then S2, then the area (failure to do so will also get 0). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question3.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions