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

Easy C Problem (STACK2 is located below) Just add the three member functions to

ID: 3726675 • Letter: E

Question

Easy C Problem (STACK2 is located below)

Just add the three member functions to the already completed STACK2. Once complete, take screenshot of the compiled output.

(1) Add OutputSTACK() as a new member function to the STACK2 abstract data type.

//--------------------------------------------------

void OutputSTACK(const STACK *stack,FILE *OUT)

//--------------------------------------------------

{

   fprintf(OUT,"[%d:%d] ",GetSizeSTACK(stack),GetCapacitySTACK(stack));

   if ( IsEmptySTACK(stack) )

      printf("(empty)");

   else

   {

      int offset;

      int UB = GetSizeSTACK(stack)-1;

     

      for (offset = 0; offset <= UB; offset++)

         fprintf(OUT,"%d%c",PeekSTACK(stack,offset),((offset == UB) ? '.' : ' '));

   }

}

(2) Add CopySTACK() as a new member function to STACK2.

//--------------------------------------------------

void CopySTACK(STACK *LHS,const STACK *RHS)

//--------------------------------------------------

{

   if ( LHS != RHS )

   {

      int offset;

      DestructSTACK(LHS);

      ConstructSTACK(LHS,RHS->capacity);

      Student provides missing code to traverse *RHS, PeekSTACK()-ing all elements

         contained in *RHS, PushSTACK()-ing each element, in turn, on to *LHS.

   }

}

(3) Add EQSTACK() as a new member function to STACK2.

//--------------------------------------------------

bool EQSTACK(const STACK *LHS,const STACK *RHS)

//--------------------------------------------------

{

   bool r;

   int offset;

  

   if ( LHS->size != RHS->size ) return( false );

   Student provides missing code to traverse *LHS and *RHS, PeekSTACK()-ing

      corresponding elements to ensure every pair of corresponding elements are

      equal; that is, compute r with the following universal quantified proposition

      r ¬ "offsetÎ[0,size-1] ( PeekSTACK(LHS,offset) = PeekSTACK(RHS,offset) )

   return( r );

}

(4) Take screenshot of compiled output

//----------------------------------------------------

// STACK2.h

//----------------------------------------------------

#ifndef STACK2_H

#define STACK2_H

#include <stdbool.h>

//==============================================================

// Data model definitions

//==============================================================

typedef struct STACK

{

   int size;

   int capacity;

   int *elements;

} STACK;

//==============================================================

// Public member function prototypes

//==============================================================

void ConstructSTACK(STACK *stack,const int capacity);

void DestructSTACK(STACK *stack);

void PushSTACK(STACK *stack,const int element);

void PopSTACK(STACK *stack);

int PeekSTACK(const STACK *stack,const int offset);

int GetSizeSTACK(const STACK *stack);

int GetCapacitySTACK(const STACK *stack);

bool IsFullSTACK(const STACK *stack);

bool IsEmptySTACK(const STACK *stack);

int PeekSTACK2(const STACK *stack,const int offset,bool *exception);

int PeekSTACK3(const STACK *stack,const int offset);

//==============================================================

// Private utility member function prototypes

//==============================================================

// (none)

#endif

//--------------------------------------------------

// STACK2.c

//--------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include ".Stack2.h"

#include "..ADTExceptions.h"

//--------------------------------------------------

void ConstructSTACK(STACK *stack,const int capacity)

//--------------------------------------------------

{

   if ( capacity <= 0 ) RaiseADTException(STACK_CAPACITY_ERROR);

   stack->size = 0;

   stack->capacity = capacity;

   stack->elements = (int *) malloc(sizeof(int)*capacity);

  

   if ( stack->elements == NULL ) RaiseADTException(MALLOC_ERROR);

}

//--------------------------------------------------

void DestructSTACK(STACK *stack)

//--------------------------------------------------

{

free(stack->elements);

}

//--------------------------------------------------

void PushSTACK(STACK *stack,const int element)

//--------------------------------------------------

{

   if ( IsFullSTACK(stack) ) RaiseADTException(STACK_OVERFLOW);

   stack->elements[stack->size] = element;

   ++stack->size;

}

//--------------------------------------------------

void PopSTACK(STACK *stack)

//--------------------------------------------------

{

   if ( IsEmptySTACK(stack) ) RaiseADTException(STACK_UNDERFLOW);

   stack->size--;

}

//--------------------------------------------------

int PeekSTACK(const STACK *stack,const int offset)

//--------------------------------------------------

{

   if ( !((0 <= offset) && (offset <= GetSizeSTACK(stack)-1)) ) RaiseADTException(STACK_OFFSET_ERROR);

   return( stack->elements[stack->size-(offset+1)] );

}

//--------------------------------------------------

int GetSizeSTACK(const STACK *stack)

//--------------------------------------------------

{

   return( stack->size );

}

//--------------------------------------------------

int GetCapacitySTACK(const STACK *stack)

//--------------------------------------------------

{

   return( stack->capacity );

}

//--------------------------------------------------

bool IsFullSTACK(const STACK *stack)

//--------------------------------------------------

{

   return( (stack->size == stack->capacity) ? true : false );

}

//--------------------------------------------------

bool IsEmptySTACK(const STACK *stack)

//--------------------------------------------------

{

   return( stack->size == 0 );

}

//--------------------------------------------------

int PeekSTACK2(const STACK *stack,const int offset,bool *exception)

//--------------------------------------------------

{

   if ( !((0 <= offset) && (offset <= GetSizeSTACK(stack)-1)) )

   {

      *exception = true;

      return( 0 );

   }

   *exception = false;

   return( stack->elements[stack->size-(offset+1)] );

}

//--------------------------------------------------

int PeekSTACK3(const STACK *stack,const int offset)

//--------------------------------------------------

{

   if ( !((0 <= offset) && (offset <= GetSizeSTACK(stack)-1)) )

   {

      fprintf(stderr," Exception "%s" ",STACK_OFFSET_ERROR);

      system("PAUSE");

      exit( 1 );

   }

   return( stack->elements[stack->size-(offset+1)] );

}

//--------------------------------------------------------------

// ADTExceptions.h

//--------------------------------------------------------------

#ifndef ADTEXCEPTIONS_H

#define ADTEXCEPTIONS_H

// ADT exception definitions

#define MALLOC_ERROR                "malloc() error"

#define DATE_ERROR                  "DATE error"

#define STACK_CAPACITY_ERROR        "STACK capacity error"

#define STACK_UNDERFLOW             "STACK underflow"

#define STACK_OVERFLOW              "STACK overflow"

#define STACK_OFFSET_ERROR          "STACK offset error"

// ADT exception-handler prototype

void RaiseADTException(char exception[]);

#endif

//--------------------------------------------------------------

// ADTExceptions.c

//--------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include ".ADTExceptions.h"

//--------------------------------------------------------------

void RaiseADTException(char exception[])

//--------------------------------------------------------------

{

   fprintf(stderr," Exception "%s" ",exception);

   system("PAUSE");

   exit( 1 );

}

//--------------------------------------------------------------

// STACK ADT Problem #2

// Problem2.c

//--------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include ".STACK2.h"

#include "..ADTExceptions.h"

//--------------------------------------------------------------

int main()

//--------------------------------------------------------------

{

   int capacity;

   printf("capacity? ");

   while ( scanf("%d",&capacity) != EOF )

   {

      STACK stack;

      int i,pushN,popN,peekN;

      ConstructSTACK(&stack,capacity);

      printf("pushN? "); scanf("%d",&pushN);

      printf("peekN? "); scanf("%d",&peekN);

      printf(" popN? "); scanf("%d",&popN);

      for (i = 1; i <= pushN; i++)

         PushSTACK(&stack,i);

/*

   Method (1) Allow ADT to RaiseADTException() which provides a uniform

      exception-handling approach for *ALL* ADT

*/

      for (i = 1; i <= peekN; i++)

         printf("%d ",PeekSTACK(&stack,i-1));

      printf(" ");

/*

   Method (2) ADT returns exception status (true or false) then client code

      checks status after *EVERY* ADT exception-producing operation call

      for (i = 1; i <= peekN; i++)

      {

         bool exception;

         int x = PeekSTACK2(&stack,i-1,&exception);

         if ( exception )

         {

            fprintf(stderr," Exception "%s" ",STACK_OFFSET_ERROR);

            system("PAUSE");

            exit( 1 );

         }

         printf("% d",x);

      }

      printf(" ");

*/

/*

   Method (3) ADT handles the exception (identical to Method (1)

      from client’s point-of-view)

      for (i = 1; i <= peekN; i++)

         printf("%d ",PeekSTACK3(&stack,i-1));

      printf(" ");

*/

      for (i = 1; i <= popN; i++)

         PopSTACK(&stack);

      DestructSTACK(&stack);

      printf(" capacity? ");

   }

   system("PAUSE");

   return( 0 );

}

Explanation / Answer

check once with this code... If you need any help, give me a comment....

// / ----------------------------------------------------

// STACK2.h

//----------------------------------------------------

#ifndef STACK2_H

#define STACK2_H

#include <stdbool.h>

//==============================================================

// Data model definitions

//==============================================================

typedef struct STACK

{

int size;

int capacity;

int *elements;

} STACK;

//==============================================================

// Public member function prototypes

//==============================================================

void ConstructSTACK(STACK *stack, const int capacity);

void DestructSTACK(STACK *stack);

void PushSTACK(STACK *stack, const int element);

void PopSTACK(STACK *stack);

int PeekSTACK(const STACK *stack, const int offset);

int GetSizeSTACK(const STACK *stack);

int GetCapacitySTACK(const STACK *stack);

bool IsFullSTACK(const STACK *stack);

bool IsEmptySTACK(const STACK *stack);

int PeekSTACK2(const STACK *stack, const int offset, bool *exception);

int PeekSTACK3(const STACK *stack, const int offset);

void OutputSTACK(const STACK *stack,FILE *OUT);

void CopySTACK(STACK *LHS,const STACK *RHS);

bool EQSTACK(const STACK *LHS,const STACK *RHS);

//==============================================================

// Private utility member function prototypes

//==============================================================

// (none)

#endif

// / --------------------------------------------------

// STACK2.c

//--------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include "./Stack2.h"

#include "./ADTExceptions.h"

//--------------------------------------------------

void ConstructSTACK(STACK *stack, const int capacity)

//--------------------------------------------------

{

if (capacity <= 0)

RaiseADTException(STACK_CAPACITY_ERROR);

stack->size = 0;

stack->capacity = capacity;

stack->elements = (int *)malloc(sizeof(int) * capacity);

if (stack->elements == NULL)

RaiseADTException(MALLOC_ERROR);

}

//--------------------------------------------------

void DestructSTACK(STACK *stack)

//--------------------------------------------------

{

free(stack->elements);

}

//--------------------------------------------------

void PushSTACK(STACK *stack, const int element)

//--------------------------------------------------

{

if (IsFullSTACK(stack))

RaiseADTException(STACK_OVERFLOW);

stack->elements[stack->size] = element;

++stack->size;

}

//--------------------------------------------------

void PopSTACK(STACK *stack)

//--------------------------------------------------

{

if (IsEmptySTACK(stack))

RaiseADTException(STACK_UNDERFLOW);

stack->size--;

}

//--------------------------------------------------

int PeekSTACK(const STACK *stack, const int offset)

//--------------------------------------------------

{

if (!((0 <= offset) && (offset <= GetSizeSTACK(stack) - 1)))

RaiseADTException(STACK_OFFSET_ERROR);

return (stack->elements[stack->size - (offset + 1)]);

}

//--------------------------------------------------

int GetSizeSTACK(const STACK *stack)

//--------------------------------------------------

{

return (stack->size);

}

//--------------------------------------------------

int GetCapacitySTACK(const STACK *stack)

//--------------------------------------------------

{

return (stack->capacity);

}

//--------------------------------------------------

bool IsFullSTACK(const STACK *stack)

//--------------------------------------------------

{

return ((stack->size == stack->capacity) ? true : false);

}

//--------------------------------------------------

bool IsEmptySTACK(const STACK *stack)

//--------------------------------------------------

{

return (stack->size == 0);

}

//--------------------------------------------------

int PeekSTACK2(const STACK *stack, const int offset, bool *exception)

//--------------------------------------------------

{

if (!((0 <= offset) && (offset <= GetSizeSTACK(stack) - 1)))

{

*exception = true;

return (0);

}

*exception = false;

return (stack->elements[stack->size - (offset + 1)]);

}

//--------------------------------------------------

int PeekSTACK3(const STACK *stack, const int offset)

//--------------------------------------------------

{

if (!((0 <= offset) && (offset <= GetSizeSTACK(stack) - 1)))

{

fprintf(stderr, " Exception "%s" ", STACK_OFFSET_ERROR);

system("PAUSE");

exit(1);

}

return (stack->elements[stack->size - (offset + 1)]);

}

// / --------------------------------------------------

void OutputSTACK(const STACK *stack, FILE *OUT)

//--------------------------------------------------

{

fprintf(OUT, "[%d:%d] ", GetSizeSTACK(stack), GetCapacitySTACK(stack));

if (IsEmptySTACK(stack))

printf("(empty)");

else

{

int offset;

int UB = GetSizeSTACK(stack) - 1;

for (offset = 0; offset <= UB; offset++)

fprintf(OUT, "%d%c", PeekSTACK(stack, offset), ((offset == UB) ? '.' : ' '));

}

}

// --------------------------------------------------

void CopySTACK(STACK *LHS, const STACK *RHS)

//--------------------------------------------------

{

if (LHS != RHS)

{

int offset;

DestructSTACK(LHS);

ConstructSTACK(LHS, RHS->capacity);

// Student provides missing code to traverse *RHS, PeekSTACK() - ing all elements

// contained in *RHS, PushSTACK() - ing each element, in turn, on to *LHS.

for (offset = 0; offset < RHS->size; offset++)

{

PushSTACK(LHS, offset);

}

}

}

// --------------------------------------------------

bool EQSTACK(const STACK *LHS, const STACK *RHS)

//--------------------------------------------------

{

bool r;

int offset;

if (LHS->size != RHS->size)

return (false);

// Student provides missing code to traverse *LHS and *RHS, PeekSTACK()-ing corresponding elements to ensure every pair of corresponding elements are equal; that is, compute r with the following universal quantified proposition r ¬ "offset Î [0,size-1] ( )

for (offset = 0; offset < RHS->size - 1; offset++)

{

if (PeekSTACK(LHS, offset) != PeekSTACK(RHS, offset))

{

return false;

}

}

return true;

}

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