Advertisement
icatalin

lab 03 9.03.2018

Mar 9th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7.     int i, x;
  8.     int a, b, k;
  9.  
  10.     printf("a = ");
  11.     scanf("%d",&a);
  12.     printf("b = ");
  13.     scanf("%d",&b);
  14.  
  15.     printf("k = ");
  16.     scanf("%d",&k);
  17.  
  18.     x = rand()%(b + 1 - a) + a;
  19.     printf("\n (x = %d)\n",x);
  20.  
  21.     int ok = 0;
  22.  
  23.     for (i=0; i<k; i++)
  24.     {
  25.         int guess;
  26.  
  27.         printf("Ghiciti numarul. ");
  28.         scanf("%d",&guess);
  29.         printf("\n");
  30.  
  31.         if (guess == x)
  32.         {
  33.             printf("Numar ghicit.");
  34.             break;
  35.             ok = 1;
  36.         }
  37.     }
  38.  
  39.     if (ok == 0)
  40.         printf("\nNumarul nu a fost ghicit.");
  41.  
  42.     srand(time(NULL));
  43.  
  44.     return 0;
  45. }
  46.  
  47. //////////////
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <time.h>
  52.  
  53. int func(int m, int n, int (*f)(int))
  54. {
  55.     int i, sum = 0;
  56.  
  57.     for (i=m; i<=n; i++)
  58.         sum = sum + f(i);
  59.  
  60.     return sum;
  61. }
  62.  
  63. int f1(int x)
  64. {
  65.     return x*(x+1)/2;
  66. }
  67.  
  68. int f2(int x)
  69. {
  70.     return 3*x*x + 7;
  71. }
  72.  
  73. int f3(int x)
  74. {
  75.     if (x > 0)
  76.         return 1;
  77.     if (x < 0)
  78.         return -1;
  79.     return 0;
  80. }
  81.  
  82. int main()
  83. {
  84.     printf("%d",func(1,3,f2));
  85.     return 0;
  86. }
  87.  
  88.  
  89. ///////////////////
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <time.h>
  93.  
  94. void *cautare(void *val, void *tablou, int nr_elemente, int dim_el)
  95. {
  96.     int i, c = 0;
  97.     char *pt = tablou;
  98.  
  99.     for (i=0; i<nr_elemente; i++)
  100.         if (memcmp(val,pt + i*dim_el,dim_el) == 0)
  101.             c++;
  102.  
  103.     return c;
  104. }
  105.  
  106. int main()
  107. {
  108.     int v[] = {1,2,2,4,5};
  109.     int a = 2;
  110.     printf("%d",cautare(&a,v,5,sizeof(int)));
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement