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<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
#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....
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the...
#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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT