Advertisement
BartekCK

Untitled

Mar 6th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int function1 (int a1, int an, int n)
  6. {
  7.     return ((a1+an)/2)*n;
  8. }
  9.  
  10. int function2 (int a1, int q, int n)
  11. {
  12.     return a1 * ((1-(pow(q,n)))/(1-q));
  13. }
  14.  
  15. int main()
  16. {
  17.     printf("Wybierz co chcesz policzyc:\n");
  18.     printf("1) Suma ciagu arytmetycznego\n");
  19.     printf("2) Suma ciagu geometrycznego\n");
  20.     int wybor;
  21.     scanf("%d",&wybor);
  22.     int (*pointer)(int, int, int) = NULL;
  23.  
  24.     switch(wybor)
  25.     {
  26.         case 1:
  27.             {
  28.                 int a1,an,n;
  29.                 printf("Podaj pierwszy wyraz ciagu:");
  30.                 scanf("%d",&a1);
  31.                 printf("Podaj ostatni wyraz ciagu:");
  32.                 scanf("%d",&an);
  33.                 printf("Podaj ilosc wyrazow ciagu:");
  34.                 scanf("%d",&n);
  35.  
  36.                 pointer = function1;
  37.                 printf("Suma ciagu arytmetycznego: %d",function1(a1,an,n));
  38.             }
  39.             break;
  40.  
  41.         case 2:
  42.             {
  43.                 int a1,q,n;
  44.                 printf("Podaj pierwszy wyraz ciagu:");
  45.                 scanf("%d",&a1);
  46.                 printf("Podaj iloraz ciagu:");
  47.                 scanf("%d",&q);
  48.                 printf("Podaj ilosc wyrazow ciagu:");
  49.                 scanf("%d",&n);
  50.                 pointer = function2;
  51.                 printf("Suma ciagu geometrycznego: %d",function2(a1,q,n));
  52.             }
  53.             break;
  54.         default:
  55.             printf("Nie ma takiej pocji w menu");
  56.     }
  57.  
  58.     return 0;
  59. }
  60. ---------------------
  61. #include<stdio.h>
  62. #include<stdlib.h>
  63. #define NUMBER_OF_ELEMENTS 20
  64.  
  65. void fill_array(int *array)
  66. {
  67. int i;
  68. for(i=0;i<NUMBER_OF_ELEMENTS;i++)
  69. array[i]=i;
  70. }
  71.  
  72. void print_array(int *array)
  73. {
  74. int i;
  75. for(i=0;i<NUMBER_OF_ELEMENTS;i++)
  76. printf("%d ",array[i]);
  77. puts("");
  78. }
  79.  
  80. void swap(int *first, int *second)
  81. {
  82. if(first!=second) {
  83. *first ^= *second;
  84. *second ^= *first;
  85. *first ^= *second;
  86. }
  87. }
  88.  
  89. void selection_sort(int *array)
  90. {
  91.     int i,j;
  92.  
  93.         for(i=0; i<NUMBER_OF_ELEMENTS-1; i++) {
  94.         int min = i;
  95.         for(j=i+1; j<NUMBER_OF_ELEMENTS; j++)
  96.         if(array[min]<array[j])
  97.         min = j;
  98.         if(min!=i)
  99.         swap(&array[min],&array[i]);
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106. int main(void)
  107. {
  108. int *array_pointer = (int *)calloc(NUMBER_OF_ELEMENTS,sizeof(int));
  109.  
  110. if(array_pointer)
  111. {
  112. fill_array(array_pointer);
  113. print_array(array_pointer);
  114.  
  115. selection_sort(array_pointer);
  116.  
  117. printf("Oto wartosci tablicy posortowane malejaco:\n");
  118.  
  119. print_array(array_pointer);
  120.  
  121. free(array_pointer);
  122. array_pointer = NULL;
  123. }
  124. return 0;
  125. }
  126. --------------------------
  127. #include<stdio.h>
  128. #include<stdlib.h>
  129. #define NUMBER_OF_ELEMENTS 20
  130.  
  131. void fill_array(int *array)
  132. {
  133. int i;
  134. for(i=0;i<NUMBER_OF_ELEMENTS;i++)
  135.     *(array+i) = i; //wykorzystanie arytmetyki wskaznikow do przypisania wartosci i kolejnym elementom tablicy
  136. }
  137.  
  138. void print_array(int *array)
  139. {
  140. int i;
  141. for(i=0;i<NUMBER_OF_ELEMENTS;i++)
  142. printf("%d ",array[i]);
  143. puts("");
  144. }
  145.  
  146. int main(void)
  147. {
  148. int *array_pointer = (int *)calloc(NUMBER_OF_ELEMENTS,sizeof(int));
  149.  
  150. if(array_pointer)
  151. {
  152. fill_array(array_pointer);
  153. print_array(array_pointer);
  154.  
  155. free(array_pointer);
  156. array_pointer = NULL;
  157. }
  158. return 0;
  159. }
  160. ----------------
  161. #include <stdio.h>
  162. #include <stdlib.h>
  163. #include <string.h>
  164.  
  165. #define NUMBER_OF_ELEMENTS 20
  166.  
  167. void fill_array(int * array) {
  168.   int i;
  169.   for (i = 0; i < NUMBER_OF_ELEMENTS; i++)
  170.     array[i] = i;
  171. }
  172.  
  173. void print_array(int * array) {
  174.   int i;
  175.   for (i = 0; i < NUMBER_OF_ELEMENTS; i++)
  176.     printf("%d ", array[i]);
  177.   puts("");
  178. }
  179.  
  180. _Bool allocate_array(int **array_pointer, unsigned int number_of_elements, unsigned int element_size) {
  181.   unsigned long int array_size = element_size * number_of_elements;
  182.   (*array_pointer) = malloc(array_size);
  183.  
  184.   if (*array_pointer) {
  185.     (*array_pointer) = memset(*array_pointer, 0, array_size);
  186.   } else {
  187.     return 0;
  188.   }
  189.   return 1;
  190. }
  191.  
  192. int main(void) {
  193.   int * array_pointer = NULL;
  194.  
  195.   if (allocate_array(&array_pointer, NUMBER_OF_ELEMENTS, sizeof(int))) {
  196.     fill_array(array_pointer);
  197.     print_array(array_pointer);
  198.  
  199.     free(array_pointer);
  200.     array_pointer = NULL;
  201.   }
  202.   return 0;
  203. }
  204. ------------------------
  205. #include <stdio.h>
  206. #include <stdlib.h>
  207. #include <time.h>
  208.  
  209.     void macierz (int **tab,int x)
  210.     {
  211.         srand(time(NULL));
  212.         int i,j;
  213.  
  214.         for(i=0;i<x;i++)
  215.         {
  216.             for(j=0;j<x;j++)
  217.             {
  218.                 tab[i][j] = (rand()%21)-10;
  219.                 printf("%d\t",tab[i][j]);
  220.             }
  221.             printf("\n");
  222.         }
  223.     }
  224.  
  225.     void suma_elementow (int **tab, int x)
  226.     {
  227.         int i;
  228.         int suma = 0;
  229.  
  230.         for(i=0;i<x;i++)
  231.         {
  232.             suma += tab[i][i];
  233.         }
  234.         printf("\nSuma elementow lezacych na przekatnej: %d",suma);
  235.     }
  236.  
  237.  
  238.  
  239. void uzupelnij(int rozmiar, int *mac)
  240. {
  241.     srand(time(0));
  242.     int i;
  243.  
  244.     for(i=0;i<rozmiar;i++)
  245.     {
  246.  
  247.     }
  248. }
  249.  
  250.  
  251. int main()
  252. {
  253.  
  254.     int x;
  255.     printf("Podaj rozmiar macierzy: ");
  256.     scanf("%d",&x);
  257.  
  258.     int i;
  259.     int **tab; // tworzysz tablice tablic
  260.     tab = (int**) malloc(x * sizeof(int*)); // alokujesz miejsce na x tablic - kolumny
  261.  
  262.         for(i = 0; i < x; ++i)
  263.         {
  264.         tab[i] = (int*) malloc(x * sizeof(int*)); // alokujesz w kazdej z kolumn miejsce na tablice - wiersze
  265.         }
  266.  
  267.     macierz(tab,x);
  268.  
  269.     suma_elementow(tab,x);
  270.  
  271.     for(i=0;i<x;i++)
  272.         free(tab[i]);
  273.  
  274.     free(tab);
  275.     tab = NULL;
  276.  
  277.     return 0;
  278. }
  279. -------------------
  280. #include <stdio.h>
  281. #include <stdlib.h>
  282.  
  283.  
  284. void funkcja(size_t rozmiar, int *buffer)
  285. {
  286.     size_t maximumLength = rozmiar;
  287.  
  288.     int liczba;
  289.     int i = 0;
  290.  
  291.      while(liczba != 0)
  292.     {
  293.         printf("Podaj liczbe: ");
  294.         scanf("%d", &liczba);
  295.  
  296.         if(i == maximumLength)
  297.         {
  298.             int *newBuffer = realloc(buffer, (maximumLength = maximumLength + rozmiar) * sizeof (*buffer) );
  299.             if (!newBuffer)
  300.             {
  301.                 free(buffer);
  302.                 perror("blad realloc");
  303.             }
  304.             buffer = newBuffer;
  305.         }
  306.  
  307.         buffer[i++]=liczba;
  308.     }
  309.  
  310.  
  311.  
  312. }
  313.  
  314.  
  315. void wyswietlanie(int *array)
  316. {
  317.     int i = 0;
  318.     while(array[i] != 0)
  319.     {
  320.         printf("%d ",array[i]);
  321.         i++;
  322.     }
  323. }
  324.  
  325.  
  326.  
  327.  
  328. int main()
  329. {
  330.     const size_t rozmiar = 5;
  331.     int *buffer;
  332.     buffer = (int*)malloc(rozmiar * sizeof(*buffer));
  333.  
  334.     if(buffer)
  335.     {
  336.         funkcja(rozmiar,buffer);
  337.         wyswietlanie(buffer);
  338.  
  339.         free(buffer);
  340.         buffer = NULL;
  341.     }
  342.  
  343.  
  344.  
  345.  
  346.  
  347.     return 0;
  348. }
  349. ------------------------
  350. #include <stdio.h>
  351. #include <stdlib.h>
  352. #include <string.h>
  353.  
  354. #define SIZE 20
  355.  
  356. _Bool allocate_array(double ** array_pointer, unsigned int size) {
  357.     unsigned int array_size = sizeof(double) * size;
  358.     (*array_pointer) = (double *) malloc(array_size);
  359.     if (*array_pointer) {
  360.         (*array_pointer) = memset((*array_pointer), 1.0, array_size);
  361.         return 1;
  362.     } else {
  363.         return 0;
  364.     }
  365. }
  366.  
  367. void fill_array(double * array, unsigned int size) {
  368.     for (int i = 0; i < size; i++) {
  369.         double value;
  370.         scanf("%lf", &value);
  371.         array[i] = value;
  372.     }
  373. }
  374.  
  375. void print_array(double (*func)(double), double * array, unsigned int size) {
  376.     for (int i = 0; i < size; i++) {
  377.         func(array[i]);
  378.     }
  379. }
  380.  
  381. void print_element(double elem) {
  382.     printf("%f ", elem);
  383. }
  384.  
  385. void save_element(double elem) {
  386.     FILE *file = fopen("file.txt", "a");
  387.     if (file) {
  388.         fprintf(file, "%f ", elem);
  389.     } else {
  390.         printf("Nie udalo sie otworzyc pliku file.txt\n");
  391.         exit(1);
  392.     }
  393.     fclose(file);
  394. }
  395.  
  396.  
  397. int main() {
  398.     double * array_pointer;
  399.     int choice;
  400.     void (*function_pointer)(double elem)= NULL;
  401.  
  402.     printf("Wybierz funkcje: 1 - wswietlanie wartosci, 2 - zapis do pliku\n");
  403.     scanf("%d", &choice);
  404.  
  405.     if (choice == 1) {
  406.         function_pointer = print_element;
  407.     } else if (choice == 2) {
  408.         function_pointer = save_element;
  409.     } else {
  410.         printf("Bledny wybor\n");
  411.         exit(1);
  412.     }
  413.  
  414.     if (allocate_array(&array_pointer, SIZE)) {
  415.         printf("Wprowadz 20 liczb zmiennoprzecinkowych:\n");
  416.         fill_array(array_pointer, SIZE);
  417.         print_array(*function_pointer, array_pointer, SIZE);
  418.         free(array_pointer);
  419.         array_pointer = NULL;
  420.     }
  421.  
  422.         free(function_pointer);
  423.         function_pointer = NULL;
  424.     return 0;
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement