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

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

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

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free...

    JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

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
ADVERTISEMENT