Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int function1 (int a1, int an, int n)
- {
- return ((a1+an)/2)*n;
- }
- int function2 (int a1, int q, int n)
- {
- return a1 * ((1-(pow(q,n)))/(1-q));
- }
- int main()
- {
- printf("Wybierz co chcesz policzyc:\n");
- printf("1) Suma ciagu arytmetycznego\n");
- printf("2) Suma ciagu geometrycznego\n");
- int wybor;
- scanf("%d",&wybor);
- int (*pointer)(int, int, int) = NULL;
- switch(wybor)
- {
- case 1:
- {
- int a1,an,n;
- printf("Podaj pierwszy wyraz ciagu:");
- scanf("%d",&a1);
- printf("Podaj ostatni wyraz ciagu:");
- scanf("%d",&an);
- printf("Podaj ilosc wyrazow ciagu:");
- scanf("%d",&n);
- pointer = function1;
- printf("Suma ciagu arytmetycznego: %d",function1(a1,an,n));
- }
- break;
- case 2:
- {
- int a1,q,n;
- printf("Podaj pierwszy wyraz ciagu:");
- scanf("%d",&a1);
- printf("Podaj iloraz ciagu:");
- scanf("%d",&q);
- printf("Podaj ilosc wyrazow ciagu:");
- scanf("%d",&n);
- pointer = function2;
- printf("Suma ciagu geometrycznego: %d",function2(a1,q,n));
- }
- break;
- default:
- printf("Nie ma takiej pocji w menu");
- }
- return 0;
- }
- ---------------------
- #include<stdio.h>
- #include<stdlib.h>
- #define NUMBER_OF_ELEMENTS 20
- void fill_array(int *array)
- {
- int i;
- for(i=0;i<NUMBER_OF_ELEMENTS;i++)
- array[i]=i;
- }
- void print_array(int *array)
- {
- int i;
- for(i=0;i<NUMBER_OF_ELEMENTS;i++)
- printf("%d ",array[i]);
- puts("");
- }
- void swap(int *first, int *second)
- {
- if(first!=second) {
- *first ^= *second;
- *second ^= *first;
- *first ^= *second;
- }
- }
- void selection_sort(int *array)
- {
- int i,j;
- for(i=0; i<NUMBER_OF_ELEMENTS-1; i++) {
- int min = i;
- for(j=i+1; j<NUMBER_OF_ELEMENTS; j++)
- if(array[min]<array[j])
- min = j;
- if(min!=i)
- swap(&array[min],&array[i]);
- }
- }
- int main(void)
- {
- int *array_pointer = (int *)calloc(NUMBER_OF_ELEMENTS,sizeof(int));
- if(array_pointer)
- {
- fill_array(array_pointer);
- print_array(array_pointer);
- selection_sort(array_pointer);
- printf("Oto wartosci tablicy posortowane malejaco:\n");
- print_array(array_pointer);
- free(array_pointer);
- array_pointer = NULL;
- }
- return 0;
- }
- --------------------------
- #include<stdio.h>
- #include<stdlib.h>
- #define NUMBER_OF_ELEMENTS 20
- void fill_array(int *array)
- {
- int i;
- for(i=0;i<NUMBER_OF_ELEMENTS;i++)
- *(array+i) = i; //wykorzystanie arytmetyki wskaznikow do przypisania wartosci i kolejnym elementom tablicy
- }
- void print_array(int *array)
- {
- int i;
- for(i=0;i<NUMBER_OF_ELEMENTS;i++)
- printf("%d ",array[i]);
- puts("");
- }
- int main(void)
- {
- int *array_pointer = (int *)calloc(NUMBER_OF_ELEMENTS,sizeof(int));
- if(array_pointer)
- {
- fill_array(array_pointer);
- print_array(array_pointer);
- free(array_pointer);
- array_pointer = NULL;
- }
- return 0;
- }
- ----------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NUMBER_OF_ELEMENTS 20
- void fill_array(int * array) {
- int i;
- for (i = 0; i < NUMBER_OF_ELEMENTS; i++)
- array[i] = i;
- }
- void print_array(int * array) {
- int i;
- for (i = 0; i < NUMBER_OF_ELEMENTS; i++)
- printf("%d ", array[i]);
- puts("");
- }
- _Bool allocate_array(int **array_pointer, unsigned int number_of_elements, unsigned int element_size) {
- unsigned long int array_size = element_size * number_of_elements;
- (*array_pointer) = malloc(array_size);
- if (*array_pointer) {
- (*array_pointer) = memset(*array_pointer, 0, array_size);
- } else {
- return 0;
- }
- return 1;
- }
- int main(void) {
- int * array_pointer = NULL;
- if (allocate_array(&array_pointer, NUMBER_OF_ELEMENTS, sizeof(int))) {
- fill_array(array_pointer);
- print_array(array_pointer);
- free(array_pointer);
- array_pointer = NULL;
- }
- return 0;
- }
- ------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void macierz (int **tab,int x)
- {
- srand(time(NULL));
- int i,j;
- for(i=0;i<x;i++)
- {
- for(j=0;j<x;j++)
- {
- tab[i][j] = (rand()%21)-10;
- printf("%d\t",tab[i][j]);
- }
- printf("\n");
- }
- }
- void suma_elementow (int **tab, int x)
- {
- int i;
- int suma = 0;
- for(i=0;i<x;i++)
- {
- suma += tab[i][i];
- }
- printf("\nSuma elementow lezacych na przekatnej: %d",suma);
- }
- void uzupelnij(int rozmiar, int *mac)
- {
- srand(time(0));
- int i;
- for(i=0;i<rozmiar;i++)
- {
- }
- }
- int main()
- {
- int x;
- printf("Podaj rozmiar macierzy: ");
- scanf("%d",&x);
- int i;
- int **tab; // tworzysz tablice tablic
- tab = (int**) malloc(x * sizeof(int*)); // alokujesz miejsce na x tablic - kolumny
- for(i = 0; i < x; ++i)
- {
- tab[i] = (int*) malloc(x * sizeof(int*)); // alokujesz w kazdej z kolumn miejsce na tablice - wiersze
- }
- macierz(tab,x);
- suma_elementow(tab,x);
- for(i=0;i<x;i++)
- free(tab[i]);
- free(tab);
- tab = NULL;
- return 0;
- }
- -------------------
- #include <stdio.h>
- #include <stdlib.h>
- void funkcja(size_t rozmiar, int *buffer)
- {
- size_t maximumLength = rozmiar;
- int liczba;
- int i = 0;
- while(liczba != 0)
- {
- printf("Podaj liczbe: ");
- scanf("%d", &liczba);
- if(i == maximumLength)
- {
- int *newBuffer = realloc(buffer, (maximumLength = maximumLength + rozmiar) * sizeof (*buffer) );
- if (!newBuffer)
- {
- free(buffer);
- perror("blad realloc");
- }
- buffer = newBuffer;
- }
- buffer[i++]=liczba;
- }
- }
- void wyswietlanie(int *array)
- {
- int i = 0;
- while(array[i] != 0)
- {
- printf("%d ",array[i]);
- i++;
- }
- }
- int main()
- {
- const size_t rozmiar = 5;
- int *buffer;
- buffer = (int*)malloc(rozmiar * sizeof(*buffer));
- if(buffer)
- {
- funkcja(rozmiar,buffer);
- wyswietlanie(buffer);
- free(buffer);
- buffer = NULL;
- }
- return 0;
- }
- ------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 20
- _Bool allocate_array(double ** array_pointer, unsigned int size) {
- unsigned int array_size = sizeof(double) * size;
- (*array_pointer) = (double *) malloc(array_size);
- if (*array_pointer) {
- (*array_pointer) = memset((*array_pointer), 1.0, array_size);
- return 1;
- } else {
- return 0;
- }
- }
- void fill_array(double * array, unsigned int size) {
- for (int i = 0; i < size; i++) {
- double value;
- scanf("%lf", &value);
- array[i] = value;
- }
- }
- void print_array(double (*func)(double), double * array, unsigned int size) {
- for (int i = 0; i < size; i++) {
- func(array[i]);
- }
- }
- void print_element(double elem) {
- printf("%f ", elem);
- }
- void save_element(double elem) {
- FILE *file = fopen("file.txt", "a");
- if (file) {
- fprintf(file, "%f ", elem);
- } else {
- printf("Nie udalo sie otworzyc pliku file.txt\n");
- exit(1);
- }
- fclose(file);
- }
- int main() {
- double * array_pointer;
- int choice;
- void (*function_pointer)(double elem)= NULL;
- printf("Wybierz funkcje: 1 - wswietlanie wartosci, 2 - zapis do pliku\n");
- scanf("%d", &choice);
- if (choice == 1) {
- function_pointer = print_element;
- } else if (choice == 2) {
- function_pointer = save_element;
- } else {
- printf("Bledny wybor\n");
- exit(1);
- }
- if (allocate_array(&array_pointer, SIZE)) {
- printf("Wprowadz 20 liczb zmiennoprzecinkowych:\n");
- fill_array(array_pointer, SIZE);
- print_array(*function_pointer, array_pointer, SIZE);
- free(array_pointer);
- array_pointer = NULL;
- }
- free(function_pointer);
- function_pointer = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement