Advertisement
Guest User

YEP

a guest
Oct 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4.  
  5. float aux;
  6. void starton(void){
  7.     aux = clock();
  8. }
  9. float startoff(void){
  10.     return(clock() - (float)aux) / CLOCKS_PER_SEC; // didn't work on my machine with clk_tck
  11. }
  12. typedef struct Lista{
  13.    
  14.     int ID,nr;
  15.     char Name[100];
  16.    
  17. } NOD;
  18. NOD vals[100];
  19. int LINEARSRCH(int x,NOD *vals, int n){
  20.     int i=0;
  21.     while ((i < n - 1) && (vals[i].ID != x))
  22.         i++;
  23.     if (vals[i].ID != x){
  24.        
  25.         printf("The array doesn't contain the searched element\n");
  26.         return 0;
  27.        
  28.     }
  29.     else{
  30.        
  31.         printf("The element was found at index %d \n", i);
  32.         return i;
  33.        
  34.     }
  35. }
  36. int LINEARSRCHSAN(int x,NOD *vals, int n){
  37.     int i = 0;
  38.     vals[n].ID = x;
  39.     while (vals[i].ID != x)
  40.         i++;
  41.     if (i == n ){
  42.        
  43.         printf("The array doesn't contain the searched element\n");
  44.         return 0;
  45.        
  46.     }
  47.     else{
  48.        
  49.         printf("The element was found at index %d \n", i+1);
  50.         return i;
  51.        
  52.     }
  53. }
  54. int main(){
  55.     int i = 0;
  56.     FILE *fp = fopen("FisierCORalfabetic.txt","r");
  57.     if (fp == NULL){
  58.        
  59.         printf("The file cannot be opened \n");
  60.         return 0;
  61.        
  62.     }
  63.         while (!feof(fp)){
  64.            
  65.         fscanf(fp,"%d", &vals[i].nr);
  66.         fscanf(fp,"%d", &vals[i].ID);
  67.         fgets(vals[i].Name, 90,fp);
  68.         i++;
  69.            
  70.     }
  71.    
  72.     starton();
  73.     LINEARSRCH(14,vals,i-1);
  74.     aux = startoff();
  75.     printf("Durata liniar search:  %f\n",aux);
  76.     starton();
  77.     LINEARSRCHSAN(14,vals,i-1);
  78.     aux = startoff();
  79.     printf("Durata liniar search santinel:  %f",aux);
  80.    
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement