Question

In: Computer Science

Write a c program to calculate the factorial of a number using recursion    [8] Question five...

  1. Write a c program to calculate the factorial of a number using recursion    [8]

Question five

  1. Write a stack algorithm to POP an item                                                         [6]
  2. What does FRONT and REAR signify in a queue?                                                                 [6]
  3. Write an algorithm for a dequeue operation                                                                       [8]

Solutions

Expert Solution

Write a c program to calculate the factorial of a number using recursion .

#include <stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, factorial(n));
return 0;
}
int factorial(int n)
{
if (n >= 1)
return n*factorial(n-1);
else
return 1;
}

Write a stack algorithm to POP an item

 function pop: stack

   if stack is empty
      return null
   endif
   
   data ← stack[top]
   top ← top - 1
   return data

end 

Function://implementation

int pop(int data) {

   if(!isempty()) {
      data = stack[top];
      top = top - 1;   
      return data;
   } else {
      printf("Could not retrieve data, Stack is empty.\n");
   }
}

What does FRONT and REAR signify in a queue?  

Front and rear variables point to the position from where insertions and deletions are performed in a queue. Initially, the value of front and queue is -1 which represents an empty queue. Array representation of a queue containing 5 elements along with the respective values of front and rea

A B C D E
0 1 2 3 4 5
FRONT REAR

FRONT POINTS TO A

REAR POINTS TO E

Write an algorithm for a dequeue operation       

function dequeue
   
   if queue is empty
      return underflow
   end if

   data = queue[front]
   front ← front + 1
   return true

end function

Implementation

int dequeue() {
   if(isempty())
      return 0;

   int data = queue[front];
   front = front + 1;

   return data;
}

Related Solutions

In C++, write a program that uses array to calculate the factorial of a reasonable large...
In C++, write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).
In C++. Write a program that uses array to calculate the factorial of a reasonable large...
In C++. Write a program that uses array to calculate the factorial of a reasonable large number (say, up to 2,000).  
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the program for the value 10 and compute the Execution time in a MIPS processor at 2.4GHz. The CPI is 1.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write down the C++ Program To Find Factorial.
Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a factorial C++ program where n=10,000,000
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT