Advertisement
Aminpro

[ECP1026] In Lab 2, Question 1

Dec 13th, 2012
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. //https://www.facebook.com/AminproPastebin
  2. // Would like to thank Raymond Choo for helping :)
  3.  
  4. #include <stdio.h>
  5.  
  6. int ways=0;
  7. int totaltrns=0;
  8.  
  9.  
  10. void queue(int length,int turns){
  11.        
  12.        
  13.     turns++;
  14.     if (length>0){
  15.         queue(length-1,turns);
  16.         if (length>1){
  17.             queue(length-2,turns);
  18.         }
  19.     }
  20.         else{
  21.         ways++;
  22.         totaltrns+=turns;
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.        
  29.     while(1){
  30.        
  31.         int length;
  32.         float average;
  33.         ways=0;
  34.         totaltrns=0;
  35.        
  36.        
  37.         printf("Queue length: ");
  38.         scanf("%d", &length);
  39.         queue(length,-1);
  40.         average=(float)totaltrns/(float)ways;
  41.         printf("The average turns are %.1f\n", average);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement