Question

In: Computer Science

Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them...

Write push and pop functions on C/C++.

Have a program having 10 random integers(hardcoded)and put them on your stack.

Also, print them. Read a character from the keyboard.

If the character is an “o” then pop from the stack but don’t print.

If the character is a “p”, then pop from the stack and print that number.

If the character is an “e” or the stack is empty, end the program.

Solutions

Expert Solution

C program

#include<stdio.h>
#include<stdlib.h>
//stack array, stack top, item decalred as integer
int top,i, stack[100],item;
char ch;

void push()//push operation insert element to stack
{
        top++;
        stack[top]=item;
}
void pop()//pop operation delete element at top of stack
{
        item = stack[--top];
}
void display()//display function prints stack elements
{
        printf("Elements in stack:");
        for(i = top ; i > 0; i--)
                printf("%d ",stack[i]);
        printf("\n");
}

void compare()//'p', 'o' ,'e' operations are done in this function
{
        
        scanf("%c",&ch);
        if(ch == 'o') //delete top of stack
        {
                pop();
        }
                                
        else if(ch == 'p') //delete and display top of stack
        {
                pop();
                printf("Deleted Element is %d\n",item);
        }
        else if(ch == 'e')//End program
        {
                top = 0;
        }
}
void main()
{
        char ch;
        top = 0;
        for(int i = 0; i < 10 ; i++)//insert 10 elements in to the stack
        {
                item = (rand() % 11) + 10;//generate random integer
                push();
        }
        display();
        while(ch!='e' && top!=0)
        {
                printf("Enter character: ");
                compare();
        }
        printf("End Program");
}

C program screenshot

OUTPUT SCREENSHOT


Related Solutions

Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
LDR, POP, STR, PUSH, MOV What is the difference between them?
LDR, POP, STR, PUSH, MOV What is the difference between them?
Problem Description: In C write a program that assigns random integers between 1 and 20 to...
Problem Description: In C write a program that assigns random integers between 1 and 20 to a 5 x 5 two-dimensional array then displays the array with average of each row in a table format and the total of all elements in the array. Also calculate the total of each diagonal, top left to bottom right and top right to bottom left. Display the largest of the diagonal’s totals. Example of the array: 6          10        3          19        20        1          17       ...
Question: We have 10 students that each of them passed 3 tests. Write a c++ program...
Question: We have 10 students that each of them passed 3 tests. Write a c++ program that asks each student to enter 3 grades for each test and then find the average by using nested loop. Validate each grade. (before getting the grade test it whether it is greater than 0 and less than 100).
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Write a program that ask the user for three integers. Use two functions to determine the...
Write a program that ask the user for three integers. Use two functions to determine the largest and smallest number of the three. One function will determine the largest number, and the other function will determine the smallest number. (6 points) In c++ using functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT