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

In C, For this program you will be printing of a grid of 10 points (x, y). You w

ID: 3592124 • Letter: I

Question

In C,

For this program you will be printing of a grid of 10 points (x, y).

You will be reading the points from a file, and printing the points
and grid to a file.

Program template link - https://pastebin.com/FrW6Mpnx

Both files will be specified in the command-line arguments.

** Your output in the file must match mine EXACTLY. **

The grid will be 20x20, which means all points will fall within
the range 0 <= x <= 19.


You will also be tasked to find the 2 points that are closest to
each other. These 2 points will be marked by a different character
in your grid.

You should use the following functions:

   fill()
   getdist()
   closest()
   grid()
   printpoints()
   printgridxy()


You will read the points from the file into an array of structures.
One structure should contain an x-value and a y-value.

Follow all instructions in the program.


Example input file: (will contain exactly 10 points)
7 19
11 5
15 11
4 10
1 8
10 4
2 5
14 12
10 9
12 4

Example running:
----------------------------------------------------------------------
$ ./a.out points.txt output.txt


Example output file:
0: ( 7, 19)
1: (11, 5)
2: (15, 11)
3: ( 4, 10)
4: ( 1, 8)
5: (10, 4)
6: ( 2, 5)
7: (14, 12)
8: (10, 9)
9: (12, 4)

Explanation / Answer

#include <cassert>        // For assert

#include "LinkedStack.h" // Header file

LinkedStack::LinkedStack() : topPtr(nullptr)

{

} // end default constructor

LinkedStack::LinkedStack(const LinkedStack& aStack)

{

   // Point to nodes in original chain

   Node* origChainPtr = aStack.topPtr;

  

   if (origChainPtr == nullptr)

      topPtr = nullptr; // Original stack is empty

   else

   {

      // Copy first node

      topPtr = new Node();

      topPtr->setItem(origChainPtr->getItem());

     

      // Point to first node in new chain

      Node* newChainPtr = topPtr;

     

      // Advance original-chain pointer

      origChainPtr = origChainPtr->getNext();

     

      // Copy remaining nodes

      while (origChainPtr != nullptr)

      {

         // Get next item from original chain

         ItemType nextItem = origChainPtr->getItem();

        

         // Create a new node containing the next item

         Node* newNodePtr = new Node(nextItem);

        

         // Link new node to end of new chain

         newChainPtr->setNext(newNodePtr);

        

         // Advance pointer to new last node

         newChainPtr = newChainPtr->getNext();

        

         // Advance original-chain pointer

         origChainPtr = origChainPtr->getNext();

      } // end while

     

      newChainPtr->setNext(nullptr);           // Flag end of chain

   } // end if

} // end copy constructor

LinkedStack::~LinkedStack()

{

                // Pop until stack is empty

                while (!isEmpty())

                                pop();

} // end destructor

bool LinkedStack::push(const ItemType& newItem)

{

                Node* newNodePtr = new Node(newItem, topPtr);                                                        

                topPtr = newNodePtr;

    newNodePtr = nullptr;

     

                return true;

} // end push

bool LinkedStack::pop()

{

                bool result = false;

                if (!isEmpty())

                {

      // Stack is not empty; delete top

                                Node* nodeToDeletePtr = topPtr;

                                topPtr = topPtr->getNext();

                               

                                // Return deleted node to system

      nodeToDeletePtr->setNext(nullptr);

                                delete nodeToDeletePtr;

      nodeToDeletePtr = nullptr;

           

      result = true;

                } // end if

  

                return result;    

} // end pop

ItemType LinkedStack::peek() const

{

                assert(!isEmpty()); // Enforce precondition during debugging

  

                // Stack is not empty; return top

                return topPtr->getItem();

} // end getTop

bool LinkedStack::isEmpty() const

{

   return topPtr == nullptr;

} // end isEmpty

................................................................

LinkedStack.h Codes:

................................................................

#ifndef LINKED_STACK_

#define LINKED_STACK_

#include "Node.h"

class LinkedStack

{

private:

                Node* topPtr; // Pointer to first node in the chain;

                  // this node contains the stack's top

public:

                LinkedStack();                   

                LinkedStack(const LinkedStack& aStack); // Copy constructor

                ~LinkedStack();

                bool isEmpty() const;

                bool push(const ItemType& newEntry);

                bool pop();

                ItemType peek() const;

};

#endif

.......................................................

LinkedStackTest.cpp Codes:

.......................................................

#include "LinkedStack.h"

#include <iostream>

using namespace std;

int main()

{

    LinkedStack s;

   

    cout << "[1] " << s.isEmpty() << endl;

   

    s.push(1);

    s.push(2);

    s.push(3);

    s.push(4);

   

    cout << "[4] " << s.peek() << endl;

    cout << "[0] " << s.isEmpty() << endl;

   

    LinkedStack s2(s);

   

    cout << "[4] " << s2.peek() << endl;

    cout << "[0] " << s2.isEmpty() << endl;

   

    s.pop();

   

    cout << "[3] " << s.peek() << endl;

   

    s.push(5);

   

    cout << "[5] " << s.peek() << endl;

   

    s.pop();

    s.pop();

    s.pop();

    s.pop();

   

     cout << "[1] " << s.isEmpty() << endl;

    

     //s.peek();

}

............................................................

Node.cpp Codes:

............................................................

#include "Node.h"

Node::Node() : next(nullptr)

{

} // end default constructor

Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

} // end constructor

Node::Node(const ItemType& anItem, Node* nextNodePtr) :

                item(anItem), next(nextNodePtr)

{

} // end constructor

void Node::setItem(const ItemType& anItem)

{

   item = anItem;

} // end setItem

void Node::setNext(Node* nextNodePtr)

{

   next = nextNodePtr;

} // end setNext

ItemType Node::getItem() const

{

   return item;

} // end getItem

Node* Node::getNext() const

{

   return next;

} // end getNext

........................................................

Node.h Codes:

.........................................................

#ifndef NODE_

#define NODE_

#include <string>

using namespace std;

typedef int ItemType;

class Node

{

private:

   ItemType item; // A data item

   Node*     next; // Pointer to next node

  

public:

   Node();

   Node(const ItemType& anItem);

   Node(const ItemType& anItem, Node* nextNodePtr);

   void setItem(const ItemType& anItem);

   void setNext(Node* nextNodePtr);

   ItemType getItem() const ;

   Node* getNext() const ;

}; // end Node

#endif