Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, you will make a struct that contains x and y coordinates: stru

ID: 3812455 • Letter: I

Question

In this exercise, you will make a struct that contains x and y coordinates: struct point {int x; int y;}; The object of this exercise is to make a singly linked list of these point structures. Add one more member called "next" to your point struct. It is a pointer to the next member of the linked list. The next member of the last point struct should point to NULL, which indicates the end of the list. Your program will read in lines of integers, two integers on each line, representing the x and y coordinates for a point. Your program should keep accepting input until you receive a line of "0 0". For example, for input: You should make three structs, the first with x coordinate 1 and y coordinate 2, the second with x coordinate 0 and y coordinate 1, and the third with x coordinate 1 and y coordinate 3. The first struct's "next" field/member, which is a pointer, will point to the second struct. The second structs "next" field/member will point to the third struct. The third strucfs "next" field/member will point to NULL. After your get your structs stored in memory, you will output the square of the distance of each point to the origin (0, 0), with each result in a new line. For the above input, the expected output should be: Notice that you are NOT allowed to store the structs in an array. They must be in a singly linked list, and the structures must be allocated dynamically. This lab is intended to expose you to the use of pointers, memory allocation, and structures. Pointers and structs are essential in many of the following C.S. classes (e.g. Operating Systems), so please make sure you understand the reading and complete this lab with a thorough understanding.

Explanation / Answer

please refer below C++ code

#include<stdlib.h>
#include<stdio.h>

typedef struct point
{
int x;
int y;
struct point *next;
}point;

//adding node to linked list
void add_node(point **head, point *node)
{

if(*head == NULL)
*head = node;

else
{
point *temp = *head;

while(temp->next != NULL)
temp = temp->next;

temp->next = node;
}

}
//squaring distance from origin
void square_distance(point *head)
{
point *temp = head;
int dist;
while(temp != NULL)
{
dist = (temp->x * temp->x) + (temp->y * temp->y); //distance from origin
printf("%d ", dist);
temp = temp->next;
}
}
int main()
{
int x,y;
point *head = NULL;
point *temp;

while(1) //infinite loop to scan infinite lines
{
scanf("%d %d", &x,&y);

if(x==0 && y ==0)
break;

temp = new point; //creating node
temp->x = x; //setting x field
temp->y = y; //setting y field
temp->next = NULL;

add_node(&head,temp);
}
printf(" ");
square_distance(head);
return 0;
}

please refer below output

5 6
4 4
1 3
1 0
0 2
-1 2
-2 0
0 -4
3 -1
0 0


61
32
10
1
4
5
4
16
10

Process returned 0 (0x0) execution time : 44.080 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote