Question

CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification: A stack is a container that can be defined in terms of an array where all adds are preformed at the end of the sequence of existing values, and all removes are also preformed at end of the sequence of existing values. An empty stack is one that has no existing values in the array at all. We use the notion of top of the stack to keep track of (at which index) a new value in the sequence is to be added. Hence, an empty stack is one in which top is equal to 0 and a full stack is one in which top is equal to the arrays physical size capacity. For this assignment, you will write a C program to create and use an array based stack Requirements: You must write a C program which satisfies the following: . Your C program must dynamically allocate the memory for the underlying array of float values, with a capacity of 1024. Your C program must dynamically free this memory before termination. Your C program must declare and define functions to: - Create an empty stack (of float values) with a capacity of 1024. - Test to see if the stack is empty - Test to see if the stack is full - Push (add) an item (a float value) onto the stack - if not already full -Pop (remove) an item (a float value) from the stack if not already empty . Your C program must define a main function to fully test all of the above funcionality. Notes: 1. A stack is really three separate data items: (a) a float pointer to the array (b) an int top index indicator (c) an int capacity (physical size of the array) 2. The effective size of the stack (number of items in the stack) cabe deduced from the top index indicator Sample run(s):

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

stack.c

#include<stdio.h>

#include<stdlib.h>

#include<limits.h> //INT_MIN

//structure for stack

struct stack

{

int top;

int capacity;

float *array;

};

//create a emptyp stack of size capacity

struct stack* createStack(int capacity)

{

struct stack* stk=(struct stack*)malloc(sizeof(struct stack));

stk->capacity=capacity;

stk->top=0;

stk->array=(float*)malloc(stk->capacity*sizeof(float));

return stk;

}

//check for stack is full or not

int isFull(struct stack *stk)

{

if(stk->top==(stk->capacity-1))

return 1;

return 0;

}

//check for stack is empty

int isEmpty(struct stack* stk)

{

return stk->top==0;

}

//push data to stack

void push(struct stack* stk,float data)

{

if(isFull(stk))

{

printf("\nstack is full");

return;

}

stk->array[++stk->top]=data;

printf("\n%.2f pushed to stack\n",data);

}

//pop data from stack

float pop(struct stack *stk)

{

if(isEmpty(stk))

{

printf("\nStack is empty");

return INT_MIN;

}

return stk->array[stk->top--];

}

int main()

{

struct stack* stk=createStack(1024);

push(stk,10.24);

push(stk,13.0);

push(stk,14.21);

printf("\n%.2f popped from stack\n",pop(stk));

push(stk,1.78);

push(stk,90.21);

printf("\n%.2f Popped from stack\n",pop(stk));

return 0;

}//end of main

OUTPUT

Add a comment
Know the answer?
Add Answer to:
CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification:...
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
  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory...

    Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to Co Sci 839 or google: "binary...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  •      program will enter data into two single dimension arrays (do not st...

         program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...

  • Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise...

    Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise as a practice for c programming, could someone do this as well so i could compare if my code makes makes sense and to see and help correct the errors im getting right now.. Any helpful help would be appreciated.. Pointers &Pointer arithmetic Memory allocation and freeing Main topics: Exercise This lab is designed to give you practice working with pointers and memory allocation...

  • Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...

    Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++. Assignment In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type. A driver program is provided for this assignment to test your implementation. You don't have to write the tests. Program You will need to write a single template class for this...

  • Write a Client class with a main method that tests the data structures as follows: For...

    Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...

  • Write a C++ program that will test function described below that use pointers and dynamic memory...

    Write a C++ program that will test function described below that use pointers and dynamic memory allocation. Functions: You will write the function described below. Then you will call them from the main function, to demonstrate their correctness. concat_array: takes two int arrays and the arrays' sizes as arguments (that's 4 arguments). It should create a new array big enough to store both arrays. Then it should copy the contents of the first array to the new array, and then...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

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