Question

In: Computer Science

#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...

#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!

Solutions

Expert Solution

// 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


Related Solutions

#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
The task is to sort structs in array. The struct is struct coor{ int x; int...
The task is to sort structs in array. The struct is struct coor{ int x; int y; int z; }; Create an array with 100 coordinates, which are randomly initiated with x between 0 and 30 and y between 5 and 60, and z between 10 and 90. Then modify the Quick Sort algorithm (QuickSort.cpp) to sort coordinated. Comparison of the coordinates should be carried on first x, then y, and z last. It means that if two points has...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT