Advertisement
ppupil2

PRF192_workshop-6

Mar 31st, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5.  
  6. void menu(void);
  7. int input(float a[]);
  8. void display(float a[], int n);
  9. void bubblesort (float a[], int size);
  10. int find (float search, float a[], int size);
  11.  
  12. int main() {
  13.     float a[100], x;
  14.     int choice, pos, n = 0;
  15.     char ch;
  16.    
  17.     while (1) {
  18.         while (1) {
  19.             system("cls");
  20.             menu();
  21.             printf("                     Enter your selection (0 -> 4): ");
  22.             fflush(stdin);
  23.             ch = '\0';
  24.             scanf("%d%c", &choice, &ch);
  25.             printf("\n");
  26.             if (choice >= 0 && choice <= 4 && ch == '\n') {
  27.                 break;
  28.             }
  29.             printf("                     Your selection is incorrect\n");
  30.             printf("\n");
  31.             printf("           Press any key to continue...\n");
  32.             getch();
  33.         }
  34.        
  35.        
  36.         if (choice == 0) { // get out while(1) if choice == '0'
  37.             break;
  38.         }
  39.         switch (choice) {
  40.             case 1: {
  41.                 n = input(a);
  42.                 break;
  43.             }
  44.             case 2: {
  45.                 if (n == 0) printf("           Please enter number first.\n");
  46.                 else {
  47.                     bubblesort(a, n);
  48.                     printf("           Successful.\n");
  49.                 }
  50.                 break;
  51.             }
  52.             case 3: {
  53.                 if (n == 0) printf("           Please enter number first.\n");
  54.                 else {
  55.                     /* input x and check validation */
  56.                     while (1) {
  57.                         printf("           Enter the value x to be searched: ");
  58.                         fflush(stdin);
  59.                         ch = '\0';
  60.                         scanf("%f%c", &x, &ch);
  61.                         if (ch == '\n') {
  62.                             break;
  63.                         }
  64.                         printf("           Invalid input, please re-enter!\n\n");
  65.                     }
  66.                    
  67.                     pos = find(x, a, n);
  68.                     if (pos != -1) {
  69.                         printf("           Found %.2f at position %d.\n", x, pos+1);
  70.                     }
  71.                     else {
  72.                         printf("           %.2f is not found.\n", x);
  73.                     }
  74.                 }
  75.                 break;
  76.             }
  77.             case 4: {
  78.                 if (n == 0) printf("           Please enter number first.\n");
  79.                 else display(a, n);
  80.                 break;
  81.             }
  82.         }
  83.         printf("\n");
  84.         printf("           Press any key to continue...\n");
  85.         getch();
  86.     }
  87.     return (0);
  88. }
  89.  
  90. void menu(void) {
  91.     printf("                     +---------------------------------------+\n");
  92.     printf("                     |         Array of real numbers         |\n");
  93.     printf("                     +---------------------------------------+\n");
  94.     printf("                     | 1. Enter the list of float numbers    |\n");
  95.     printf("                     | 2. Sort the list in inceasing order   |\n");
  96.     printf("                     | 3. Search the value x in the list     |\n");
  97.     printf("                     | 4. Display the list                   |\n");
  98.     printf("                     | 0. Exit                               |\n");
  99.     printf("                     +---------------------------------------+\n\n");
  100. }
  101.  
  102. int input(float a[]) {
  103.     int n, x;
  104.     char ch[100], c;
  105.     bool check;
  106.    
  107.     /* input n and check validation */
  108.     while (1) {
  109.         printf("           Input the size of array n = ");
  110.         fflush(stdin);
  111.         c = '\0';
  112.         scanf("%d%c", &n, &c);
  113.         if (n>0 && c == '\n') {
  114.             break;
  115.         }
  116.         printf("           Invalid input, please re-enter!\n\n");
  117.     }
  118.    
  119.     /* input n float numbers and check validation */
  120.     while (1) {
  121.         printf("           Enter %d float number: ", n);
  122.         c = '\0';
  123.         for (int i = 0; i<n; i++) {
  124.             scanf("%f", &a[i]);
  125.             if (i == (n-1)) { /* after number position n must be '\n' */
  126.                 c = getchar();
  127.             }
  128.         }
  129.         if (c == '\n') {
  130.             break;
  131.         }
  132.         printf("           Invalid input, please re-enter!\n\n");
  133.         fflush(stdin);     
  134.     }
  135.  
  136.     return (n);
  137. }
  138.  
  139. void display(float a[], int n) {
  140.     printf("           Position: ");
  141.     for (int i = 1; i<=n; i++) {
  142.         printf("%-9d", i);
  143.     }
  144.     printf("\n           Value:    ");
  145.     for (int i = 0; i<n; i++) {
  146.         printf("%-9.2f", a[i]);
  147.     }
  148.     printf("\n");
  149. }
  150.  
  151. void bubblesort (float a[], int size) {
  152.     int i, j;
  153.     float temp;
  154.    
  155.     for (i = size - 1; i > 0; i--) {
  156.         for (j = 0; j < i; j++) {
  157.             if (a[j] > a[j+1]) {
  158.                 temp = a[j];
  159.                 a[j] = a[j+1];
  160.                 a[j+1] = temp;
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166. int find (float search, float a[], int size) {
  167.     int i = 0, rc = -1; // rc = -1 mean not found, if found rc record position of key[i]
  168.    
  169.     for (i = 0; i < size && rc == -1; i++) {
  170.         if (search == a[i]) {
  171.             rc = i;
  172.         }
  173.     }
  174.     return rc;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement