Advertisement
vencinachev

Kursova-St

Jun 8th, 2020
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #define MAX_COUNT 100
  6.  
  7. int main()
  8. {
  9.     int numbers[MAX_COUNT];
  10.     int n, num;
  11.     char option;
  12.  
  13.     while (1)
  14.     {
  15.         printf("a. Enter numbers from console\n");
  16.         printf("b. Check for number in the array\n");
  17.         printf("c. Print the numbers in console\n");
  18.         printf("d. Count of elements squared which are smaller then max element\n");
  19.         printf("e. Read the numbers from file\n");
  20.         printf("f. Save the numbers in file\n");
  21.         printf("g. Exit\n");
  22.         option = getch();
  23.  
  24.         if (option == 'a')
  25.         {
  26.             n = enterArrayConsole(numbers);
  27.         }
  28.         else if (option == 'b')
  29.         {
  30.             printf("Enter number: ");
  31.             scanf("%d", &num);
  32.             if (checkForElement(numbers, n, num) != -1)
  33.             {
  34.                 printf("%d is found!", num);
  35.             }
  36.             else
  37.             {
  38.                 printf("%d is not found!", num);
  39.             }
  40.         }
  41.         else if (option == 'c')
  42.         {
  43.             printArray(numbers, n);
  44.         }
  45.         else if (option == 'd')
  46.         {
  47.             printf("Count is: %d", func4(numbers, n));
  48.         }
  49.         else if (option == 'e')
  50.         {
  51.              n = readArrayFile(numbers);
  52.         }
  53.         else if (option == 'f')
  54.         {
  55.             saveArrayFile(numbers, n);
  56.         }
  57.         else if (option == 'g')
  58.         {
  59.             return;
  60.         }
  61.         else
  62.         {
  63.             printf("Invalid input!\n");
  64.         }
  65.         printf("\n\n");
  66.         getch();
  67.  
  68.     }
  69.     return 0;
  70. }
  71.  
  72. int enterArrayConsole(int *numbers)
  73. {
  74.     int i, n;
  75.     printf("Enter number of elements: ");
  76.     scanf("%d", &n);
  77.     for (i = 0; i < n; i++)
  78.     {
  79.         printf("Enter number[%d]:", i);
  80.         scanf("%d", &numbers[i]);
  81.     }
  82.     return n;
  83. }
  84.  
  85. void printArray(int *numbers, int n)
  86. {
  87.     int i;
  88.     for (i = 0; i < n; i++)
  89.     {
  90.         printf("%d, ", numbers[i]);
  91.     }
  92. }
  93.  
  94. int checkForElement(int *numbers, int n, int num)
  95. {
  96.     int i;
  97.     for (i = 0; i < n; i++)
  98.     {
  99.         if (numbers[i] == num)
  100.         {
  101.             return i;
  102.         }
  103.     }
  104.     return -1;
  105. }
  106.  
  107. void saveArrayFile(int *numbers, int n)
  108. {
  109.     int i;
  110.     char fname[50];
  111.     FILE *fwrite;
  112.     printf("Enter file name: ");
  113.     scanf("%s", fname);
  114.     if((fwrite = fopen(fname, "w")) == 0)
  115.     {
  116.         printf("Problem to open/create the file!");
  117.         return;
  118.     }
  119.     for (i = 0; i < n; i++)
  120.     {
  121.         fprintf(fwrite, "%d\n", numbers[i]);
  122.     }
  123.     fclose(fwrite);
  124. }
  125.  
  126. int readArrayFile(int *numbers)
  127. {
  128.     int i = 0, n = 0;
  129.     char fname[50];
  130.     FILE *fread;
  131.     printf("Enter file name: ");
  132.     scanf("%s", fname);
  133.     if((fread = fopen(fname, "r")) == 0)
  134.     {
  135.         printf("Problem to open the file!");
  136.         return;
  137.     }
  138.     while (fscanf(fread, "%d", &numbers[i]) != EOF)
  139.     {
  140.         i++;
  141.         n++;
  142.     }
  143.     fclose(fread);
  144.     return n;
  145. }
  146.  
  147. // how elements squared are smaller then max element
  148. int func4(int *numbers, int n)
  149. {
  150.     int i, count = 0, maxIndex = 0;
  151.     int max = numbers[0];
  152.     for (i = 1; i < n; i++)
  153.     {
  154.         if (numbers[i] > numbers[maxIndex])
  155.         {
  156.             maxIndex = i;
  157.         }
  158.     }
  159.  
  160.     for (i = 0; i < n; i++)
  161.     {
  162.         if (i == maxIndex)
  163.         {
  164.             continue;
  165.         }
  166.         if (numbers[i] * numbers[i] < numbers[maxIndex])
  167.         {
  168.             count++;
  169.         }
  170.     }
  171.     return count;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement