In: Computer Science
#include <stdio.h>
typedef struct Coordinates
{
int x;
int y;
} Coordinate;
typedef union uCoordinates
{
int x;
int y;
} uCoordinate;
// TODO - Populate 4 different Coordinates with any numbers
between 1 and 99 for x & y values
// using coordinate1, coordinate2, coordinate3, & coordinate4
as Coordinate names
// TODO - Print to screen the x & y values of each
coordinate
// TODO - Replace the following with your name: Ethin Svoboda
int main()
{
// ADD YOUR CODE HERE
Coordinate coordinate1 = {.x 5, .y 7};
Coordinate coordinate2 = {.x 11, .y 20};
Coordinate coordinate3[100];
coordinate3[0].x = 10;
printf(format: "Coordinate 1 - x = %d, y = %d \n", coordinate1.x,
coordinate1.y);
printf(format: "Coordinate 2 - x = %d, y = %d \n", coordinate2.x,
coordinate2.y);
return 0;
}
The code I have is in my int main but I'm kind of confused on how to do it. What I'm supposed to do it says "TODO" before. Any help would be great thanks!
// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// C Program
#include <stdio.h>
// this is the way of defining structure this structure
has two elements x and y
typedef struct Coordinates
{
int x;
int y;
} Coordinate;
// this is the way of defining union this union has two
elements x and y
typedef union uCoordinates
{
int x;
int y;
} uCoordinate;
// TODO - Populate 4 different Coordinates with any
numbers between 1 and 99 for x & y values
// using coordinate1, coordinate2, coordinate3, & coordinate4
as Coordinate names
// TODO - Print to screen the x & y values of each
coordinate
// TODO - Replace the following with your name: Ethin
Svoboda
int main()
{
// Populate 4 different Coordinates with any numbers
between 1 and 99 for x & y values
// using coordinate1, coordinate2, coordinate3, & coordinate4
as Coordinate names
// BY USING STRUCTURE
// we can create cordinates as follows
Coordinate coordinate1 = {5, 7};
Coordinate coordinate2 = {11, 20};
Coordinate coordinate3 ={9,16};
//cordinate can also be populated by declaring variable
then assigning values
Coordinate coordinate4;
coordinate4.x = 19;
coordinate4.y = 12;
// Print to screen the x & y values of each
coordinate
printf("Coordinate 1 - x = %d, y = %d \n", coordinate1.x,
coordinate1.y);
printf("Coordinate 2 - x = %d, y = %d \n", coordinate2.x,
coordinate2.y);
printf("Coordinate 3 - x = %d, y = %d \n", coordinate3.x,
coordinate3.y);
printf("Coordinate 4 - x = %d, y = %d \n", coordinate4.x,
coordinate4.y);
// NOW BY USING UNION AND CHANGING NAME OF CORDINATES TO Ethin Svoboda
// In union both x and y share the same location so if
we assign a value to x same value will be assigned to y and
vice-versa
uCoordinate EthinSvoboda1;
EthinSvoboda1.x = 4; // 4
assigned to EthinSvoboda1.y also
uCoordinate EthinSvoboda2;
EthinSvoboda2.y = 51; // 51
assigned to EthinSvoboda2.x also
uCoordinate EthinSvoboda3;
EthinSvoboda3.x = 11;
uCoordinate EthinSvoboda4; // if we not
assign any value by default it initialise with 4
EthinSvoboda4.y = 38;
printf("\n");
// Print to screen the x & y values of each
coordinate
printf("Coordinate 1 - x = %d, y = %d \n", EthinSvoboda1.x,
EthinSvoboda1.y);
printf("Coordinate 2 - x = %d, y = %d \n", EthinSvoboda2.x,
EthinSvoboda2.y);
printf("Coordinate 3 - x = %d, y = %d \n", EthinSvoboda3.x,
EthinSvoboda3.y);
printf("Coordinate 4 - x = %d, y = %d \n", EthinSvoboda4.x,
EthinSvoboda4.y);
return 0;
}
# SAMPLE OUTPUT