Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

It failed the first two tests, it passed only the third test, can you help me fix the problem? Thank you. prog1_2.h typedef struct stack{

It failed the first two tests, it passed only the third test, can you help me fix the problem? Thank you.

prog1_2.h

typedef struct stack{ int* data; int size; int capacity; } STACK; STACK* MakeStack(int initialCapacity); void Push(STACK *stackPtr,int data); int Pop(STACK *stackPtr); void Grow(STACK *stackPtr);

prog1_2.c

#include "prog1_2.h" #include #include

STACK* MakeStack(int initialCapacity) { STACK *s = (STACK *)malloc(sizeof(STACK)); s->capacity = initialCapacity; s->size = 0; s->data = (int *)malloc(sizeof(int)*initialCapacity); return s; }

void Push(STACK *stackPtr, int data) { if(stackPtr->size capacity) { stackPtr->data[stackPtr->size++] = data; } }

int Pop(STACK *stackPtr) { if(stackPtr->size == 0) { return -1; } else { return stackPtr->data[--(stackPtr->size)]; } }

void Grow(STACK *stackPtr) { int i; stackPtr->capacity *= 2; int *arr = (int *)malloc(sizeof(int)*stackPtr->capacity); for(i = 0; i size; ++i) { arr[i] = stackPtr->data[i]; } stackPtr->data = arr; }

prog1_3.c

#include "prog1_2.h"

#include

#include

#include

int main(int argc, char *argv[])

{

int n;

int i;

char line[256];

char command[15], value [15];

STACK* stk = MakeStack(10);

printf("Assignment #1-3 ");

if(argc != 2)

{

printf("This program expects a single command line argument. ");

return 1;

}

n = atoi(argv[1]);

for(int i = 1; i

{

printf("> ");

fgets(line, 256, stdin);

sscanf(line, "%s", command);

if(strcmp(command, "push") == 0)

{

sscanf(line, "%s %s", command, value);

Push(stk, atoi(value));

}

else if(strcmp(command, "pop") == 0)

{

printf("%d ", Pop(stk));

}

}

}

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed It failed the first two tests, it passed only the third test, can you help me fix the problem? Thank you.

prog1 3 Create a driver program that takes a single command line argument N (in addition to the name of the program being executed). The command line argument N will be an integer number. If there is not a single command line argument your program should print an error message and quit. For valid inputs, your program will print a single right carrot take N lines of input from STDIN (entire line inputs). Your program will split each of these lines on a space delimiter. Your program should ignore multiple space characters (i.e. treat as many spaces in a row as a single space. It should also ignore leading and trailing spaces. For example Hello World should only result in two tokens, "Hello" and "World". Neither token should have spaces in the string. If there are exactly two tokens AND the first token is the string "push" then your program should convert the second token to an integer and push the result onto your STACK If there is exactly one token AND the token is the string "pop" then your program should Pop a value off of the STACK and print it to STDOUT on its own line. All other input should be ignored. Your program should only print the assignment header, the error message, the carrot at the beginning of the input line or the result of a Pop. Your program can assume that all STDIN inputs will be less than 256 characters, the command line argument will be an integer (if it exists) and the second token in a two token input that has push" as the first token will be an integer Example compilation Example execution ./prog1 3 8 Example run (User input to STDIN highlighted with yellow) Assignment #1-3 > Push 4 > push 2 > abs pop > pop 1 > push 3 more > push 2 pop 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions