forked from hermanwongkm/CS1010
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit.c
52 lines (44 loc) · 1.46 KB
/
rabbit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* rabbit.c
* A rabbit can jump at most 50 centimetres. It needs to cross a
* river, with rocks positioned in a straight line in the river.
* The program computes the minimum number of jumps for the rabbit
* to cross to the other side of the river.
*/
#include <stdio.h>
int countJumps(int [], int);
int main(void) {
int num_rocks, rockDistance[20], x,i; // including opposite bank
printf("Enter number of rocks: ");
scanf("%d", &num_rocks);
for(i = 0; i < num_rocks; i++){
scanf("%d",&rockDistance[i]);
}
x = countJumps(rockDistance,num_rocks);
printf("%d\n",x);
return 0;
}
// Counts the minimum number of jumps the rabbit needs
// to take to get to the other side of the river.
// Precond: size > 0
int countJumps(int rocks[], int size) {
int i = 0, post = 0,currentPost, counter = 0;
//printf("Function initalized\n");
do{
currentPost = post;//rabbit jumped
// if(currentPost == post)
// return -1;
//printf("While Loop initalized\n");
while(currentPost + 50 >= rocks[i]){
if(i == (size))
break;
post = rocks[i];
i ++;
// printf(" i = %d and post = %d\n",i,post);
}
if(currentPost == post)
return -1;
counter++;
}while(post < rocks[size -1]); //position is not at last rock
return counter; // this is just a stub; replace it with the correct value
}