Advertisement
Domy131097

[LV1] Algoritmi

Mar 12th, 2018
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. void gen_arr(float V[], int n, float dg, float gg){
  12. for(int i = 0; i < n; i++){
  13. V[i] = dg + (rand() / (RAND_MAX / (gg-dg)));
  14. }
  15. }
  16.  
  17. int sekv_pret(float V[], int n, float x){
  18. for(int i = 0; i < n; i++){
  19. if(V[i] == x) return i;
  20. }
  21. return -1;
  22. }
  23.  
  24. void Zamjena(float *x, float *y){
  25. float temp = 0;
  26. temp = *x;
  27. *x = *y;
  28. *y = temp;
  29. }
  30.  
  31. void Ispis(float V[], int n){
  32. printf("V[");
  33. for(int i = 0; i < n; i++){
  34. if(i < n-1) printf("%.2f, ", V[i]);
  35. else printf("%.2f]\n", V[i]);
  36. }
  37. }
  38. void sort(float V[], int n) {
  39. for(int i = 0; i < n-1; i++){
  40. for(int j = i + 1; j < n; j++){
  41. if(V[j] < V[i]) Zamjena(&V[j], &V[i]);
  42. }
  43. }
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49. int n;
  50. float *V = NULL;
  51. printf("Unesite broj elemenata:\n");
  52. scanf("%d", &n);
  53. V = (float *)malloc(sizeof(float)*n);
  54. gen_arr(V, n, 0, 100);
  55. Ispis(V,n);
  56. sort(V, n);
  57. Ispis(V, n);
  58. return 0;
  59. }
Advertisement
RAW Paste Data Copied
Advertisement