Question

Exercise 1: Making a linked list of points In this exercise, you will make a struct that contains x andycoordinates: 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 00 For example, for input: 1 2 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 structs 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 structs 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: 10 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. Sample input (2): Output (2) 61 5 6 Sample input (1): 32 4 4 1 2 1 3 10 0 1 10 1 3 0 22 00 -1 2 -20 Output (1): 0-4 16 3-1 10 00 10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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\n", 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("\n\n");
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.

Add a comment
Know the answer?
Add Answer to:
In this exercise, you will make a struct that contains x and y coordinates: struct point...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a complete program that: 1) declares two local integers x and y 2) defines a...

    Write a complete program that: 1) declares two local integers x and y 2) defines a global structure containing two pointers (xptr,yptr) and an integer (z) 3) Declares a variable (mst) by the type of previous structure 4) requests the values of x and y from the user using only one scanf statement 5) sets the first pointer in the struct to point to x 6) sets the second pointer in the struct to point to y 7) uses the...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Please need help, programming in C - Part A You will need to create a struct...

    Please need help, programming in C - Part A You will need to create a struct called Team that contains a string buffer for the team name. After you've defined 8 teams, you will place pointers to all 8 into an array called leaguel], defined the following way: Team leaguel8]. This must be an aray of pointers to teams, not an array of Teams Write a function called game) that takes pointers to two teams, then randomly and numerically determines...

  • 3. Polar Coordinates. (a) Given a rectangular coordinate point (x, y), how do you compute the...

    3. Polar Coordinates. (a) Given a rectangular coordinate point (x, y), how do you compute the equivalent polar coordinates: (r, 0)? (b) Given a polar coordinate (r, o), how do you compute the equivalent rectangular coordinate: (x, y)? (c) Consider the drawing in Figure 1. Compute the coordinate of each small circle. (d) What if the circle is centered at the point (cx, cy) (and not the origin). How does the formula change?

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • Project 4 simulation with conway's rules for life - revisited due before 4/30 11:59pm, autograded by...

    Project 4 simulation with conway's rules for life - revisited due before 4/30 11:59pm, autograded by project 4 zylab, limited to 10 submissions. We will be using the concepts from project 2 to test the more recent concepts of linked lists, pointers, and file I/O. The initial data points will be stored in a file (specification of the layout of the file below), each grid of values will be dynamically allocated and then referenced via a pointer in a linked...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT