The C programming Language. Please add comments to the codes when you answer. Ci
ID: 3589998 • Letter: T
Question
The C programming Language.
Please add comments to the codes when you answer.
Circles as struct.
In this question we will represent circles with a struct . A circle has a certain position in a coordinate system and a radius. The position is the center of the circle. struct circle { int x, y ; int radius; }; typedef struct circle Circle ;
a) Write a function printOut (...) that takes a Circle as in - parameter and print it out to standard output. Note that this function should not load anything from the standard input. Call your function with circle from main. Create the circle as shown here:
Example: A circle with position (3,2) and radius 5 print out : Circle at (3,2) with radius 5
b) Write a function named move(...) that takes a pointer to a circle and two integers (dx and dy) as in - parameters and then move the circle (i.e.,the circle the pointer points to) dx units in x - axis and dy units in y - ranks. Please note that this is an auxiliary function: it will not load anything from standard input or print something to standard output. Call your function with circle from question a ) and move it one step in x - axis and two steps in y axes before you print it.
Explanation / Answer
Given below is the code and output. Please don't forget to rate the answer if it helped. Thank you
#include <stdio.h>
struct circle
{
int x, y ;
int radius;
};
typedef struct circle Circle ;
void printOut(Circle c);
void move(Circle* cptr, int dx, int dy);
int main()
{
Circle c = {3, 2, 5}; //initializes the struct with the member values x = 3, y = 2, radius = 5
/*Alternate way is
Circle c;
c.x = 3;
c.y = 2;
c.radius = 5;
*/
printOut(c);
//moving the circle 1 step in x axis nad 2 steps in y axis
move(&c, 1, 2);
printf("After moving ... ");
printOut(c);
}
void printOut(Circle c)
{
printf("Circle at (%d, %d) with radius %d ", c.x, c.y, c.radius);
}
void move(Circle* cptr, int dx, int dy)
{
cptr->x = cptr->x + dx;
cptr->y = cptr->y + dy;
}
output
$ gcc Circle.c
$ ./a.out
Circle at (3, 2) with radius 5
After moving ...
Circle at (4, 4) with radius 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.