Question

(20 pts) (9) Fill in the program code below for the square ADT (abstract data type)...

(20 pts) (9) Fill in the program code below for the square ADT (abstract data type)
//------ POINT ----------------------------------------------------------------
typedef struct point point;
struct point { double x, y; };
//------ SQUARE -----------------------------------------------------------------
typedef struct square square;
struct square { point ul; size_t side; };
square* square_init(double ulx, double uly, double side); // TODO
void square_delete(square* sq); // TODO
void square_move(square* sq, double x, double y); // TODO
void square_expandby(square* sq, double expandby); // TODO
double square_area(square* sq); // TODO

double square_perimeter(square* r); // TODO
// print location, side, area and perimeter
void square_print(const char* msg, square* sq); // TODO
void test_square(double ulx, double uly, double side) {
square* sq = square_init(ulx, uly, side);
square_print(“sq is: “, sq);
square_move(2, 2);
square_print(“sq is now: “, sq);
square_expandby(sq, 10);
square_print(“sq has expanded to: “, sq);
square_delete(sq);
printf(“\n\n”);
}
void tests_square() {
test_square(0, 0, 10);
test_square(1, 1, 5);
// add other tests if you wish // TODO (optional)
}
int main(int argc, const char* argv[]) {
tests_square();
return 0;
}

IN C LANGUAGE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the code in C

Let me know for any help with any other quesitons.

=====================================================================

#include<stdio.h>

#include <stdlib.h>

//------ POINT ----------------------------------------------------------------

typedef struct point point;

struct point { double x, y; };

//------ SQUARE -----------------------------------------------------------------

typedef struct square square;

struct square { point ul; size_t side; };

square* square_init(double ulx, double uly, double side) // TODO

{

                square* p = (square*)malloc(sizeof(square))        ;

                p->ul.x = ulx;

                p->ul.y = uly;

                p->side = (size_t)side;

                return p;

}

void square_delete(square* sq) // TODO

{              free(sq);

}

void square_move(square* sq, double x, double y) // TODO

{             

                sq->ul.x = x;

                sq->ul.y = y;

}

void square_expandby(square* sq, double expandby) // TODO

{             

                sq->side = (sq->side)*expandby;

}

double square_area(square* sq) // TODO

{             

                return (sq->side)*(sq->side);

}

double square_perimeter(square* r) // TODO

{             

return 4*(r->side);

}

square_print(char* msg, square* sq){

                printf(msg);

                printf("Point: %.2lf by %.2lf, Area: %.2lf, Perimeter: %.2lf \n",sq->ul.x,sq->ul.y,square_area(sq),square_perimeter(sq));

}

// print location, side, area and perimeter

void test_square(double ulx, double uly, double side) {

square* sq = square_init(ulx, uly, side);

square_print("sq is: ", sq);

square_move(sq, 2, 2);

square_print("sq is now: ", sq);

square_expandby(sq, 10);

square_print("sq has expanded to: ", sq);

square_delete(sq);

printf("\n\n");

}

void tests_square() {

test_square(0, 0, 10);

test_square(1, 1, 5);

// add other tests if you wish // TODO (optional)

}

int main(int argc, const char* argv[]) {

tests_square();

return 0;

}

=====================================================================

Add a comment
Know the answer?
Add Answer to:
(20 pts) (9) Fill in the program code below for the square ADT (abstract data type)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • QUESTION 6 What is the output of following C code? struct numbers     {         int x...

    QUESTION 6 What is the output of following C code? struct numbers     {         int x = 2;         int y = 3;     } int main() {   struct numbers nums;         nums.x = 110;         nums.y = 100;         printf("%d\n%d", nums.x, nums.y);         return 0; } A. Compile-time Error B. 110 100 C. 2 3 D. Run-time Error 2 points    QUESTION 7 What is the output of following C code? typedef struct student {         char *stud; }s1; int main() {   s1 s;         s.stud...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

  • Run the C program below and complete the table showing all the variables. Add printf function...

    Run the C program below and complete the table showing all the variables. Add printf function calls to obtain the addresses and values of all 13 variables. (Count the array (ca) as 3 variable/values and also include argc and argv in the table). The table must show the Address, Name, Datatype, Scope, and Value of each variable on the stack. (Hint: use the sizeof function to double check your addresses.) Explain how different the actual memory allocations are from what...

  • A limited-sized Queue ADT is an abstract data type that has a limit on the length...

    A limited-sized Queue ADT is an abstract data type that has a limit on the length of the queue. It can be created with a static array of size N, where N is the maximum length of the array. In C, this structure can be defined as follows: typedef struct {int * data;//array of the data on the queue//you may add other attributes here but use as few as possible} queue_t; Write an (efficient) pseudocode for the implementation of each...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • 3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the...

    3. (8 pts) You need to understand how to define and use a struct for this exercise. You should be able to figure out the answer without actually compiling or running the program. Which is true about the following codes? If you choose a, you need to specify where the syntax error(s) occur; if you choose b, you need to specify what error occurs when you run it; if you choose c, you need to specify the outputs of the...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT