Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void clrScr()
- {
- system("cls");
- }
- int loadDiamonds(double **diamonds)
- {
- int numberOfDiamonds, i;
- double *pointer;
- pointer = *diamonds;
- printf("Wprowadz liczbe diamentow: ");
- scanf("%d", &numberOfDiamonds);
- clrScr();
- pointer = (double*)malloc(sizeof(double) * numberOfDiamonds);
- for(i = 0; i < numberOfDiamonds; i++)
- {
- printf("Wprowadz wartosc %i diamentu: ", i + 1);
- scanf("%lf", &pointer[i]);
- clrScr();
- }
- *diamonds = pointer;
- return numberOfDiamonds;
- }
- int calculatePairs(double **prices, int numberOfDiams, double *diams)
- {
- double *pricesPtr;
- int numberOfPairs;
- int i, n, k;
- if(numberOfDiams < 2)
- {
- numberOfPairs = 0;
- }
- else
- {
- n = 2;
- for(i = 3; i <= numberOfDiams; i++)
- {
- n *= i;
- }
- k = 1;
- for(i = 2; i <= (numberOfDiams - 2); i++)
- {
- k *= i;
- }
- k *= 2;
- numberOfPairs = n / k;
- pricesPtr = *prices;
- pricesPtr = (double*)malloc(sizeof(double)*numberOfPairs);
- if(numberOfPairs == 1)
- {
- pricesPtr[0] = diams[0] + diams[1];
- }
- else
- {
- n = 0;
- for(i = 0; i < (numberOfDiams - 1); i++)
- {
- for(k = i + 1; k < numberOfDiams; k++)
- {
- pricesPtr[n] = diams[i] + diams[k];
- n++;
- }
- }
- }
- }
- *prices = pricesPtr;
- return numberOfPairs;
- }
- void checkPrice(double *prices, int diamonds, int pairs)
- {
- int a, b, n, i;
- printf("Wprowadz numer 1 diamentu: ");
- scanf("%d", &a);
- clrScr();
- printf("Wprowadz numer 2 diamentu: ");
- scanf("%d", &b);
- clrScr();
- if(b == a)
- {
- printf("BLAD!");
- }
- else
- {
- if(a > b)
- {
- int c;
- c = b;
- b = a;
- a = c;
- }
- n = 0;
- for(i = 0; i < (a - 1); i++)
- {
- n += diamonds - i - 1;
- }
- n += (b - a) - 1;
- printf("Cena za %d i %d diament to %lf", a, b, prices[n]);
- }
- }
- int main()
- {
- int numberOfDiamonds, numberOfUniquePairs;
- double *diamonds;
- double *prices;
- numberOfDiamonds = loadDiamonds(&diamonds);
- numberOfUniquePairs = calculatePairs(&prices, numberOfDiamonds, diamonds);
- checkPrice(prices, numberOfDiamonds, numberOfUniquePairs);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement