Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- long tribonacci (int n) {
- if (n<2) return 0;
- else if (n==2) return 1;
- else return
- tribonacci(n-1)
- + tribonacci(n-2)
- + tribonacci(n-3);
- }
- int coil(int a) {
- if (a<=0)
- return 0;
- else return a + coil(a - 2);
- }
- double carpet(double field, int recursion) {
- if (recursion == 0) return field * 8.0/9.0;
- else return carpet(field * 8.0/9.0, recursion - 1);
- }
- int main(void) {
- int n;
- scanf("%d", &n);
- printf("%d\n", tribonacci(n));
- int a;
- scanf("%d", &a);
- printf("%d\n", coil(a));
- int recursion;
- double field;
- scanf("%d", &recursion);
- scanf("%lf", &field);
- printf("%d, %lf\n", recursion, field);
- printf("%lf\n", carpet(field, recursion));
- }
- #include <stdio.h>
- #define SIZE 10
- int ar[SIZE];
- int top1 = -1;
- int top2 = SIZE;
- //Functions to push data
- void push_stack1 (int data)
- {
- if (top1 < top2 - 1)
- {
- ar[++top1] = data;
- }
- else
- {
- printf ("Stack Full! Cannot Push\n");
- }
- }
- void push_stack2 (int data)
- {
- if (top1 < top2 - 1)
- {
- ar[--top2] = data;
- }
- else
- {
- printf ("Stack Full! Cannot Push\n");
- }
- }
- //Functions to pop data
- void pop_stack1 ()
- {
- if (top1 >= 0)
- {
- int popped_value = ar[top1--];
- printf ("%d is being popped from Stack 1\n", popped_value);
- }
- else
- {
- printf ("Stack Empty! Cannot Pop\n");
- }
- }
- void pop_stack2 ()
- {
- if (top2 < SIZE)
- {
- int popped_value = ar[top2++];
- printf ("%d is being popped from Stack 2\n", popped_value);
- }
- else
- {
- printf ("Stack Empty! Cannot Pop\n");
- }
- }
- //Functions to Print Stack 1 and Stack 2
- void print_stack1 ()
- {
- int i;
- for (i = top1; i >= 0; --i)
- {
- printf ("%d ", ar[i]);
- }
- printf ("\n");
- }
- void print_stack2 ()
- {
- int i;
- for (i = top2; i < SIZE; ++i)
- {
- printf ("%d ", ar[i]);
- }
- printf ("\n");
- }
- int main()
- {
- int ar[SIZE];
- int i;
- int num_of_ele;
- printf ("We can push a total of 10 values\n");
- //Number of elements pushed in stack 1 is 6
- //Number of elements pushed in stack 2 is 4
- for (i = 1; i <= 6; ++i)
- {
- push_stack1 (i);
- printf ("Value Pushed in Stack 1 is %d\n", i);
- }
- for (i = 1; i <= 4; ++i)
- {
- push_stack2 (i);
- printf ("Value Pushed in Stack 2 is %d\n", i);
- }
- //Print Both Stacks
- print_stack1 ();
- print_stack2 ();
- //Pushing on Stack Full
- printf ("Pushing Value in Stack 1 is %d\n", 11);
- push_stack1 (11);
- //Popping All Elements From Stack 1
- num_of_ele = top1 + 1;
- while (num_of_ele)
- {
- pop_stack1 ();
- --num_of_ele;
- }
- //Trying to Pop From Empty Stack
- pop_stack1 ();
- return 0;
- }
- #include <stdio.h>
- int MAXSIZE = 8;
- int stack[8];
- int top = -1;
- int isempty() {
- if(top == -1)
- return 1;
- else
- return 0;
- }
- int isfull() {
- if(top == MAXSIZE)
- return 1;
- else
- return 0;
- }
- int peek() {
- return stack[top];
- }
- int pop() {
- int data;
- if(!isempty()) {
- data = stack[top];
- top = top - 1;
- return data;
- } else {
- printf("Could not retrieve data, Stack is empty.\n");
- }
- }
- int push(int data) {
- if(!isfull()) {
- top = top + 1;
- stack[top] = data;
- } else {
- printf("Could not insert data, Stack is full.\n");
- }
- }
- int main() {
- // push items on to the stack
- push(3);
- push(5);
- push(9);
- push(1);
- push(12);
- push(15);
- printf("Element at top of the stack: %d\n" ,peek());
- printf("Elements: \n");
- // print stack data
- while(!isempty()) {
- int data = pop();
- printf("%d\n",data);
- }
- printf("Stack full: %s\n" , isfull()?"true":"false");
- printf("Stack empty: %s\n" , isempty()?"true":"false");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement