|
1 |
| -int max(int x, int y) |
2 |
| -{ |
3 |
| - if(x > y) |
4 |
| - return y; |
| 1 | +#include<stdio.h> |
5 | 2 |
|
6 |
| - return x; |
| 3 | +int search(int arr[], unsigned int arr_len, int x){ |
| 4 | + for (unsigned int i = 0; i < arr_len; i++) |
| 5 | + if (arr[(unsigned int)i] == x) |
| 6 | + return (int)i; |
| 7 | + return -1; |
| 8 | +} |
| 9 | + |
| 10 | +int factorial(int num); |
| 11 | + |
| 12 | +int verifyFactorial(int num){ |
| 13 | + int fact = 1; |
| 14 | + if(num < 0){ |
| 15 | + fact = -1; |
| 16 | + } |
| 17 | + else if(num > 1){ |
| 18 | + do{ |
| 19 | + fact = fact * num; |
| 20 | + num = num - 1; |
| 21 | + }while(num>1); |
| 22 | + } |
| 23 | + else { |
| 24 | + fact = 1; |
| 25 | + } |
| 26 | + return fact; |
7 | 27 | }
|
8 | 28 |
|
9 |
| -/* |
10 |
| - You can write C code here or look at the examples. |
11 |
| - It will be translated as "demo/demo_translation.c". |
12 |
| - There are also translations of full programs below. |
13 |
| -*/ |
14 |
| -void main(int argc, char** argv) |
15 |
| -{ |
16 |
| - int i = 2, j = 2, k =2; |
17 |
| - if(2 >1) |
18 |
| - {printf("true");} |
19 |
| - else {printf("false");} |
20 |
| - while (j>i) |
21 |
| - {j=j-1; |
22 |
| - i=i+1;} |
23 |
| - printf("Value of this %d And done.\n", i+j+k); |
24 |
| - printf("Value of argument %d And done.\n", argc); |
25 |
| - printf("Value of this %d And done.\n", i+j+k); |
| 29 | +void main() { |
| 30 | + int arr[] = { 1, 2, 3, 4, 5, 6 }; |
| 31 | + int num = 6; |
| 32 | + int result = search(arr, 6, num); |
| 33 | + printf("%d\n", result); |
| 34 | + result = factorial(num); |
| 35 | + printf("%d\n", result); |
| 36 | +} |
26 | 37 |
|
| 38 | +//This should be linked to its function declaration |
| 39 | +int factorial(int num){ |
| 40 | + int copyNum = num; |
| 41 | + int fact = 1; |
| 42 | + if(num < 0) |
| 43 | + fact = -1; |
| 44 | + else{ |
| 45 | + while(num > 1){ |
| 46 | + fact = fact * num--; |
| 47 | + } |
| 48 | + } |
| 49 | + return fact==verifyFactorial(copyNum)? fact:-2; |
27 | 50 | }
|
| 51 | + |
| 52 | + |
0 commit comments