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
  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • Using Python Create a simple linked list by defining a struct (in C/C++) or a class...

    Using Python Create a simple linked list by defining a struct (in C/C++) or a class (in Python3). For example, in C/C++: struct singly_linked_list { int data; singly_linked_list *next; }; (Make sure you understand the concept of pointers - or lack thereof - in Python.) Write a function to determine if there is a cycle in the singly linked list. Return 1 if there is a cycle in the list, 0 if no cycle. This is a very common interview...

  • 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...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

  • 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...

  • How can you change this function from the main program “struct point” to a “class point”...

    How can you change this function from the main program “struct point” to a “class point” into new .h and .cpp projects. Make all the member variables private. Add getter and setter methods for each member variable. ///////////////////////////////////////////////// struct point { int x,y;}; int main() { point plat[20]; for (int i=0;i<10;i++) {   plat[i].x=rand()%400; plat[i].y=rand()%533; } /////////////////////////////////////////////////////////////

  • 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?

  • 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...

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