Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. //zad 1
  2. /*
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int add(int a, int b)
  8. {
  9.   return a+b;
  10. }
  11.  
  12.  
  13. int sub(int a, int b)
  14. {
  15.   return a-b;
  16. }
  17.  
  18. int main()
  19. {
  20.   int a,b;
  21.   cin>>a>>b;
  22.   int(*fun)(int,int);
  23.   fun=add;
  24.   cout<<fun(a,b)<<endl;
  25.   fun=sub;
  26.   cout<<fun(a,b)<<endl;
  27.   return 0;
  28. }
  29. */
  30.  
  31.   //zad 2
  32.   /*
  33.   #include <iostream>
  34.   using namespace std;
  35.  
  36. float add(float a,float b)
  37. {
  38.   return a+b;
  39. }
  40. float sub(float a,float b)
  41. {
  42.   return a-b;
  43. }
  44. float mul(float a,float b)
  45. {
  46.   return a*b;
  47. }
  48. float div(float a,float b)
  49. {
  50.   return a/b;
  51. }
  52.  
  53. int main()
  54. {
  55.   unsigned int o;
  56.   float a,b;
  57.   float (*fun[4])(float,float);
  58.   fun[0]=add;
  59.   fun[1]=sub;
  60.   fun[2]=mul;
  61.   fun[3]=div;
  62.   cin>>a>>b>>o;
  63.   cout<<fun[o](a,b)<<endl;
  64.  
  65.   return 0;
  66. }
  67.   */
  68.  
  69. //zad 3
  70. /*
  71. #include <iostream>
  72. using namespace std;
  73.  
  74. int min(int a,int b)
  75. {
  76.   return a<b ? a:b;
  77. }
  78. int max(int a,int b)
  79. {
  80.   return a>b ? a:b;
  81. }
  82.  
  83. int f(int arr[],int n, int (*fun)(int,int))
  84. {
  85.   int tmp=arr[0];
  86.   for(int i=0;i<n;++i)
  87.   tmp=fun(tmp,arr[i]);
  88.   return tmp;
  89. }
  90.  
  91. int main()
  92. {
  93.   int arr[]={1,2,3,4,5,6,7,8,0,15,12,7,6,22,11};
  94.   int n=15;
  95.   cout<<f(arr,n,min)<<endl;
  96.   cout<<f(arr,n,max)<<endl;
  97.   return 0;
  98. }
  99. */
  100.  
  101.  
  102. //zad 4
  103. /*
  104. #include <iostream>
  105. #include <cstdlib>
  106. using namespace std;
  107.  
  108. int min(const void *a, const void *b)
  109. {
  110.   float *ptr_a=(float *)a;
  111.   float *ptr_b=(float *)b;
  112.   float value_a= *ptr_a;
  113.   float value_b=*ptr_b;
  114.   //value_a=*(float*)a;value_b=*(float *)b;
  115.   if(value_a<value_b) return -1;
  116.   else if(value_a==value_b) return 0;
  117.   else return 1;
  118. }
  119.  
  120. int max(const void *a, const void *b)
  121. {
  122.  
  123.   return *(int*)b - *(int *)a;
  124. }
  125.  
  126.  
  127. int main()
  128. {
  129.   float arr1[]={1.f,2.f,3.f,5.f,13.f,0.f,12.f,13.f,5.f,7.f};
  130.   int arr2[]={1,2,3,4,5,6,7,8,0,15,12,7,6,22,11};
  131.  
  132.   qsort(arr1,10,sizeof(arr1[0]),min);
  133.   for(int i=0;i<10;++i)
  134.     cout<<arr1[i]<<" ";
  135.   cout<<endl;
  136.  
  137.   qsort(arr2,15,sizeof(arr2[0]),max);
  138.   for(int i=0;i<15;++i)
  139.     cout<<arr2[i]<<" ";
  140.     cout<<endl;
  141.     return 0;
  142.  
  143. }
  144. */
  145.  
  146.  
  147. //zad 5
  148. /*
  149. #include <iostream>
  150. #include <cstdlib>
  151. #include <cstring>
  152. using namespace std;
  153.  
  154. int compare(const void *p1, const void *p2)
  155. {
  156.   return -strcmp((char*)p1, (char*)p2);
  157. }
  158.  
  159.  
  160. int main()
  161. {
  162.   char arr[5][100]={"tygrys syberyjski","krokodyl nilowy","panda wielka","tygrys azjatycki","chomik europejski"};
  163.   qsort(arr,5,100,compare);
  164.   for(int i=0;i<5;++i) cout<<arr[i]<<endl;
  165.   return 0;
  166. }
  167. */
  168.  
  169. //zad 6
  170. /*
  171. #include <cstdio>
  172. #include <string>
  173.  
  174. using namespace std;
  175.  
  176. int main()
  177. {
  178.   char str[11]={0};
  179.   scanf("%10s",str);
  180.   int value=stoi(str)+10;
  181.   printf("%d\n",value);
  182.  
  183.  
  184.   return 0;
  185. }
  186.  
  187. */
  188.  
  189. //zad 7
  190. /*
  191. #include <iostream>
  192. using namespace std;
  193.  
  194. int binary_search(int *arr, int l, int r, int x)
  195. {
  196.   if(r>=1)
  197.     { int mid=l+(r-l)/2;
  198.       if(arr[mid]==x) return mid;
  199.       else if(arr[mid]>x)return binary_search(arr,l, mid-1,x);
  200.       else return binary_search(arr,mid+1,r,x);
  201.     }
  202.   return -1;
  203.  
  204. }
  205.  
  206. int main()
  207. {
  208.   int arr[]={1,2,3,4,10,22,23,42};
  209.   int x=4;
  210.   int n=sizeof(arr)/sizeof(arr[0]);
  211.   int result=binary_search(arr,0,n-1,x);
  212.   cout<<((result==-1)? "Brak": to_string(result)) <<endl;
  213.  
  214.   return 0;
  215. }
  216. */
  217.  
  218.  
  219. //zad 8
  220. /*
  221. #include <iostream>
  222. using namespace std;
  223.  
  224. void f(int *matrix, int N)
  225. {
  226.   for(int i=0;i<N;++i) for(int j=0;j<N;++j) matrix[i*N+j]=(i+1)*(j+1);
  227. }
  228.  
  229.  
  230. int main()
  231. {
  232.   int N=10;
  233.   int *arr=new int[N*N];
  234.   f(arr,N);
  235.   for(int i=0;i<N;++i){
  236.     for(int j=0;j<N;++j)
  237.       cout<<arr[i*N+j]<<" ";
  238.   cout<<endl;
  239.   }
  240.   delete [] arr;
  241.   return 0;
  242. }
  243. */
  244.  
  245. //zad 9
  246. /*
  247. #include <iostream>
  248. using namespace std;
  249.  
  250. void f(float *arr, int n, int m, float **result)
  251. {
  252.   for(int i=0;i<n;++i)
  253.     for(int j=0;j<m;++j)
  254.       if(i==0 || j==0 ||i==n-1 || j==m-1) result[i][j]=0.f;
  255.   else result[i][j]=arr[i*m+j];
  256.  
  257. }
  258.  
  259.  
  260. int main()
  261.  
  262. {
  263.   int n=3, m=5;
  264.   float **result=nullptr;
  265.   float arr[15]={1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f,1.f};
  266.   result=new float*[n];
  267.   for(int i=0;i<n;++i)
  268.     result[i]=new float[m];
  269.   f(arr,n,m,result);
  270.   for(int i=0;i<n;++i)
  271.     {
  272.       for(int j=0;j<m;++j)
  273.     cout<<result[i][j]<<" ";
  274.       cout<<endl;
  275.  
  276.     }
  277.   delete[] result;
  278.   return 0;
  279. }
  280. */
  281.  
  282.  
  283. //zad 10
  284. /*
  285. #include <iostream>
  286. #include <numeric> //iota, accumulate,inner_product
  287. #include <algorithm> //random_shuffle
  288. using namespace std;
  289.  
  290.  
  291.  
  292.  
  293. int main()
  294. {
  295.   int n,x,init=0;
  296.   cin>>n>>x;
  297.   int *t1=nullptr, *t2=nullptr;
  298.   t1=new int[n];
  299.   t2=new int[n];
  300.  
  301.   iota(t1,t1+n,x);
  302.   iota(t2,t2+n,x);
  303.   random_shuffle(t1,t1+n);
  304.   random_shuffle(t2,t2+n);
  305.   for(int i=0;i<n;++i) cout<<t1[i]<<" "; cout<<endl;
  306.   for(int i=0;i<n;++i) cout<<t2[i]<<" "; cout<<endl;
  307.  
  308.   cout<<accumulate(t1,t1+n,init)<<endl;
  309.   cout<<inner_product(t1,t1+n,t2,init)<<endl;
  310.  
  311.   delete[] t1;
  312.   delete[] t2;
  313.   return 0;
  314. }
  315. */
  316.  
  317. //zad 11
  318. /*
  319. #include <iostream>
  320. using namespace std;
  321.  
  322. #define MAX_SIZE 100
  323.  
  324. int main()
  325. {
  326.   char str[MAX_SIZE], reverse[MAX_SIZE];
  327.   char *s=str;
  328.   char *r=reverse;
  329.  
  330.   cin>>str;
  331.   int len=0;
  332.   while(*(s++)) len++;
  333.  
  334.   s--;
  335.  
  336.   while(len>=0)
  337.     {
  338.       *(r++)=*(--s);
  339.       len--;
  340.     }
  341.   *r='\0';
  342.   cout<<"Original: "<<str<<endl;
  343.   cout<<"Reverse: "<<reverse<<endl;
  344.  
  345.  
  346.   return 0;
  347. }
  348. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement